war
warlock
pypi i warlock
war

warlock

Python object model built on JSON schema and JSON patch.

by Brian Waldon

2.0.1 (see all)License:Apache-2.0
pypi i warlock
Readme

Warlock 🧙‍♀️

Create self-validating Python objects using JSON schema.

PyPI PyPI - Python Version PyPI - Downloads

Build Status Coverage Status GitHub commits since latest release (branch)

Package management: poetry Code Style Black

Installation

Warlock is available on PyPI:

pip install warlock

Usage

1) Create your schema

```python
>>> schema = {
    'name': 'Country',
    'properties': {
        'name': {'type': 'string'},
        'abbreviation': {'type': 'string'},
        'population': {'type': 'integer'},
    },
    'additionalProperties': False,
}
```

2) Create a model

```python
>>> import warlock
>>> Country = warlock.model_factory(schema)
```

3) Create an object using your model

```python
>>> sweden = Country(name='Sweden', abbreviation='SE')
```

4) Let the object validate itself

```python
>>> sweden.name = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "warlock/core.py", line 53, in __setattr__
    raise InvalidOperation(msg)
warlock.core.InvalidOperation: Unable to set 'name' to '5'

>>> sweden.overlord = 'Bears'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "warlock/core.py", line 53, in __setattr__
    raise InvalidOperation(msg)
warlock.core.InvalidOperation: Unable to set 'overlord' to 'Bears'
```

5) Generate a JSON Patch document to track changes

```python
>>> sweden.population=9453000
>>> sweden.patch
'[{"path": "/population", "value": 9453000, "op": "add"}]'
```

GitHub Stars

127

LAST COMMIT

2mos ago

MAINTAINERS

1

CONTRIBUTORS

19

OPEN ISSUES

9

OPEN PRs

1
VersionTagPublished
2.0.1
2mos ago
2.0.0
2mos ago
2.0.0a0
2mos ago
1.3.3
3yrs ago
No alternatives found
No tutorials found
Add a tutorial