Skip to content

Repository files navigation

python-projects

Collection of Simple Python Utility Scripts, including pytest unit tests.

Simple Jinja Template merge

Useful references:

NOTE: Jinja DOES NOT support Python f-string formatting

Files:

PS1> python .\jinja-cli.py -t .\examples\interfaces.txt -p .\examples\interfaces.jsonPS1> python .\jinja-cli.py -t .\examples\interfaces.txt -p .\examples\interfaces.yamlinterface Ethernet1 description leaf01-eth51 ip address 10.50.0.0/31interface Ethernet2 description leaf02-eth51 ip address 10.50.0.2/31
PS1> python .\jinja-cli.py -t .\examples\family-array.txt -p .\examples\flintstones.jsonPS1> python .\jinja-cli.py -t .\examples\family-array.txt -p .\examples\flintstones.yaml### RAW ###FamilyName: Flintstones Fred: 30 years old; Wilma: 25 years old; Pebbles: 1 years old; Dino: 5 years old;### FORMAT ###FamilyName: FLINTSTONES Fred: 30 years old; Wilma: 25 years old; Pebbles: 01 years old; Dino: 05 years old;
PS1> python .\jinja-cli.py -t .\examples\family-hash.txt -p .\examples\rubbles.jsonPS1> python .\jinja-cli.py -t .\examples\family-hash.txt -p .\examples\rubbles.yaml### RAW ###FamilyName: rubbles barney: 29 years old; betty: 26 years old; bamm-bamm: 1 years old; hoppy: 2 years old;### FORMAT ###FamilyName: RUBBLES Barney: 29 years old; Betty: 26 years old; Bamm-bamm: 01 years old; Hoppy: 02 years old;

Simple ArgParse example programs

Written to demonstrate how to use argparse to read from a positional file parameter or standard-in.

simple-cli.py - simple command line example for reading/writing files

PS1> python .\simple-cli.py .\examples\flintstones.json001: {002: "family":"flintstone",003: "members":004: [005: {"Name":"Fred", "Age":30},006: {"Name":"Wilma", "Age":25},007: {"Name":"Pebbles", "Age":1},008: {"Name":"Dino", "Age":5}009: ]010: }

kitten.py - a simplistic UNIX cat example

PS1> python .\kitten.py -n -f 3 -l 9 .\examples\flintstones.json003: "members":004: [005: {"Name":"Fred", "Age":30},006: {"Name":"Wilma", "Age":25},007: {"Name":"Pebbles", "Age":1},008: {"Name":"Dino", "Age":5}009: ]

Note: Add #!/usr/bin/python3 or #!/usr/bin/env python3 to the first line of the file to run directly from UNIX command-line.

UNIX epoch example

Utility for displaying a UNIX epoch in UTC or local time-zone.

  • unix-epoch.py - example for displaying a UNIX epoch in UTC or local time-zone
PS1> python .\unix_epoch.py # 1734877954PS1> python .\unix_epoch.py -i -e 1734877954 # 2024-12-22T15:32:34+0000PS1> python .\unix_epoch.py -l -e 1734877954 # 2024-12-22 16:32:34 W. Europe Standard Time+0100

Base64 binary file encoding and decoding

Restricted to common image and video file types, but could easily be extended to support other binary file types.

  • image-to-json.py - reads and encodes binary file, wrapping its content in a JSON file
  • json-to-image.py - extracts and decodes the binary data and writes it to a file
PS1> python .\image-to-json.py .\examples\python-logo.png .\examples\python-logo.json

JSON file content

 {
"type": "png",
"data": "<base64-encoded-content>",
"epoch": 1735030251,
"created": "2024-12-24T09:50:51+0000"
}

Kitten

A simplistic version on UNIX cat command, kitten.py

