Skip to content

wilsonmar/python-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

layout lastchange url
post
25-11-27 v060 + planets-turtle.py :README.md

This README describes practical examples of production-quality Python-language code which exhibit the security and scalability features needed in a (hostile) production environment where debugging needs to occur quickly.

Programs here make use of uv (from astral.sh) instead of pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, etc. To catch dependency issues, we recommend that the latest set of dependencies is assembled daily by creating requirements.txt, pyprojects.toml, and uv.lock with every run.

We also recommend that libraries download historical versions of each library downloaded so that in case of issues, forensics and fall-back can be performed even though public versions become corrupted.

Featured Projects

Code which we think have the most practical usefulness:

  • dundars-list.py lists programs by date, along with the last_change and status text in each python program.

  • git-commit.sh is copied to this repo's .git/hooks folder to run

    • ruff is catch code formatting issues
    • >bandit to identify secure coding issues
    • GitGuardian, GitLeaks, TruffleHog, Legit, etc. to identify what looks like secrets stored in the repo.

      Note that safety is needed to identify libraries (versions) marked as vulnerable in CVS, it is no longer run because the vendor now requires login. That makes it too inconvenient, especially offline.
  • diagrams-graphwiz.py generate diagrams (image files) from text, based on the graphwiz tool library.

  • planets-turtle.py illustrates a simple 2D map of planets around our sun, using the turtle library and Python object-oriented programming.

  • python-samples.py is a conglomeration of many features.

  • retry-flask.py is a Flask server app that purposely returns errors to client responses from

  • retry-client.py using the tenacity library to handle errors (500 server err, Timelout, 404, 204 rate limit, 204 empty response).

  • gpu-sample.py recognizes what GPU is available (CUDA or Apple MPS) and runs sample PyTorch microbenchmarks of timings and memory used. Enhancements:

  • About Google's Tensorflow: ipynb based on the VIDEO: Awaiting the 2025 4th edition to October 2022 O'Reilly book: Hands-on Machine Learning with Scikit-Learn, Keras and TensorFlow (3rd edition) by AurĂ©lien Geron (YouTube, interview, X)

  • print-inkjet.py keeps print heads clear by sending a pdf containing colored text and images. Features and enhancements:

    • Quotes scraped from the internet to print.

  • ls_al.py lists files in a directory like the Linux command ls -al.

  • mondrian-gen.py to generate a png art in the style of Mondrian. References macOS Keychain to keep OpenAI API key.

  • saytime.py to use the macos say CLI command to voice the time or other text.

  • sorting.py to run different sorting algorithms to compare performance as n rises, on a matplotlib visualization.

  • dash-streaming.py to use Plotly Dash library to animate a stream of real-time updates (random values) within a Flask app.

  • sqlite-sample.py to create and maintain a SQLite database.

In the weather folder:

  • openweather.py to obtain from API calls and format weather data (as fuzzy tokens). Features

    • Lookup Longitude/Latitude by zip code
    • wttr-weather.py issues curl commands for weather data (https://github.com/chubin/wttr.in)
    • Display on a monitor information about the location of people you communicate with
    • Use BeautifulSoup to scrape predictions from google.com (which references Weather.com).
    • Compare actuals vs. predictions of each prediction service.
    • Compare accuracy of different predictors (weather.com vs. wattr.in vs. Openweather. etc.)

  • sunset-speed.py calculates how fast one would need to drive to experience a continuous sunset. This theoretical question is designed to make math fun and make for curiosity about programming math. This shows a use for the cosign trigonometry function.

  • youtube-download.py downloads videos based on its URL from a list of URLs in a CSV file.

In the recommender folder:

  • it-media.py is a popular way to show off skill at database manipulation, analytics, and machine learning. Related enhancements:

    • Bulk load into database movie info (such as "m1-100k")

    • Reconcile data about the same movie from various sources.

    • Sorting by several factors: genre, content rating, year, actors, directors, academy awards

    • Link to rottentomatoes.com and other reviews

    • Link availability of movie on various streaming platforms (Netflix, Prime, etc.)

    • Enable entry of user ratings and use that data to influence recommendations.

    • Link release dates to calendar

    • Catalog DVD collection by scanning UPC labels.

    • Play a particular video in a media app after scanning the UPC on the video box.

    • surprise.py provides a GUI created (using tkinter) to select movies and TV shows as an example of machine learning matrix eigenvector SVD (Singular Value Decomposition) & PCA (Principal Component Analysis) feature extraction algorithms from data science.

    • Recommend movies based on what friends watched.

  • pdf2llm.py converts PDF files to ".md" (markdown) format files, which LLMs can read (and are smaller files). There are two algorithms (libraries). pymupdf4llm converts to "###" headings and "**" bold.

Other Programs in this repo (alphabetically):

  • argparse-samples.py to specify command line arguments into this program.

  • bookmarks_export.py to export bookmarks from Chrome to a HTML file.

  • calculator-tk.py is a calculator created using the Tk GUI library.

  • dijkstras.py to compare calculations of shortest path on a graph

  • dijkstra-yt.py to calculate shortest path using heapq library

  • endecode-protobuf.py encodes/decodes a comma-separated list so each word in front of a # prefix has a char count, like Protobuf does for gRPC.

  • fido2-titan.py to use the FIDO2/WebAuthn protocol to read FIDO2-compliant OTP+FIDO+CCID keys

  • log-time-csv.py creates the path to a CSV file based on static naming standards to write a sequential time stamp to a CSV file. Has limits to an infinite loop.

  • logging.py to use the logging module to log messages to a file.

  • ml-metrics.py to use PyTorch & Scikit to generate metrics for Machine Learning.

  • num2words.py simply converts a number input to its word.

  • otel-flask.py - a simple Flask app with automatic OpenTelemetry instrumentation

  • pengram2nato.py to use the NATO phonetic alphabet spell out a sentence.

  • perf-ns.py obtains timings in nanosecond-level resolution.

  • plotting.py to create common visualizations using matplotlib.

  • pytorch-mnist.py to use PyTorch to build, train and evaluate a neural network to recognize a hand written digit MNIST

  • random-niche.py to generate a 19-digit cryptographically secure random number.

  • recursive-cache.py shows faster Fibonnici recursion calls when using functools cache.

  • rolldice.py rolls a 6-sided die used in Yahtzee, rolled repeated until "quit".

  • rot13.py is used on UseNet to encode sentences using a cypher that's 13 characters away.

  • roku-set.py opens Roku at a given IP address to the YouTube video specified.

  • roman2int.py converts Roman numerals to base 10 numbers.

  • sklearn-sample.py calculates

  • try-accept.py to show usage of try/except/else/finally exceptions

  • unittest_calculator.py to use Python's Unit Test feature.

  • variables.py to experiment with defining and viewing objects of various data types.

Other coding samples

  • bomonike/memon to generate a strong passphrase based on random words in English and German.
  • bomonike/google/gcp-services has authentication and other functions to access services within the Google Cloud Platform (GCP)
  • bomonike/google/myutils.py contains utility functions useful for calling by other programs.

In the countries folder:

  • country_info.csv
  • country_lang_info.xlsx
  • Comparison of countries: csv from a year of the World Happiness Report and statistics from the CIA Factbook.
  • Future program to use GeoAI:
    • Rasterio works with raster data such as satellite images and elevation models
    • GeoPandas extends GeoPandas
    • Folium creates interactive maps
    • Prestereo, Shapeley)

Experiments being built

  • Multi-threading

  • Use OpenCV to recognize hand signals (Sign Language), then take an automatic action or raise an alarm when touching your face (bad hygiene).

In the predictions folder is an example

NOTE: Other pograms are in the https://github.com/bomonike organization:

Cloud AI comparisons

In programs that make use of Cloud AI LLMs, (such as listen4cmd.py and mondrian.py) capture timings and costs for each request. Here is a sample table for recognizing the word "docker":

LLM Output MiliSecs. Cost USD
Anthropic 91% 33 $0.00420
AWS 89% 28 $0.00680
Azure 78% 39 $0.00431
DeepSeek 63% 43 $0.00532
Google 95% - -
IBM 87% 29 $0.00267
Mistral 77%r 43 $0.00532
Cohere 82% 13 $0.00255
OpenAI 88% 23 $0.00490
Qwen 3 72% 63 $0.00230
Sphinx 55% - -
Wit.ai 63% 61 $0.00320

== above means the word was recognized correctly.
- above means the word was NOT recognized.

  • Google is free but is limited to 50 requests per day.
  • Azure
  • Mistral is based in France.
  • Qwen tests used by Alibaba in China with servers in Singapore.
  • Sphinx is the only local LLM, from CMU. But it didn't recognize many words correctly.

    Cost USD is calculated from tokens charged times price.

Another table contains sums from all runs, from which analytics charts are drawn.

Code sections sequence

