Recently, I have stumbled upon an apparently strange situation in which a non-empty Python package was empty after it got imported. Let’s take a close look at that issue because it may happen to anyone.
Read More
Tag Archives: Python
Even a Feature That You Do Not Use Can Bite You
Let’s have a look at a simple piece of Python code that I have both accidentally written and seen in code reviews which does an entirely different thing than expected.
On Incomplete HTTP Reads and the Requests Library In Python
The requests library is arguably the mostly widely used HTTP library for Python. However, what I believe most of its users are not aware of is that its current stable version happily accepts responses whose length is less than what is given in the Content-Length header. If you are not careful enough to check this by yourself, you may end up using corrupted data without even noticing. I have witnessed this first-hand, which is the reason for the present blog post. Let’s see why the current requests version does not do this checking (spoiler: it is a feature, not a bug) and how to check this manually in your scripts.
Read More
Implementing multiprocessing.pool.ThreadPool from Python in Rust
In this post, we will implement multiprocessing.pool.ThreadPool
from Python in Rust. It represents a thread-oriented version of multiprocessing.Pool
, which offers a convenient means of parallelizing the execution of a function across multiple input values by distributing the input data across processes. We will use an existing thread-pool implementation and focus on adjusting its interface to match that of multiprocessing.pool.ThreadPool
.
Read More
Consuming and Publishing Celery Tasks in C++ via AMQP
Celery is an asynchronous task queue based on distributed message passing. It is written in Python, but the protocol can be implemented in any language. However, there is currently no C++ client that is able to publish (send) and consume (receive) tasks. This is needed when your project is written in a combination of Python and C++, and you would like to process tasks in both of these languages. In the present post, I describe a way of interoperating between Python and C++ workers via the AMQP back-end (RabbitMQ).
Read More
A New Notification Plugin For WeeChat
In this post, I would like to present weechat-notify-send, a new WeeChat script that I wrote. It sends highlight and private-message notifications through notify-send.
Read More
Indexing Lists in Python With an Integer or Object’s Name
In this post, we will see a way of implementing a list in Python that can be indexed with an integer or string representing the name
attribute of a stored object.
Read More
Unit-Testing With unittest.mock.patch()
Sometimes, you need to unit-test functions that call functions from the standard library that rely on side effects. In this post, I show a way of doing so in Python with unittest.mock.patch()
. More specifically, we implement two context managers that use os.chdir()
to perform actions in the given directory, and show a way of unit-testing them without relying on the file system.
Read More
Restarting a Python Script Within Itself
Sometimes, you may wish to check within a script when a configuration file or the script itself changes, and if so, then automatically restart the script. In this post, you will see a way of doing this in Python.
Read More