Useful shortcuts for create mappings
Project description
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
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
Built Distribution
File details
Details for the file mapping_shortcuts-1.2.1.tar.gz
.
File metadata
- Download URL: mapping_shortcuts-1.2.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 729880226f21a58534601764b20e87b9d17ebd299122398633d4db0f79542fa8 |
|
MD5 | 9d5f6dcb45ab78cf4d1cd992639ac077 |
|
BLAKE2b-256 | 33fe1c961630d62fd2b622ad327a9f0a02765d1ffb3289cf7a52e6b836e9a8cb |
File details
Details for the file mapping_shortcuts-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: mapping_shortcuts-1.2.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2504bf8bc0260f6e0663d24a789fe7e79cec7345a9b7e67b81213aa555860400 |
|
MD5 | 5e8da4554d4c414c79050445089ffc27 |
|
BLAKE2b-256 | c0dc7d85945f67279145c3e4071510bda7d2a5a49032f985dd1dfcd83543d811 |