The features in each code section:

  • SECTION 01: Preparations before running this program: Bash CLI commands to load external libraries (such as uv add matplotlib ...

    last_commit = "v007 + ruff checks & fixes :print-inkjet.py"

  • SECTION 02: Load modules used by this program: External libraries are imported within a try/except structure so instructions are given on fail.

  • SECTION 03: Logging: Capture pgm start date/time (as global variables)

  • SECTION 04: Define utilities to print messages:

  • SECTION 05: Globals: Define hard-coded Global variables as parm defaults:

  • SECTION 06: Show args_prompt() menu and read params specified on program command line: -v SHOW_VERBOSE

  • SECTION 06-2: Set display and exit

  • SECTION 06-3: Read parameters as command arguments:

SECTION 06-4: Show start time & pgm values:

  • SECTION 06-5: Edit parameter values from command arguments:

  • SECTION 07: Obtain variables from .env file and secrets vault (in cloud): MY_USERNAME, MY_LOCATION, MY_PRINTER, MY_REGION, etc.

  • SECTION 08: Validate values from all sources above:

    • File or database specified of correct format and exists?
  • SECTION 09: Login:

  • SECTION 10: Define utilities to read and delete files and folders: (folder_, file_, csv_, pdf, png, sqldb_, docdb_, graphdb_, rag_, etc. => _check, _create, _search, _seqread, _delete, _update, _audit, _graph, etc.)

  • SECTION 11: Define utilities to list and print to printers:

    • CHECK_FOR_FILE_IN_PATH if an existing .pdf file exists in path to print.
  • SECTION 12: Create custom outputs:

    • Create a pdf containing colors printed (if requested by CREATE_COLOR_BLOCKS)
  • SECTION 13: Audit outputs and send alerts

  • SECTION 14: Clean up: Delete (remove) .pdf file created by this program if REMOVE_PDF_FILE_CREATED = True

  • SECTION 15: pgm_stats() to show how long the program ran and how many items were processed.

  • SECTION 16: Main calls to functions defined above:

Coding Practices

Below are notes about coding practices, from the top down:

  1. At the very top of the file:

    #!/usr/bin/env python3

    That enables you to run the program as a script without typing python3:

    ./mondrian-gen.py

    instead of

    python3 mondrian-gen.py
  2. This specifies that emjois and non-English characters (such as Japanese, Chinese, etc.) may be in the code file:

    # -*- coding: utf-8 -*-
  3. SPDX (Software Package Data Exchange) license identifiers provide machine-readable tags to communicate the license governing a piece of software.

    # SPDX-License-Identifier: MPL-2.0

    The MPL-2.0 (Mozilla Public License version 2.0) is a moderately permissive open-source license with some copyleft provisions. Its key characteristics include:

    • Allows code to be freely used, modified, and shared
    • Requires modifications to MPL-licensed files to be shared under the same license
    • Allows MPL-licensed code to be combined with code under different licenses
    • Provides patent protection for contributors and users
    • Requires preservation of copyright notices
  4. Dunder (double underscore) variables in the module define metadata about the program. PEP specifies that such variables can be read by other programs without opening the source code.

    __commit_date__ = "2025-05-20"
    __version__ = "1.0.0"
    __author__ = "Your Name"
    __license__ = "MIT"
    __commit_date__ = "2025-05-20"
    __status__ = "Production"
    __doc__ = "This module demonstrates the use of dunder (double underscore) variables in Python."

    TODO: Some also have like commit_hash = "a1b2c3d4e5f6"

    But How to get the commit hash? Doesn't adding it change the hash value?

  5. To capture the program start time at the earliest moment, the built-in time module is used:

    import time
    std_strt_timestamp = time.monotonic()

    time.monotonic() returns the time since an arbitrary point in the past, which is not affected by system clock changes.

  6. External dependencies are specified in a requirements.txt file.

    import requests
  7. Within the requirements.txt file, the myutils module is loaded to provide all other custom Python programs in this repo:

import myutils
  1. Modules used The ast (Abstract Syntax Tree) module parses Python source code into a structure so code can be better analyzed or modified programmatically.
import ast

ast is used by linters, type checkers, and other such tools to analyze code without running it.

  1. All functions defined in this file are prefixed with myutil_ to avoid name conflicts in other modules.

    def myutil_print(msg):
        print(msg)
  2. The function issuing a message to the console is printed along with the message:

    def myutil_print(msg):
        print(msg)

Ones I would like to create

  • Reset all my passwords automatically. This would involve my database in KeePassXC and 1Password, then programmatic control operating Authy and emails to confirm.

  • Functions to reference a physical Yubikey containing secrets and cryptographic certificates.

The STATUS of each program is noted within each file.

Before running each:

chmod +x dijkstras.py
./dijkstras.py

etc.

https://codehs.com/tutorial/david/sample-a-csp-performance-task-1 Sample A CSP Performance Task

https://codehs.com/tutorial/david/sample-b-csp-performance-task Sample B CSP Performance Task - Caesar Cipher

https://www.youtube.com/watch?v=LXsdt6RMNfY Use Python to automate my life

https://www.youtube.com/watch?v=mCk4Rabkmjc Automate Boring Office Tasks with ChatGPT and Python

https://www.youtube.com/watch?v=Ge-9AhVVOFc Automate any task using ChatGPT! (my full GPT building framework)

Run a Bash script

import subprocess result = subprocess.run(["bash", "path/to/script.sh"], capture_output=True, text=True) stdout = result.stdout stderr = result.stderr

https://github.com/Gatsby-Lee/DevOps/tree/master/programming_lang/python

https://www.youtube.com/watch?v=kGcUtckifXc Please Master These 10 Python Functions…

Have a single import per line to reduce merge conflicts. See https://github.com/asottile/reorder-python-imports?tab=readme-ov-file#why-this-style

https://www.debuggingbook.org/

https://www.programmingworld.tech/blog/django-framework-build-url-shortener-application-using-django-and-pyshorteners Django Framework : Build URL Shortener Application Using Django and Pyshorteners

About

Sample Python code with security and enterprise features, all in one repo for easy adoption.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •