Skip to main content

Validobj gives you valid objects

Project description

Tests Coverage PyPI Conda Version RTD

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

  1. Define a schema using dataclasses
    import dataclasses
    import enum
    from typing import Mapping, Set, Tuple, List
    
    
    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)
    
  2. Process a dictionary input into it using Validobj
    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],
            },
        ],
    }
    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'},
    #)
    #
    

The set of applied transformations is described in the documentation

Installation

The package can be installed with pip:

python3 -m pip install validobj

As well as with conda, from the conda-forge channel:

conda install validobj -c conda-forge

The code is hosted at

https://github.com/Zaharid/validobj

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

validobj-0.4.2.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

validobj-0.4.2-py2.py3-none-any.whl (11.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file validobj-0.4.2.tar.gz.

File metadata

  • Download URL: validobj-0.4.2.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for validobj-0.4.2.tar.gz
Algorithm Hash digest
SHA256 d8fb291938b65007d66f831591c60e4fe589bf684ec2adca15f5ac057968bcb4
MD5 8291d4c7c745a7fbc9301ae812fbbf14
BLAKE2b-256 e54b513aee26620612a8ed1ea3acf4004f0362925183611e368d789e4fbced8f

See more details on using hashes here.

File details

Details for the file validobj-0.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: validobj-0.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for validobj-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3118c15cf86e26666549a72e20f9fc0923a13614bd2563cbde37bd02b28f9909
MD5 7361a9c1807b7b6d77b98a23497df42f
BLAKE2b-256 13bca5048426b89816bdd156656becee3a2dbb6ff37fbdb5aba802b68fb8425a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page