In my Basic Mocking with Pytest post I showed simple cases. Now let’s do more mocking. First I’ll create the environment for my code: $ mkdir more-mocking$ cd more-mocking$ python3 -m venv venv$ . venv/bin/activate(venv)$ pip install -U pip wheel…Successfully installed pip-24.0 wheel-0.43.0(venv)$ pip install pynetbox pytest…Successfully installed certifi-2024.2.2 charset-normalizer-3.3.2 idna-3.7 iniconfig-2.0.0 packaging-23.2 pluggy-1.5.0 pynetbox-7.3.3 […]
Category: Programming
NetBox Plugins to Present External Data
I have worked with NetBox for over five years now, and NetBox plugins feature has existed since version 2.8 in April 2020, but it was only recently that I got a need to start leveraging plugins to extend the NetBox functionalities. Most of the publicly available plugins work with Django model-based data that is saved […]
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 […]
Compressed Data Between PowerShell and Python
I have built an application that transfers JSON data between remote PowerShell and Python components using Amazon SQS (Simple Queue Service). The size of the data is usually quite small, fitting nicely into the 256 KiB message size limit of SQS, letting me avoid complex multi-message handling or temporary S3 objects. In specific cases, however, […]
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 […]
Parametrizing Test Functions with Pytest
I will first create a class that is used for representing the Zabbix protocol flags (zabbix_flags.py): Subclassing enum.IntFlag is very suitable for this purpose, and enum.auto() can be used to represent the flag values. I have four test functions to check that the flag values work as expected (test_zabbix_flags.py): Let’s create a virtual environment and […]
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: >>> […]
Basics of Zabbix API with Python and PyZabbix
Zabbix API is the way to go when you need to manage Zabbix configurations or get data from Zabbix programmatically. For Python applications one of the community-based libraries is PyZabbix. Note: There is also a package called py-zabbix. That is a different one, not handled in this post. In this post I’ll show: How to […]