File: bench_cattrs.py

package info (click to toggle)
python-msgspec 0.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,356 kB
  • sloc: javascript: 23,944; ansic: 20,540; python: 20,465; makefile: 29; sh: 19
file content (51 lines) | stat: -rw-r--r-- 990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations

import enum
import datetime
from typing import Literal

import attrs
import cattrs.preconf.orjson


class Permissions(enum.Enum):
    READ = "READ"
    WRITE = "WRITE"
    READ_WRITE = "READ_WRITE"


@attrs.define(kw_only=True)
class File:
    name: str
    created_by: str
    created_at: datetime.datetime
    updated_by: str | None = None
    updated_at: datetime.datetime | None = None
    nbytes: int
    permissions: Permissions
    type: Literal["file"] = "file"


@attrs.define(kw_only=True)
class Directory:
    name: str
    created_by: str
    created_at: datetime.datetime
    updated_by: str | None = None
    updated_at: datetime.datetime | None = None
    contents: list[File | Directory]
    type: Literal["directory"] = "directory"


converter = cattrs.preconf.orjson.make_converter(omit_if_default=True)


def encode(obj):
    return converter.dumps(obj)


def decode(msg):
    return converter.loads(msg, Directory)


label = "cattrs"