PS1> python .\kitten.py --help usage: kitten.py [-h] [-n] [-f FIRST] [-l LAST] [-r] [-v] [filename]Simple version of UNIX cat applicationpositional arguments: filenameoptions: -h, --help show this help message and exit -n, --number display line numbers -f, --first FIRST first line to display -l, --last LAST last line to display -r, --reverse reverse contents -v, --verbose

Examples

PS1> python .\kitten.py -n .\examples\fruits.xml 001: <?xml version="1.0" encoding="UTF-8"?>002: <fruits>003: <fruit><name>apple</name><color>green</color><price>1.20</price></fruit>004: <fruit><name>banana</name><color>yellow</color><price>0.5</price></fruit>005: <fruit><name>kiwi</name><color>green</color><price>1.25</price></fruit>006: </fruits>007:PS1> python .\kitten.py -rn .\examples\fruits.xml001: 002: </fruits>003: <fruit><name>kiwi</name><color>green</color><price>1.25</price></fruit>004: <fruit><name>banana</name><color>yellow</color><price>0.5</price></fruit>005: <fruit><name>apple</name><color>green</color><price>1.20</price></fruit>006: <fruits>007: <?xml version="1.0" encoding="UTF-8"?>PS1> python .\kitten.py -f 2 -l 6 .\examples\fruits.xml<fruits> <fruit><name>apple</name><color>green</color><price>1.20</price></fruit> <fruit><name>banana</name><color>yellow</color><price>0.5</price></fruit> <fruit><name>kiwi</name><color>green</color><price>1.25</price></fruit></fruits>

UTF-8 Issues

When interacting with the operating system, particularly on Windows which is configured to use code-page such as cp1252, rather than UTF-8 can cause mojibake errors.

A good explanation is given in Mastering Python's UTF-8 Mode: Pitfalls and Practical Workarounds

The simple fix is to ensure that the Python interpreter is forcing UTF-8

PS1> python -X utf8 .\kitten.py -v .\examples\european-words.txt # WORKS correctlyPS1> python .\kitten.py -v .\examples\european-words.txt # FAILS "mojibake" errors

The read-utf8-text.py example shows an alternative solution by forcing UTF-8 encoding in argparse.ArgumentParser, but issues remain with pytest and Python subprocess module

PS1> python .\read-utf8-text.py -rv .\examples\european-words.txtPS1> python .\read-utf8-text.py -n .\examples\european-words.txtPS1> python .\read-utf8-text.py -v .\examples\european-words.txt

Python Objects

Python objects do not support encapsulation or static type checking unlike many object-oriented programming languages.

  • It is possible to indicate that data is not intended to be modified by prefixing a variable with
    • _ (underscore) or
    • __ (double underscore).
  • A constant value is indicated making it UPPERCASE and to show it should not be modified can be prefixed with
    • _ (underscore) or
    • __ (double underscore)
  • By convention getter and setter methods are discouraged.

It is recommended that mypy is used to check for encapsulation violations and static typing because the Python language does not enforce it.

The following examples attempt to cover the most common approaches:

Running the pytest tests

PS1> pip install pytestPS1> pytest .\tests\test_person_simple.py -vPS1> pytest .\tests\test_person_attributes.py -vPS1> pytest .\tests\test_person_decorators.py -vPS1> pytest .\tests\test_person_encapsulation.py -v

Checking for encapsulation violations and static typing errors

PS1> pip install mypyPS1> mypy .\Person_Simple.py PS1> mypy .\Person_Attributes.py PS1> mypy .\Person_Decorators.py PS1> mypy .\Person_Encapsulation.py 

Useful resources

Rhythmbox.xml file parser (unfinished)

  • rhythmbox.py - (unfinished) rhythmbox.xml file parser

Pytest Testing

A series of pytest based test programs are being developed and will be stored in the tests sub-folder.

In order for tests to work the project folder needs to be in the Python search path, and the tests folder needs to be a package, that is, it must have an __init.py__ for it to work directly from the command line.

About

Collection of Simple Python Utility Scripts

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages