Blog

Find here all the articles written at Python for the Lab.

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.
Using else with loops May 25, 2020 Python Tip: Using Else with Loops Most likely, you are aware of how to use the else statement with an if clause. However, Python also allows us to use them with loops. They are straightforward to understand and open some exciting possibilities. Before continuing, remember that else in this context should be called no-break. Read Article
Python Tips Matplotlib Saving May 18, 2020 Python Tip: Ready to Publish Matplotlib Figures Saving figures for publications, presentations, books, or websites can be a cumbersome task but doesn't need to be. In this Python Tip, we will see how to create images using Matplotlib that are ready to be embedded. We will take care of the most important aspects: shape, font sizes, and resolution. You won't have to tinker ever again with random parameters until the result is right. Read Article
Descritpors in Python May 16, 2020 Data Descriptors: Bringing Attributes to the Next Level Descriptors in Python allow us to control how attributes of classes are accessed or modified. A pattern often encountered is defining properties to use setter and getter methods encapsulated as if they were a single attribute. In this article, we will dig into how the property decorator works, to understand how to implement our solutions. Read Article
Python tips sets May 11, 2020 Python Tips: Using Sets None Read Article
Generators Iterables Iterators April 10, 2020 Generators, Iterables, Iterators in Python: When and Where Generators, Iterables, and Iterators are some of the most used tools in Python. However, we don't often stop to think about how they work, how we can develop our generators and iterables. Once you learn what you can do with them, it is possible to expand your toolbox and make your code much more efficient and pythonic. Read Article
How to control an Arduino from the computer using Python Feb. 4, 2020 How to control an Arduino from the computer using Python Arduinos may be one of the most disruptive electronic developments of the past decade. They opened a world of possibilities to quickly prototype solutions in professional settings but also opened the door for enthusiasts to learn about electronics and microcontrollers. In this article, we are going to see how to get started with an Arduino board and how to control it from a computer using Python. Read Article
How to create a setup file for your project Jan. 20, 2020 How to create a setup file for your project When you develop software, at some point you will want other people to be able to use what you have built. Sometimes it is handy if you can quickly use the code you developed yourself in the past on a new machine or a different virtual environment. We have already seen that for importing to work properly, it is very important to have well-defined packages, and that Python knows where to find them. Read Article
Screenshot PFTL Jan. 2, 2020 Website Redesigned and Improved To start a new year, we have decided to give Python for the Lab a complete new look. We hired a very talented designer to work on improving the look and feel of the website. It is incredible how many different details must be taken into account when deciding how a website should look. From color palette, and icons, to typography. We really hope you enjoy the new version of the website. We know we do love how it looks now! Read Article
Complete guide to imports Oct. 4, 2019 Complete Guide to Imports in Python: Absolute, Relative, and More Importing is not only a matter of using external libraries, but it also allows you to keep your code clean and organized. In this tutorial, we are going to discuss from the very basics of importing to complex topics such as lazy loading of modules in your packages. You are free to skip ahead to the section that compels you the most. Read Article
Handling and sharing data between threads Aug. 6, 2019 Handling and Sharing Data Between Threads When working with threads in Python, you will find very useful to be able to share data between different tasks. One of the advantages of threads in Python is that they share the same memory space, and thus exchanging information is relatively easy. However, some structures can help you achieve more specific goals. Read Article
Starting and Synchronizing Threads Aug. 6, 2019 Starting and Synchronizing Threads If you have developed code for long enough, probably you have faced the situation in which a task takes longer to complete and in the meantime, your program can't perform any other task. Most likely you can't even politely cancel what the program is doing, you will have to resort to the Ctrl+C strategy. Fortunately, Python has different approaches to overcome these issues. Read Article
Monkey patching and its consequences June 18, 2019 Monkey Patching and its consequences Monkey patching is a technique that allows you to alter the behavior of objects at runtime. Even though it can be a very useful feature, it can also make your code much harder to understand and debug, and therefore you have to be careful with how you implement monkey patching. In this article, we are going to see some examples of how you can use monkey patching to solve quickly specific problems. We are also going to discuss the consequences of monkey patching in the context of larger projects. Read Article
Duck typing, or how to check variable types June 11, 2019 Duck Typing, or how to check variable types The name duck typing has its root in the expression If it walks like a duck and it quacks like a duck, then it must be a duck. Which means that if you want to know whether you are dealing with a duck or not, you only care about how it looks like and how it behaves rather than actually checking to what species the animal belongs. Read Article
Intro to python lambda functions March 17, 2019 Intro to Python Lambda Functions Some time ago, Python introduced in its syntax the possibility to define functions using lambda instead of def. These functions are called anonymous and are very common in other languages such as Javascript. However, in Python, they seem a bit obscure and are often either overlooked or misused. In this article, we are going to introduce the lambda functions and discuss where and how to use it. Read Article
What are args and kwargs and when to use them March 10, 2019 What are args and kwargs and when to use them If you have worked with Python for long enough, probably you have encountered code that uses *args and **kwargs as arguments in functions. Even if you haven't, it is a very neat feature that allows you to achieve great flexibility while developing code. In this article, we are going to discuss what and how to use flexible arguments in functions. Read Article
Using pyZMQ for inter-process communication: Part 2 March 5, 2019 Using pyZMQ for inter-process communication: Part 2 In this article, we are going to cover how you can leverage the possibilities of ZMQ to exchange data between different processes in Python. We have covered the basics of pyZMQ in part 1. This is a fairly advanced tutorial, in which we are not only going to use pyZMQ, but also the multiprocessing library, HDF5, and openCV. We are going to acquire images from the webcam as fast as possible, we are going to save the data to disk during the acquisition, and we are going to perform some basic analysis. Read Article
Building a CRM with Jupyter notebooks Feb. 12, 2019 Building a CRM with Jupyter Notebooks This tutorial is going to be off-topic compared to the others on the website. It was born out of a question regarding how to send personalized e-mails to several people on a list, and I thought it could be useful to post a tutorial online. This will help people interested in building a simple Customer Relationship Manager (CRM) and it will also show scientists that the skills they develop while working in the lab can be used in various contexts. Read Article
Deep and Shallow Copies of objects Feb. 4, 2019 Deep and Shallow Copies of Objects Copying objects in Python seems like a trivial task, but it can have unexpected implications in your programs. Copying data may be achieved by either duplicating the data or by storing references to the objects, having a much lower impact on the memory. In this article, we are going to review the differences between deep and shallow copies of objects in Python, including custom classes. Read Article

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.