Simple, extensible and functional serializer
Project description
Cats Serialization
Introduction
It is a simple, extensible and functional serializer for objects. The goal of the project is to provide a developer with a simple tool for developing REST applications... and not only =)
More information in wiki Git repo
Build
Install requirements
sudo apt install python3-build, python3.11-venv, twine
Clone repo
git clone http://git.dev-store.ru/Clu/cats-serialization-10
cd cats-serialization-10
Try in project directory
python3 -m buildtwine upload dist/*
Installation
pip install cats-serialization
Main Features
- Multi style notation
- Serialization and deserialization
- Validation and logical operation with same
- Full-object validation possibility
- Union types supporting
- Correction values (by validators)
Multi style notation
class Box(BaseSerializer):
annotation: str
field = Field()
def virtual(self, data):
return data.do_smth(), custom_name # custom_name if needed
Support for union operation
class Address(BaseSerializer):
star: str
planet: str
moon: str | Undefined # like not required
class Box(BaseSerializer):
address: str | Address # can be string or object
# It`s ok for:
# { address: 'Sun, Mars' }
# { address: { star: 'Sun', planet: 'Mars' } }
it works for fields notation
class Box(BaseSerialiser):
# address is str or object
address = Field(is_type=str) | ObjectField(Address)
Logical operation with validators
one_field = Field(validator=IsType(int) | IsType(str))
two_field = Field(validator=IsType(int) & IsRequired())
Instance based serializer
class Box(Serialiser):
pass
box = Box() # New instance
print(box.serialize(data))
Deserialization
class Goods:
pass
class Unpack(Deserializer):
pass
unpack = Unpack()
goods = unpack.create(data, prototype=Goods)
Correction values in passing
class IsName(BaseValidator):
def validate(self, value, *args):
if type(value) is not str:
raise ValidationError('It is not the name')
return value.strip() # safe correction
class Box(BaseSerializer):
# name will be corrected if type check will be passed
name = Field(validator=IsName())
Uses
Class-based scheme
If you need to create class based serializer, it will just inherit on BaseSerializer class
class MyScheme(BaseSerializer):
pass
Serialization
# Call serialize(...) from class MyScheme instantly
some_dict = MyScheme.serialize(some_data)
Instance-based serialisation
It helps if it really needed
class MyScheme(Serializer):
def __init__(self):
pass
Serialization
# At first, create instance
scheme = MyScheme()
# Now, you can call serialise(...)
some_dict = scheme.serialize(some_data)
Common Fields
Annotations
If you uses annotations, all field will be had type-check validation
class MyScheme(BaseSerializer):
any: Any # Any field
string: str # Primitive fields with type validation
not_required: str | Undefined # Not required field
blank: str | None # Can be blank field
integer: int | str | bool # Union fields
list: list[str] # Generic list
defaults: str = 'Hello!' # Field with default value
item: ItemScheme # Other serializer
items: list[ItemScheme] # List of other serializer
Fields
class MyScheme(BaseSerializer):
field = Field() # Simple field
item = ObjectField(ItemScheme) # Other serializer
items = IterableField(ItemScheme) # List of other serializer
required = Field(is_required=True) # Any required field
typed = Field(is_type=str) # Required with type check
# With validator
validated = Field(validator=IsStrong() & IsRequired())
Methods
class MyScheme(BaseSerializer):
@classmethod
def virtual(cls, data):
return data.sub_field, 'override_name_if_needed'
# it works, but be careful
def virtual_self(self, data):
return data.sub_field, 'override_name_if_needed'
Base Fields inherit
class MyField(BaseField):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Validation inherit
Generic validator has pattern:
class MyValidator(BaseValidator):
def validate(self, value, *args):
if smth_wrong(value):
raise ValidationError('Message')
You can make full-object validator. For example, it needs if one field will depend on another.
class MyValidator(BaseValidator):
def validate(self, value, key, data): # use key and data
if data.id == value: # it`s just example
raise ValidationError('Message')
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cats_serialization-1.1.10.tar.gz.
File metadata
- Download URL: cats_serialization-1.1.10.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e984d8d4a00f54a6fa1ccb5de1cee19e40b676e5df53372791db09ade360043d
|
|
| MD5 |
ce3f4ef8d6b86203d4efbae5a634cbae
|
|
| BLAKE2b-256 |
4a6e8d430fee6e55e23d87c1e4559c80cf7a11d17e2b0202090c1b4ca4601fda
|
File details
Details for the file cats_serialization-1.1.10-py3-none-any.whl.
File metadata
- Download URL: cats_serialization-1.1.10-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acd5ba02e2640f36a49f3efffd53c8a88eeb3a4d4a847cd6d94d65fd4f5231e0
|
|
| MD5 |
93813dc57cb6061f97fd7e63c4fcca44
|
|
| BLAKE2b-256 |
b19d80e9b4fc14f19550b16b1525b50f866fedd67049a393c7002e1b47ca7826
|