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.
Python-setup.jpg 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
Python for the Lab screenshot 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
imports-blog-illustration_linkedin.png 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
python1-01_linkedin.png 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
python1-01.png 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
shashank-sahay-1659565-unsplash_linkedin.jpg 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
joshua-coleman-655076-unsplash_linkedin.jpg 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
ivana-cajina-324103-unsplash_linkedin.jpg 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
luca-bravo-217276-unsplash_linkedin.jpg 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
thomas-jensen-592813-unsplash_linkedin.jpg 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
rawpixel-760036-unsplash_linkedin.jpg 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
rebecca-georgia-269933-unsplash_linkedin.jpg 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
tobias-fischer-185901-unsplash_linkedin.jpg Feb. 2, 2019 The with command and custom classes There is a common pattern when programming that is opening a resource, doing something with it and closing it. This is what you normally do with a file, a network connection or a device. Python offers you a command to handle this pattern: the 'with' context manager. In this article, we are going to see how you can develop classes that follow the same pattern. Read Article
thomas-jensen-592813-unsplash_linkedin.jpg Dec. 17, 2018 Using pyZMQ for inter-process communication: Part 1 Working with threads and processes in Python (and in any other language) always posses the challenge on how to exchange information between them. We are not talking about parallelizing code in a traditional way, where an expensive computation is spread through different cores, but rather being able to share the computational load among different cores with an architecture that allows changes at runtime. Read Article
pynta_screenshot_linkedin.png Dec. 14, 2018 PyNTA: Nanoparticle Tracking Analysis PyNTA is a program that aims at bridging the gap between data acquisition and analysis for experiments of nanoparticle tracking. PyNTA is my first public release of a package on PyPI, the Python repository. It is a desktop application that can be used to record images from a camera, track nanoparticles and build histograms of the distribution of sizes. It is still in beta, but the basic functionality is there. Read Article
yeo-khee-793533-unsplash_linkedin.jpg Aug. 27, 2018 What are Hashable Objects To understand hashable objects in Python, it is important to review what a hash table is. Following the article on Wikipedia, a hash table is a data structure that can map keys to values and that implements a hash function to compute the index to an array of buckets or slots. Heavy words, I know. Read Article
yeo-khee-793533-unsplash_linkedin.jpg Aug. 27, 2018 Step by Step Guide to Building a GUI In this tutorial, we are going to build a Graphical User Interface (GUI) to acquire images from your webcam. We are going to use OpenCV to quickly acquire an image from your camera and PyQt5 to build the user interface. You may find a lot of tutorials online on how to use Python for different tasks, but it is very hard to find a complete guide on how to build a desktop application using Python. Read Article
michal-pechardo-502908-unsplash_linkedin.jpg Aug. 24, 2018 Mutable or Immutable Tuples Broadly speaking, Python variables belong to one of two types: mutable and immutable. We have discussed this yesterday, in the Introduction To Mutable and Immutable Data Types. The first one refers to those elements that can be changed without the need of creating a new one, while the latter refers to those that cannot be changed after instantiation. A paradigmatic example of immutable objects is tuples. However, as we are going to see in this article, tuples may seem to change. Read Article
dan-gold-382057-unsplash_linkedin.jpg Aug. 24, 2018 Mutable and Immutable Attributes of Classes We have seen how to leverage the differences between mutable and immutable objects and what happens when you use mutable types as default function arguments. However, we haven't discussed what happens when you use mutable types as default attributes of classes. Read Article
rawpixel-274862-unsplash_linkedin.jpg Aug. 23, 2018 Mutable and Immutable Objects People who start programming in Python quickly stumble upon the existence of lists and tuples. They are defined in a similar way, they look the same. Sometimes they are even used interchangeably. The obvious question is, therefore, why do you have two different types of elements for the same goal? The answer lays in understanding the differences between mutable and immutable data types in Python. Read Article

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.