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
File details
Details for the file python-options-1.0.0.post2.tar.gz
.
File metadata
- Download URL: python-options-1.0.0.post2.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6fe9d052f0c2211817b29ada75543aeb6dd5555b5fef6fbf2621e466d41ee19 |
|
MD5 | 288a95090b3591eed1ecdaa4ba46076e |
|
BLAKE2b-256 | 41924be90d44291f8aab9da4732bee3d7a2ecc17aed620117c9f2d7ef4b74b3c |
File details
Details for the file python_options-1.0.0.post2-py3-none-any.whl
.
File metadata
- Download URL: python_options-1.0.0.post2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30e349c63e0f3baed38ba21ea95e59e47557479800e7d9db79e9035089fb60b2 |
|
MD5 | d768c7f36324daed33268a22281b2078 |
|
BLAKE2b-256 | b546c9e015287e2239f8220884545346f98b5d5a5e7db08daa1b6dc4caa46592 |