Skip to main content

A compilation of boilerplate scribbles

Project description

Boilerplate library

Pain point: Repetitive coding of the same functionality, which often involves doing the same Google searches. Really a list of recipes for consolidation.

Some workflows:

Run tests:

uvx --isolated --with-editable .[all] pytest

Install library:

pip install -e .

Uninstall library:

python setup.py develop -u

Design rationale

This is partly spurred by my usage of convenience scripts in the context of an experimental physics laboratory, with the following usage observations:

Problem: Scripts are commonly archived for reference, either for reuse or for referencing data collection strategies. Old scripts tend to break from outdated API.

  • Solution: Ensure that the library is strongly back-compatible with old functionality, not just via version-control, but every commit should be back-compatible.
  • Problem: Functions that share the same name are essentially overridden (no signature polymorphism).
  • Problem: Functions being used may become opaque to the user, e.g. when using an IDE which can perform static declaration lookups, dynamic assignment of functionality (especially under a different alias) can lead to difficulties in usage.
  • Anti-pattern: Avoid declaring functions with different names to differentiate minute behavioural changes. This can quickly pollute the library namespace.
  • Solution: Stick to the same function names as much as possible, and clearly define the function contract. Best if a static type checker is used to enforce contractual obligations. Where functions are likely deprecated, throw out one (and only one) warning during initial script invocation.
  • Possible solution: Pre-compile currently used library into current directory, so that library references change from global reference to local reference.
  • Problem: Precompilation and duplication of libraries can lead to bloated software size and excessive duplication in a version-controlled environment. This also makes usage of updated libraries in an old environment difficult.
  • Possible solution: Use of decorators to mark the version of certain functions. This mark ought to be representative of the development order of the library, i.e. older commits to the library should have older marks (perhaps a datetime). Individual scripts should keep track of which functions it should be calling, perhaps as a version pinning comment if no such comment is initially detected. Calling of newer functionality should still be supported, i.e. forward-compatibility.
  • Anti-pattern: Marking of functions by shelving them within subdirectory corresponding to their version number should be avoided. This poses issues when (1) cross-referencing similar but competing implementations, (2) developer overhead from choosing directories.

Problem: Old scripts may have missing nested directory dependencies.

  • Solution: Package all required functionality in a single library. Where not possible, pull the library functions into a version-control and/or a centralized environment.
  • Anti-pattern: Functionality that are frequently updated and cannot be reasonably maintained (e.g. porting of numpy functions is not feasible long-term - will eventually deviate from updated implementations) should be delegated to third-party libraries.
  • Anti-pattern: Avoid deploying of library as a standalone

Problem: Older API may depend on older implementations of third-party libraries.

  • Anti-pattern: Developer could document which libraries the functions were written for, and throw warnings when libraries invoked do not match those specified by function. This however increases documentation overhead (grows with size of library), and may not necessarily extend to later releases which may be compatible.
  • Possible solution: Provide suggestion of possible version conflict during a third-party library dependency call.
  • Problem: Wrong diagnosis can occur, e.g. errors due to buggy implementation of third-party functionality.
  • Solution: Not solved.

Problem: Scripts referencing libraries deployed in a central networked repository may break when connection to said network is lost. Even more so for this glue library.

  • Solution: Avoid performing git clone ... && pip install -e . workflows, especially over a network. Where possible, enforce either pip install git+... or pip install [PYPI] workflows instead.
  • Possible solution: For locally developed third-party dependencies, rely on virtual file systems (that cache contents and update dynamically whenever a change is detected), or prepare mirrors (i.e. defer to local library if central repository cannot be accessed).

Still a work-in-progress!


Some newer updates after a long lull on implementing the deprecation method. Firstly, there seems to be some proposal PEP723 floating around that are still provisional as of 2023-11-30 (looks like PEP722 has been rejected in favor of PEP723). PEP723 suggests to have a following code block for embedding pyproject.toml in single-file scripts (arguably important for users who are not necesarily familiar with installing dependencies). Looks like this:

# /// pyproject
# [run]
# requires-python = ">=3.11"
# dependencies = [
#   "requests<3",
#   "rich",
# ]
# ///

Note this does not actually fix the problem of having conflicting library versions. We want full backwards compatibility, as far as this library is concerned (hard to control versions on other dependencies, which is the whole point of trying to have this library self-contained).

Had a realization that library versioning should not be controlled by git, which limits applicability in cases where the library files are directly copied, or where git was not used to clone the repository in the first place. Need to somehow embed the version that is used by the script, hopefully automagically. This looks like a possible method: Method 1 and Method 2 and Method 3.

Some goals:

  • Backward compatibility only up till Py3.6, because the lab

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kochen-0.2025.15.tar.gz (70.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kochen-0.2025.15-py3-none-any.whl (67.1 kB view details)

Uploaded Python 3

File details

Details for the file kochen-0.2025.15.tar.gz.

File metadata

  • Download URL: kochen-0.2025.15.tar.gz
  • Upload date:
  • Size: 70.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kochen-0.2025.15.tar.gz
Algorithm Hash digest
SHA256 586b3bdda9246b7eb4b9238f3dabae11481f3a2398115524c14b5da6a3c4ad45
MD5 3fb68b826c3441bedbe91d18a6f8c3f2
BLAKE2b-256 de25bf9771df917c1fc4a7dc1c4a6e5ce8eaa9f4744027bf24b7ea0d5cb60f64

See more details on using hashes here.

Provenance

The following attestation bundles were made for kochen-0.2025.15.tar.gz:

Publisher: release.yml on pyuxiang/kochen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kochen-0.2025.15-py3-none-any.whl.

File metadata

  • Download URL: kochen-0.2025.15-py3-none-any.whl
  • Upload date:
  • Size: 67.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kochen-0.2025.15-py3-none-any.whl
Algorithm Hash digest
SHA256 5d5ab8d5b5410582a464ab2b0d15f9b061614ebad6099389b4a959f60107b833
MD5 1e5189c04853efae7601db0bc38ff59c
BLAKE2b-256 f4fee3e79d5e9418dc2c3fd6b4d45c605837034a9c01f064540779cea1702168

See more details on using hashes here.

Provenance

The following attestation bundles were made for kochen-0.2025.15-py3-none-any.whl:

Publisher: release.yml on pyuxiang/kochen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page