Skip to content

Sample Python coding with a lot of security and technical features, all in one repo for easy adoption.

License

Notifications You must be signed in to change notification settings

wilsonmar/python-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

layout lastchange url
post
25-09-16 v022 + diagrams-graphwiz.py :README.md

The objective of this repo is to provide examples of practical use of production-quality Python language.

There are plenty of other sites and repos offering code to play games or demonstrate a coding trick.

Not here.

Here we aim to provide code that exhibit the security and technical features needed in a (hostile) production environment where debugging needs to occur quickly.

Featured Projects

These contain various utilities I wrote which have the most practical usefulness.

Each contains helpful features implemented as identified:

PROTIP: List the last_change of all python (.py) files in this repo by running:

chmod +x dundars-list.py
./dundars-list.py

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.)

  • 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.

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.

Experiments being built

  • 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:

  • https://github.com/bomonike/memon calculate super strong word phrases and remember them via LLM gen'd songs. See https://www.youtube.com/watch?v=KAjkicwrD4I on how to memorize using PAO (Person Action Object)

  • palindromes.py - I personally don't understand the intellectual curiosity for words and phrases that spell the same forward and backward. Examples: TENET, 747, KAYAK, TACO CAT, RACECAR, NEVER ODD OR EVEN, STEP ON NO PETS, UFO TOFU

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/

About

Sample Python coding with a lot of security and technical features, all in one repo for easy adoption.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •