Skip to main content

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

  1. python3 -m build
  2. twine 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')

Metadata

Release files for cats-serialization 1.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cats-serialization 1.1.10
File Size Uploaded
cats_serialization-1.1.10.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cats-serialization 1.1.10
File Interpreter ABI Platform
cats_serialization-1.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 29.0 kB

Release files / cats_serialization-1.1.10.tar.gz

Download URL cats_serialization-1.1.10.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e984d8d4a00f54a6fa1ccb5de1cee19e40b676e5df53372791db09ade360043d
BLAKE2b-256 checksum
How to use checksums
4a6e8d430fee6e55e23d87c1e4559c80cf7a11d17e2b0202090c1b4ca4601fda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / cats_serialization-1.1.10-py3-none-any.whl

Download URL cats_serialization-1.1.10-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
acd5ba02e2640f36a49f3efffd53c8a88eeb3a4d4a847cd6d94d65fd4f5231e0
BLAKE2b-256 checksum
How to use checksums
b19d80e9b4fc14f19550b16b1525b50f866fedd67049a393c7002e1b47ca7826
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.1.10 This release

2 release files

1.1.9

2 release files

1.1.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page