Validobj gives you valid objects
Project description
Validobj
Validobj is library that takes semistructured data (for example JSON and YAML configuration files) and converts it to more structured Python objects. It places the emphasis on:
- Good error messages (rather than avoiding extra work in the error handling code).
- Schema defined in terms of dataclasses and other high level objects such as enums, as well as a subset of the typing module.
- Simplicity of implementation (rather than full generality).
Validobj requires Python 3.7 and has no other dependencies.
Documentation
https://validobj.readthedocs.io/en/latest/
Example
import dataclasses
import enum
from typing import Mapping, Set, Tuple, List
from validobj import parse_input
inp = {
'global_environment': {'CI_ACTIVE': '1'},
'stages': [
{
'name': 'compile',
'os': ['linux', 'mac'],
'script_path': 'build.sh',
'disk_permissions': ['READ', 'WRITE', 'EXECUTE'],
},
{
'name': 'test',
'os': ['linux', 'mac'],
'script_path': 'test.sh',
'framework_version': [4, 0],
},
],
}
class DiskPermissions(enum.Flag):
READ = enum.auto()
WRITE = enum.auto()
EXECUTE = enum.auto()
class OS(enum.Enum):
mac = enum.auto()
windows = enum.auto()
linux = enum.auto()
@dataclasses.dataclass
class Job:
name: str
os: Set[OS]
script_path: str
framework_version: Tuple[int, int] = (1, 0)
disk_permissions: DiskPermissions = DiskPermissions.READ
@dataclasses.dataclass
class CIConf:
stages: List[Job]
global_environment: Mapping[str, str] = dataclasses.field(default_factory=dict)
print(parse_input(inp, CIConf))
# This results in a dataclass instance with the correct types:
#
#CIConf(
# stages=[
# Job(
# name='compile',
# os={<OS.linux: 3>, <OS.mac:1>},
# script_path='build.sh',
# framework_version=(1, 0),
# disk_permissions=<DiskPermissions.EXECUTE|WRITE|READ: 7>,
# ),
# Job(
# name='test',
# os={<OS.linux: 3>, <OS.mac: 1>},
# script_path='test.sh',
# framework_version=(4, 0),
# disk_permissions='<DiskPermissions.READ: 1>',
# ),
# ],
# global_environment={'CI_ACTIVE': '1'},
#)
#
Installation
The package can be installed with pip
:
python3 -m pip install validobj
The code is hosted at
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
validobj-0.3.1.tar.gz
(17.3 kB
view details)
Built Distribution
File details
Details for the file validobj-0.3.1.tar.gz
.
File metadata
- Download URL: validobj-0.3.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40057b156607fe9a9239d3b8694f7849f4e7bfdb5ba08026531c56d0085b65d5 |
|
MD5 | f2f00f9c0b39e5d172f025551cacbfa9 |
|
BLAKE2b-256 | 665af0c65ab3989a33e408b56c9bf2536bb4f8665327a15ef068f6c93cbfd5b5 |
File details
Details for the file validobj-0.3.1-py2.py3-none-any.whl
.
File metadata
- Download URL: validobj-0.3.1-py2.py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b940e9c778465bf18044e13cdb4e62a4bc12e4c9d91ef4e35cc45d6d4b030167 |
|
MD5 | 6a509645a4720c8d69f3a6582eddc4a0 |
|
BLAKE2b-256 | 885abbbc46608cf3406502d029fb0ab4d2b0f9f0022cab84b44d461c3e700011 |