Make python class with option base on property. Using build option for client like kafka, command option like ffmpeg.
Project description
python-options
Make python class with option base on property. Using build option for client like kafka, command option like ffmpeg.
Support: python 3.6+
Feature:
- Build options (config) easily.
- Clone option (changing not effect with other config which initial from single class)
- (Optional) Filter value on set.
- (Optional) Auto fill default value if delete old value.
Example:
from typing import Any, Type
from pyopt import Options, UnsetOption, option
def filter_type(_type: Type):
def _filter_wrapper(instance: Any):
if not isinstance(instance, _type):
raise TypeError(f"Instance must be `{_type.__name__}`")
return instance
return _filter_wrapper
class Config(Options):
server = option("url", filter_type(str), doc="URL of server host.")
if __name__ == '__main__':
conf = Config()
try:
conf.server = 1 # raise TypeError
except TypeError as e:
print(f"ERROR: {e}")
try:
print(conf.server)
except UnsetOption as e:
print(f"Unset: {e}")
conf.server = "127.0.0.1:9090"
print(conf.__repr__())
print(conf.build())
>>> config = Config()
>>> config.server = 1
TypeError: Instance must be `str`
>>> conf.server
The option `url` of Config hasn't been set.
>>> config.server = "127.0.0.1:9090"
>>> print(conf)
Config:
- server | "url" = "127.0.0.1:9090" | Doc: URL of server host.
>>> print(conf.build())
{'url': '127.0.0.1:9090'}
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for python-options-1.0.0.post1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1272ee62cd60dac2f8fa0732d2cfd70c1938696a15b83f3f3e41167fabd4326a |
|
MD5 | 419586398dba3d0f20885e70f038fc14 |
|
BLAKE2b-256 | d3f7be9f04a992037582da180968b9375c96486ff03fb59d8a2c769e314d5580 |
Close
Hashes for python_options-1.0.0.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1b9b5f454a9e3e068fc9cb8af4dbbe124e107f3b3b062617c5872c77ef93095 |
|
MD5 | 12073e8c291fd3fbe3a37794ca3e7a0f |
|
BLAKE2b-256 | 8bc0a9258f0c29e53b9babe70627e5e6ab17e0390befcb55ff403d34d78f6ea0 |