https://github.com/pskd73/pydictable
Python object serializer/deserializer and schema validator with extendability!
https://github.com/pskd73/pydictable
deserializer json-schema python schema-validation serializer
Last synced: about 2 months ago
JSON representation
Python object serializer/deserializer and schema validator with extendability!
- Host: GitHub
- URL: https://github.com/pskd73/pydictable
- Owner: pskd73
- License: mit
- Created: 2021-03-30T20:55:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-10T17:07:03.000Z (9 months ago)
- Last Synced: 2025-03-17T13:52:48.660Z (about 2 months ago)
- Topics: deserializer, json-schema, python, schema-validation, serializer
- Language: Python
- Homepage:
- Size: 147 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# pydictable
A pure python, zero dependency solution for
1. Schema (json/dict) validation
2. Serialize/Deserialize to json### Installation
```
pip install pydictable
```### Example
```pythonclass LatLng(DictAble):
lat: int
lng: intclass Address(DictAble):
pin_code: int
lat_lng: LatLngclass Person(DictAble):
name: str
address: Addressinput_dict = {
'name': 'Pramod',
'address': {
'pin_code': 560032,
'lat_lng': {
'lat': 12345,
'lng': 67890
}
}
}p = Person(dict=input_dict) # Raises DataValidationError if not valid
p.name # Pramod
p.address # Address object
p.address.pin_code # 560032p.to_dict() == input_dict # Serialize
p.address.pin_code = 518466 # You can change the values
# you can initiate with named params too
p2 = Person(
name='Pramod',
address=Address(
pin_code=560032,
lat_lng=LatLng(
lat=12345,
lng=67890
)
)
)Person.get_input_schema() # Get the Schema spec as json!
```### Extendability
You can quickly build your own fields as explained below
```python
class PositiveIntField(IntField):
def validate_json(self, field_name: str, v):
assert v > 0, 'Should be positive integer'class Person(DictAble):
age: int = PositiveIntField()Person(dict={'age': -1}) # Raises DataValidationError
```### Polymorphism
Auto converts multiple types
```python
class Homo(DictAble):
name: strclass Neanderthal(Homo):
animals_killed: intclass Sapien(Homo):
words_spoken: intclass Human(DictAble):
species: Homo = MultiTypeField([Neanderthal, Sapien])human = Human(dict={
'species': {
'name': 'Mufasa',
'words_spoken': 1024,
'__type': 'Sapien'
}
})
assert human.species.name == 'Mufasa'
assert isinstance(human.species, Sapien)
assert human.species.words_spoken == 1024
```Feel free to report bugs or push changes! Cheers!