Skip to main content

thinking-modules

CI PyPI version codecov

Part of thinking family.

Module-related python utilities - recursive package scan and import, modeling of names and modules, etc

Main use case for this library is importing everything in given package. Simplest example are decorators that register stuff for CI, a bit like annotation scanning in Java, which need to be triggered/executed before the main portion of your program.

Besides that provides models and related utilities for modules and their names.

Requires python 3.12. Is properly typed.

API

The code is pretty self-documenting (meaning "browse the code, don't expect Sphinx"). It is organized into following modules:

  • thinking_modules.model
    • ModuleName
      • represent module or package name
      • doesn't import the module, is just a pointer
      • allows for easy modeling of subpackages, submodules, parent packages, etc
      • can refer to non-existent modules
    • ModuleNamePointer
      • union type representing everything that can be parsed to ModuleName - a str, module itself, etc
    • Module
      • is a descriptor over a python module or package
      • may refer to non-existing piece of code, but will raise exception when using most methods
      • allows you to recognize whether something is a module, package, pkg.__main__ file or shell session
      • exposes some other useful info, like whether a module has been already imported, where is it located in filesystem (if anywhere), what is its root package and allows you to import it with importlib
    • most properties of Module and ModuleName are lazy and cached, so, for example, if you create a Module pointing to a non-existent piece of code, it won't raise exception until you actually access m.module_object; similarly, once you access m.kind, it will be cached and will be quickly accessible next time you access it
    • kudos to lazy for that
  • thinking_modules.scan
    • holds a single function: scan(pkg: ModuleNamePointer)
    • that function takes a package name (fails if given a non-package module name)
    • it analyses filesystem, trying to find all the module names within the tree stemming from that package
  • thinking_modules.main_modules
    • when we're running anything, its name becomes __main__
    • that module still should be accessible by name based on its filepath
    • for example, if you do python -m pkg.subpkg.mod, you'll execute file .../pkg/subpkg/mod.py which otherwise would be available as pkg.subpkg.mod, but in such runtime will have __name__ __main__
    • this module will analyse (on import) file structure, and expose main_name (of type ModuleName) and main_module (of type Module) that describe pkg.subpkg.mod (as opposed to __main__)
    • it will also (on import) alias pkg.subpkg.mod in sys.modules to __main__, so you can safely do circular imports
  • thinking_modules.definitions
    • micro-DSL for queries like:
      • instance(SomeClass()).type_.defining_module
      • type_(SomeClass).defining_module (equivalent to the one above)
      • instance(...).type_.defined_in_module("pkg.mod")
      • instance(...).type_.defined_in_package("pkg") (meaning "in package pkg or any of its subpackages or submodules)
    • nothing complicated, but makes for readable metaprogramming code
  • thinking_modules.immutable
    • helper module for something that emulates NamedTuple, while allowing for lazy properties
    • not really related to domain of this project, strictly technical, but pretty useful util
    • is needed, as lazy works around the __dict__, which is lacking in NamedTuples

Reading the test suite is gonna be useful too, in case of uncertainty.

Metadata

Release files for thinking-modules 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for thinking-modules 0.0.4
File Interpreter ABI Platform Actions
thinking_modules-0.0.4-py3-none-any.whl Python 3 none any Details for thinking_modules-0.0.4-py3-none-any.whl

Release files / thinking_modules-0.0.4-py3-none-any.whl

Download URL thinking_modules-0.0.4-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91f55dd9c5fc206ebd101a9cdd9529c560973a48131b68d07944b54fa8e3198e
BLAKE2b-256 checksum
How to use checksums
bde9f12cf811dbf601604834145f0a2c42b491919f4b7056e5417c25188c50da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.5

Security for thinking-modules 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page