Mapping Shortcuts
python package with useful mapping shortcuts
Installation
pip install mapping-shortcuts
Contains
- Function decorator for mapping factory
- Class decorator for mapping factory
- Metaclass for mapping factory
- Function for import all subpackages in package
- CLI param parser and processor
Decorator factory for mapping
function decorator
from mapping_shortcuts.decors import create_collector
decorator, collection = create_collector(
raise_on_duplicate=True, # default: True
)
@decorator('key1')
def func1():
...
@decorator('key2')
def func2():
...
print(collection)
'''
output: {
'key1': <function func1 at 0x104adc430>,
'key2': <function func2 at 0x104adc4c0>,
}
'''
class decorator
from mapping_shortcuts.decors import create_class_collector
decorator, collection = create_class_collector(
raise_on_duplicate=True, # default: True
key_getter=lambda x: x.key # default: lambda x: x.__name__
)
@decorator
class SomeClass1:
key = 123
@decorator
class SomeClass2:
key = 456
print(collection)
'''
output: {
123: <class '__main__.SomeClass1'>,
456: <class '__main__.SomeClass2'>,
}
'''
Metaclass factory for mapping
import abc
from mapping_shortcuts.meta import create_collection_meta
MetaClass, collections = create_collection_meta(
base=abc.ABCMeta, # default: type
getter=lambda x: x.__name__, # default: lambda x: str(x)
raise_on_duplicate = True, # default: True
)
class A(metaclass=MetaClass):
...
class B(metaclass=MetaClass):
...
print(collections)
'''
oputput: {
'A': <class '__main__.A'>,
'B': <class '__main__.B'>,
}
'''
Function for import all subpackages in package
For exmaple with have five files:
- python code
app/tools.py - empty file
app/providers/a/__init__.py - empty file
app/providers/b/__init__.py - python code in
app/providers/a/module.py - python code in
app/providers/b/module.py
app/tools.py be like:
from mapping_shortcuts.decors import create_collector
decorator, collection = create_collector()
app/providers/a/module.py is:
from app.tools import decorator
@decorator('A-func')
def function_a():
...
app/providers/b/module.py is:
from app.tools import decorator
@decorator('B-func')
def function_b():
...
execute load_package():
from mapping_shortcuts.dirtools import load_package
from app.tools import collection
load_package('app.providers')
print(collection)
'''
output: {
'A-func': <function function_a at 0x104cfa0e0>,
'B-func': <function function_b at 0x104cfa290>,
}
'''
CLI param parser and processor
from pydantic import BaseModel, Field
from mapping_shortcuts.cli import cli_handler, process_sysargv
@cli_handler('command1', desc='run handler for command1')
def handler_1(args: dict[str, str | bool]) -> None:
print('command 1 handled!')
class ArgModel(BaseModel):
x: int = Field(alias='--x', description='arg x')
y: int = Field(alias='--y', description='arg y')
@cli_handler('command2', desc='sum X and Y', model=ArgModel)
def handler_1(args) -> None:
print(f'{args.x=}')
print(f'{args.y=}')
print(f'result: {args.x + args.y}')
@cli_handler('command3', desc='sum X and Y')
def handler_1(args: ArgModel) -> None:
print(f'{args.x=}')
print(f'{args.y=}')
print(f'result: {args.x + args.y}')
if __name__ == '__main__':
process_sysargv(
help_msg_header='That\'s my program!',
help_msg_run_cmd='python -m project',
)
Execution:
$ python script.py
That's my program!
usage: python -m project [options] [command]
command:
help - see this cool msg again
command1 - run handler for command1
command2 - sum X and Y
--x - arg x
--y - arg y
command3 - sum X and Y
--x - arg x
--y - arg y
$ python script.py command3 --x=123 --y=456
args.x=123
args.y=456
result: 579
Metadata
Release files for mapping-shortcuts 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mapping_shortcuts-1.2.1.tar.gz | 9.3 kB | Without provenance | Details for mapping_shortcuts-1.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mapping_shortcuts-1.2.1-py3-none-any.whl | Python 3 | none | any | Details for mapping_shortcuts-1.2.1-py3-none-any.whl |
Total release size: 20.3 kB
Release files / mapping_shortcuts-1.2.1.tar.gz
| Download URL | mapping_shortcuts-1.2.1.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
729880226f21a58534601764b20e87b9d17ebd299122398633d4db0f79542fa8
|
|
BLAKE2b-256 checksum How to use checksums |
33fe1c961630d62fd2b622ad327a9f0a02765d1ffb3289cf7a52e6b836e9a8cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.8
|
Release files / mapping_shortcuts-1.2.1-py3-none-any.whl
| Download URL | mapping_shortcuts-1.2.1-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2504bf8bc0260f6e0663d24a789fe7e79cec7345a9b7e67b81213aa555860400
|
|
BLAKE2b-256 checksum How to use checksums |
c0dc7d85945f67279145c3e4071510bda7d2a5a49032f985dd1dfcd83543d811
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.8
|
Security for mapping-shortcuts 1.2.1
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.