Have Python log to syslog
This article documents how I set up Python and the syslog daemon so that Python programs can log to syslogd.
This article documents how I set up Python and the syslog daemon so that Python programs can log to syslogd.
When you are exploring a problem, in general first write a command-line program whenever possible.
It will take less effort to write then a full-blown GUI.
Recently I wrote a program to remove the protection from ms-excel files.
The original version was written as a command-line program. Later I re-used the relevant code for a GUI program for use on ms-windows. This was mainly for the benefit of some colleagues who are not comfortable with using the command-line.
In this article I want to contrast the two programs.
There are basically two ways in which one can make use of a modern CPU with multiple cores for computationally intensive work.
In the first case, all data is implicitly shared. In the second case, data must be explicitly shared or communicated.
The first option is often said to be more convenient. I would like to make the case that this usually makes the task more difficult, because of the need to manage all shared data.
Sometimes I need to use some binary data (e.g. a bitmap image) in a Python
script.
In a package, you can store this data portable in a data
subdirectory of
a modules using the package_data
argument of setuptools.setup
.
This won’t work in a standalone script.
You can of course make your script into a package, but here I want to show an
alternative solution.
The ImageMagick suite has been in my software toolbox for years. It is my go-to tool for manipulating bitmap images. Over the years I have written several front-ends for specific tasks for in Python.
In general, I have used the subprocess module to launch convert
or
mogrify
from Python.
With the release of Wand 0.5.0 which supports ImageMagick 7, I decided to try
that by porting one of my scripts (foto4lb) to it.
This turned out to be slower than using convert
directly.
But now it is 2021. Py-wand is at 0.6.7 and it is time to try again.
After some initial experiments with sqlite as a replacement for a json data file format (which I did not pursue in the end), I have now started using sqlite in earnest. And I must say that I quite like it.
This document explores how an SQLite database could be used to store data for the resin-calculator. Normally, the recipes are a dictionary keyed to the name of the resin. The value of each recipe is a list of (component, parts-by-weight) tuples. Since a database only contains tables, where each row has the same form, how do we store such infomation in a database?
As an engineer I do a lot of calculations. These can be done with pen and paper and a calculator, in an IPython notebook or in a throwaway spreadsheet. All of these methods have shortcomings, though.
Pen and paper is hard to share and (in my case) hard for others to read. In IPython you can assign the results of calculations to a variable, but you have to perform a separate action to display them. And spreadsheets in general show you the results but not the calculations.
So I wrote a simple function in Python to help me with that. Using this function I can print both simple assignments and relatively complex calculations. And it shows both the calculation and the result.
With leading white space governing the indentation level and so the grouping of statements, Python code already looks relatively clean.
Yet there are additional tools that will help you improve your code.
Sometimes I miss the C’s plain old struct
in Python.
Of course Python has dictionaries, but I prefer to write a.b
over a['b']
.
Here are several ways of doing something akin to a struct
in Python.