Blog

All the articles, tutorials, and discussions written at Python for the Lab. Learn from real-world use cases, and join the discussion to deepen your knowledge and understanding. We focus on patterns and topics that are useful for researchers working in a lab. From automation to data storage. If you don't find what you are looking for, drop us a line!.

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.
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
The With command and custom classes 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
Using pyZMQ for inter-process communication part 2 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 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
Step by Step Guide to Building a GUI 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
What are hashable objects 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
Mutable and immutable attributes of classes 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
Mutable and immutable tuples 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
Mutable and Immutable Objects Aug. 23, 2018 Mutable and Immutable Objects Mutable objects can be changed without recreating them. This opens the door to very interesting patterns, but they can also give issues if one is not aware. We explore what mutable and immutable objects are and explore how to use their properties. Read Article
Storing data with SQLite Aug. 12, 2018 Storing Data with SQLite SQLite is the best approach to learn about databases. They require minimal setup, and the entire database is a single file. In this tutorial we explore how to store data using a database. Read Article
Storing binary data and serializing Aug. 11, 2018 Storing Binary Data and Serializing Last week we have seen how to store data into plain text files that can be read by any editor or by other programs. We have also seen that if you separate your data with commas your file will follow a standard and it will be automatically compatible with other applications, such as spreadsheets. One of the main limitations of this strategy is that if the data itself contains a comma, your file will not be readable anymore. Read Article
Introduction to Storing Data in Files Aug. 10, 2018 Introduction to Storing Data in Files Storing data to reuse it later is a central part in most Python applications. Whether you are doing a measurement in the lab or developing a web application, you will need to save information in a persistent way. For example, you would like to analyze your results after you have performed an experiment. Or you would like to keep a list of e-mails of people who registered on your website. Read Article
Learning (not) to handle exceptions June 4, 2018 Learning (not) to Handle Exceptions When you develop code, it is almost impossible not to run into an error. Some problems are going to arise as soon as you start your program, for example, if you forgot to close a parenthesis, or forgot the : after an if-statement. However, errors at runtime are also very frequent and harder to deal with. In this article, you are going to learn how to handle exceptions, i.e. how to avoid program crashes when you can anticipate that an error may appear. Read Article
Documenting with Sphinx and Readthedocs May 31, 2018 Documenting with Sphinx and Readthedocs If you have ever followed a guide on how to start programming, most likely you have encounter reflections about the importance of adding comments to your code. Comments allow you to understand what the developer was thinking when programming, maybe left some traces of what could be improved. If you are developing software in places where people change often, it is crucial to leave extra information behind, to speed up the catching up of the new developers. Read Article
Implementing threads for measurements May 29, 2018 Implementing Threads for Measurements Probably you have run into the problem of wanting to update a plot while acquiring a signal, but finding that Python is busy during the acquisition. This happens, for example, when functions or methods take long to execute and you can't regain control until it is done. Python has at least two different ways of solving this issue, one is the threading module and the other is the multiprocessing module. They look the same but are fundamentally different, and therefore you need to understand their differences in order to decide when to use one or the other. Read Article
A primer on classes in Python May 22, 2018 A Primer on Classes in Python Python is an object-oriented programming (OOP) language. Object-oriented programming is a programming design that allows developers not only to define the type of data of a variable but also the operations that can act on that data. For example, a variable can be of type integer, float, string, etc. We know that we can multiply an integer to another, or divide a float by another, but that we cannot add an integer to a string. Objects allow programmers to define operations both between different objects as with themselves. For example, we can define an object person, add a birthday and have a function that returns the person's age. Read Article
How to use decorators part 2 May 18, 2018 How to use decorators Part 2 Decorators are a very useful programming pattern that allows changing the behavior of functions with little refactoring. Decorators allow developers to abstract common options from functions, but mastering their use in Python can be challenging. In this article, we are going to go in depth regarding different options when implementing decorators. The topics covered are: Read Article

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.