Category: Programming

Datetimes with Timezones in Python

First, let’s show how the commonly-used plain datetime.datetime.now() gets the local time but does not have a timezone: >>> import datetime >>> now = datetime.datetime.now() >>> type(now) <class ‘datetime.datetime’> >>> now datetime.datetime(2024, 2, 17, 12, 47, 40, 34210) >>> print(now) 2024-02-17 12:47:40.034210 >>> now.tzinfo is None True >>> Let’s add timezone to it, using the […]

Network Interface Name Sorting in Python

Evergreen problem in sorting strings: How do you implement human-expected ordering when the strings have varying-length numbers in them? Prime example in the networking world is network interface naming in various devices. Example: >>> interfaces = [“1/1/1”, “1/1/2”, “1/1/10”, “1/1/11”, “1/1/10.32”, “1/1/10.311”, “1/1/10.312”, “eth-1/1/1.1”, “eth-1/1/1.2”, “eth-1/1/1.10”]>>> print(“\n”.join(sorted(interfaces)))1/1/11/1/101/1/10.3111/1/10.3121/1/10.321/1/111/1/2eth-1/1/1.1eth-1/1/1.10eth-1/1/1.2>>> That’s not ideal as the default string sorting […]

Python Versions

Even though I’ve used Python programming language on-and-off since about year 2000, I only started paying more attention to specific Python versions around 2016 when I gradually started using Python more seriously in devops. When dealing with different runtime environments it is crucial to know which features are available in which Python versions. This page […]

Count Lines of Python Code

I have created a new application called lopc (Lines of Python Code). It is available for install with pip on PyPI (https://pypi.org/project/lopc/), and the source code is available on GitHub: https://github.com/markkuleinio/lopc The app really only counts the number of non-empty and non-comment lines of *.py files in the given directories. These are some features of […]

Python f-String Formatting Codes

Here is my short list about the common f-string formatting codes in Python: >>> score = 123.728 >>> f”{score}” ‘123.728’ Minimum field length: >>> f”{score:>10}” ‘ 123.728’ >>> f”{score:<10}” ‘123.728 ‘ >>> f”{score:^10}” ‘ 123.728 ‘ Number of decimals: >>> f”{score:.2f}” ‘123.73’ >>> f”{score:10.2f}” ‘ 123.73’ >>> f”{score:<10.2f}” ‘123.73 ‘ Padded with a character: >>> […]

Configuring Logging in NetBox

Creating the log file and setting the permissions: $ sudo mkdir /var/log/netbox $ sudo touch /var/log/netbox/netbox.log $ sudo chown -R netbox.netbox /var/log/netbox $ Configuring NetBox (in /opt/netbox/netbox/netbox/configuration.py): You can set the level for the loggers as needed, in this setup the file handler accepts DEBUG and higher but the actual loggers (“django” and “netbox”) only […]

Basic Mocking with Pytest

Since I always seem to search for these examples over and over again, here it is, basic mocking with pytest. My example code is this (testmodule.py): It can be run: $ python3 testmodule.py In testfunc, sleeping $ Not surprisingly, running it takes about five seconds. The time.sleep() call simulates some long-running operation in the code. […]