How gaogaotiantian/objprint works
Project type and purpose
• This is a Python library (“objprint”) published on PyPI.
• Its purpose is to provide functions (op, objstr, objjson) and a decorator (@add_objprint) for printing or serializing arbitrary Python objects in a human-readable or JSON format, with configurable depth, colorization, and custom formatter plugins.
File structure
• Top-level:
• CI/workflow configs (.github/workflows/)
• Project metadata (pyproject.toml, Makefile, LICENSE, README.md)
• Development requirements (requirements-dev.txt)
• Source root: src/objprint/
• Core modules under src/objprint/:
• objprint.py – main entrypoints (op, objstr, objjson)
• decorator.py – implementation of @add_objprint decorator
• frame_analyzer.py – captures call site info (filename, line number)
• executing/ subpackage – execution-context utilities for runtime introspection
• _exceptions.py, _position_node_finder.py – helper code for executing
• Tests under tests/ validate each feature and module.
Key architecture insights from README
• Uses a global/configurable registry of type formatters (plugin pattern)
• Separation between formatting to string vs. building a JSON-serializable structure
• Decorator pattern to inject __str__ dynamically
• Optional features toggled via global or per-call config
Main components
a. Entrypoint Layer
• op() / objstr() / objjson() functions (in objprint.py)
• install() to expose op() globally
b. Core Engine
• Formatter Registry (maps Python types → formatter functions)
• Format Dispatcher (selects built-in vs. custom formatter via MRO)
• Object Inspector / Walker (reflects attributes based on regex filters)
• JSON Converter (builds dict/list primitives)
c. Extension Layer
• @add_objprint decorator (in decorator.py), which wraps class’s __str__/__repr__ to call core engine
d. Utility Modules
• frame_analyzer & executing subpackage (capture call site for line_number=True)
• Config Module (global vs. per-call settings)
e. External Interfaces
• Standard Python print() and json.dumps() (the library integrates with them)
Component interactions and data flows
• User calls op(my_obj, indent=4, format="json") → Entrypoint Layer
• Entrypoint reads/merges global & local Config → Config Module
• Core Engine queries Formatter Registry for type(my_obj) → Formatter Dispatcher
• If format="json", routes to JSON Converter; else, routes to Inspector & String Builder
• Inspector may call frame_analyzer if line_number=True
• Colorization applied last by color_util if enabled
• Decorator usage: at import time, @add_objprint injects calls to Core Engine
Architectural patterns and design principles
• Layered architecture: Entrypoint → Engine → Utility modules
• Plugin/Registry pattern for type formatters (extensibility)
• Decorator pattern to augment user classes
• Use of Python’s MRO for formatter resolution
• Strict isolation of test suite in tests/