May 25, 2020Python Tip: Using Else with LoopsMost 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
May 18, 2020Python Tip: Ready to Publish Matplotlib FiguresSaving 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
May 16, 2020Data Descriptors: Bringing Attributes to the Next LevelDescriptors 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
April 10, 2020Generators, Iterables, Iterators in Python: When and WhereGenerators, 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
Feb. 4, 2020How to control an Arduino from the computer using PythonArduinos 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
Jan. 20, 2020How to create a setup file for your projectWhen 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
Jan. 2, 2020Website Redesigned and ImprovedTo 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
Oct. 4, 2019Complete Guide to Imports in Python: Absolute, Relative, and MoreImporting 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
Aug. 6, 2019Handling and Sharing Data Between ThreadsWhen 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
Aug. 6, 2019Starting and Synchronizing ThreadsIf 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
June 11, 2019Duck Typing, or how to check variable typesThe 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
March 17, 2019Intro to Python Lambda FunctionsSome 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
March 10, 2019What are args and kwargs and when to use themIf 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
March 5, 2019Using pyZMQ for inter-process communication: Part 2In 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
Feb. 12, 2019Building a CRM with Jupyter NotebooksThis 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