GithubHelp home page GithubHelp logo

yushiomote / perde Goto Github PK

View Code? Open in Web Editor NEW
24.0 3.0 1.0 3.87 MB

Python serialization framework powered by Rust

Home Page: https://yushiomote.github.io/perde

Rust 66.49% Python 32.47% Makefile 1.04%
serialization messagepack json toml yaml python typing dataclasses

perde's Introduction

perde: python-wrapped serde

Heavily under construction towards 0.1.0 ๐ŸŽ…

Project Status: WIP โ€“ Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. License PyPi Supported python versions Actions Status codecov Coding style

Python wrapper around the powerful Rust serialization framework.

  • Serialization & deserialization of python data structures.
  • Supports various types including dataclasses, generic types, enum and common built-in types.
  • Supports various serialization formats. By design, perde can support as many format as serde can.
  • Provides string case conversion of field names, skipping serialization/deserialization options, structure flattening.
  • Precise type checking based on type hints.
  • Very fast.

Install

pip install perde

Usage

>>> import perde

Assume you have a dataclass,

>>> @dataclass
... class A:
...     a: int
...     b: str

To serialize class A to JSON,

>>> perde.json.dumps(A(a=10, b='x'))
'{"a":10,"b":"x"}'

To deserialize JSON to class A,

>>> perde.json.loads_as(A, '{"a":10,"b":"x"}')
A(a=10, b='x')

To deserialize JSON to a dictionary,

>>> perde.json.loads('{"a":10,"b":"x"}')
{'a': 10, 'b': 'x'}

More formats are supported.

>>> perde.yaml.dumps(A(10, "x"))
'---\na: 10\nb: x'
>>> perde.yaml.loads_as(A, '---\na: 10\nb: x')
A(a=10, b='x')
>>> perde.msgpack.dumps(A(10, "x"))
b'\x82\xa1a\n\xa1b\xa1x'
>>> perde.msgpack.loads_as(A, b'\x82\xa1a\n\xa1b\xa1x')
A(a=10, b='x')

Supported formats

  • JSON (perde.json)
  • YAML (perde.yaml)
  • MessagePack (perde.msgpack)
  • TOML (perde.toml)
  • CBOR
  • Pickle
  • RON
  • BSON
  • Avro
  • JSON5
  • Postcard
  • URL
  • Environment variables
  • AWS Parameter Store
  • S-expressions
  • D-Bus
  • FlexBuffer
  • XML

Supported types

  • dataclass
  • Primitive types
    • int
    • str
    • float
    • bool
    • bytes
    • bytearray
  • Generic types
    • dict /typing.Dict
    • list / typing.List
    • set / typing.Set
    • frozenset / typing.FrozenSet
    • tuple / typing.Tuple
    • typing.Optional
    • typing.Union
    • typing.Any
  • Enum types
    • Enum
    • IntEnum
    • Flag
    • IntFlag
  • More built-in types
    • datetime.datetime
    • datetime.date
    • datetime.time
    • decimal.Decimal
    • uuid.UUID

Attributes

Attributes allow to modify the way of serialization/deserialization.

For example, to serialize/deserialize the field names as camelCase,

>>> @perde.attr(rename_all="camelCase")
... @dataclass
... class A:
...     foo_bar: int
...     bar_bar: int

>>> perde.json.dumps(A(foo_bar=1, bar_bar=2))
'{"fooBar":1,"barBar":2}'
>>> perde.json.loads_as(A, '{"fooBar":1,"barBar":2}')
A(foo_bar=1, bar_bar=2)

See the book for more details.

Benchmark

The benchmark repeats (de)serializing the data structure A 10000 times:

class A:
    a: int
    b: str
    c: float
    d: bool

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.