Last active
July 13, 2018 08:41
-
-
Save fortunto2/61740c0c3fc2b612f3955cce7a5760ef to your computer and use it in GitHub Desktop.
Сравнение двух способов создание классов с данными
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
import datetime | |
import json | |
from typing import Callable, Dict, Iterable, List, Tuple | |
import attr | |
from pprint import pprint | |
@attr.s | |
class File: | |
hash = attr.ib(default=attr.Factory(dict)) | |
mime = attr.ib(default=attr.Factory(dict)) | |
name = attr.ib(default=attr.Factory(str)) | |
path = attr.ib(default=attr.Factory(dict)) | |
size = attr.ib(default=attr.Factory(int)) | |
storage = attr.ib(default=attr.Factory(dict)) | |
user = attr.ib(default=attr.Factory(dict)) | |
updated_at = attr.ib(default=datetime.datetime.now()) | |
created_at = attr.ib(default=datetime.datetime.now()) | |
@attr.s | |
class Payload: | |
file = attr.ib(default=attr.Factory(File)) | |
mime = attr.ib(default=attr.Factory(dict)) | |
storage = attr.ib(default=attr.Factory(dict)) | |
@attr.s | |
class Message(object): | |
# service = attr.ib(validator=attr.validators.instance_of(str)) | |
uuid = attr.ib(default=uuid.uuid4()) | |
timestamp = attr.ib(default=datetime.datetime.now()) | |
status = attr.ib(default={'success': True}) | |
source = attr.ib(default=attr.Factory(dict)) | |
payload = attr.ib(default=attr.Factory(Payload)) | |
item = Message(payload=Payload()) | |
d = attr.asdict(item) | |
pprint(d) | |
# x = json.dumps(d, indent=4, sort_keys=True, default=str) | |
# pprint(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
import datetime | |
import json | |
from typing import Callable, Dict, Iterable, List, Tuple | |
from dataclasses import dataclass, asdict, field | |
from pprint import pprint | |
@dataclass | |
class File: | |
hash: str = '' | |
mime: str = '' | |
name: str = '' | |
path: str = '' | |
size: int = 0 | |
storage: str = '' | |
user: str = '' | |
updated_at: int = datetime.datetime.now() | |
created_at: int = datetime.datetime.now() | |
@dataclass | |
class Payload: | |
file: File = field(default_factory=dict) | |
@dataclass | |
class Message: | |
uuid: str = uuid.uuid4() | |
service: str = '' | |
timestamp: int = datetime.datetime.now().timestamp() | |
status: dict = None | |
source: str = None | |
payload: Payload = field(default_factory=dict) | |
def __post_init__(self): | |
self.status = {'success': True} | |
@dataclass | |
class InputMessage(Message): | |
pass | |
item = InputMessage() | |
item.payload = Payload(File()) | |
d = asdict(item) | |
pprint(d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment