Skip to content

language

#313: Persisted Cache for Function Calls

It is already 2 years since I wrote about built-in functools to cache calls to your Python functions. While this is still a great approach, it has one downside: when we restart our tool, the cache is empty and we need to do the computational work once more.

When it comes to persisting our cache, we have multiple options. A simple little tool that pops up in a lot of post I read in the last weeks is DiskCache. The last update of this library is from August 2023, what leads to questions if this library is still maintained. We should keep an eye on that but still give it a try. Let us see how we can use this tool for a persisted cache.

#312: Switch Statement in Python

For a long time, there was no switch/case statement in Python. Instead, we had to use a cascade of if/elif/else to filter for the different cases. But with Python 3.10 that changed and now we can use match / case to save us some typing. Let us see how this works.

#309: Repeating and Combining Lists With itertools

One of the things I like the most of Advent of Code is that it forces us to learn more about the language we use. This year I had to do a lot of combinations of elements in a list and for that we can either write the boring code on our own or use the itertools module that ships with Python. In this post we explore the most useful features of this hidden gem.

#308: Overwrite | to Simulate UNIX Pipes

In our LangChain posts we used this handy way to create a chain of the various parts that we need to interact with an LLM:

chain = prompt | llm

This mimics the pipelines in UNIX/LINUX, where we can chain command line tools together to create powerful operations. But how does this work behind the scenes? Let us find out how we can create such a behaviour on our own.

#294: Callable Classes in Python

In Python, everything is an object - including functions. That flexibility goes both ways: not only can functions behave like objects, but objects can behave like functions. The mechanism that unlocks this duality is the special (or "dunder") method __call__. By defining __call__ in a class, we make it possible to invoke it with parentheses, just like an ordinary function, while still retaining all the advantages of stateful objects.

#290: Record Audio With PyAudio

Last week we used sounddevice to record audio with Python. In this post we do the same thing, but with the PyAudio library. Depending on your environment and sound devices, you may need more flexibility and operating-system specific support than what sounddevice can offer.

#282: Working With Temporary Files

Sometimes we just need a file to store some temporary data and discard it as soon as we are done. In this case, creating permanent files and deleting them manually does not make sense. Fortunately, Python gives us a solution tailored for this situation: the tempfile module. Let us explore how it can help.