-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththings.py
More file actions
47 lines (36 loc) · 1.16 KB
/
things.py
File metadata and controls
47 lines (36 loc) · 1.16 KB
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
from dataclasses import dataclass
from typing import Optional
from uuid import uuid4 as uuid
COLLECTION_NAME = 'things'
@dataclass
class BaseThingsData:
'''Base attributes of Things type'''
pass
@dataclass
class ThingsData(BaseThingsData):
'''Things data'''
id: Optional[str] = None
@dataclass
class ThingsItemKeys:
'''Things DDB item keys'''
pk: str
sk: str
def get_data(self):
return { k:v for (k, v) in self.__dict__.items() if k not in ThingsItemKeys.__dict__.keys() }
@dataclass
class ThingsItem(ThingsData, ThingsItemKeys):
'''Things DDB item'''
id: str
def get_data(self):
return { k:v for (k, v) in self.__dict__.items() if k not in ['sk', 'pk'] }
def create_keys() -> ThingsItemKeys:
'''Create keys for DDB'''
key = '{}#{}'.format(COLLECTION_NAME, str(uuid()))
return ThingsItemKeys(**{'pk': key, 'sk': key})
def get_keys_from_id(_id: str) -> ThingsItemKeys:
'''Get keys for DDB'''
key = '{}#{}'.format(COLLECTION_NAME, _id)
return ThingsItemKeys(**{'pk': key, 'sk': key})
def get_id_from_keys(keys: ThingsItemKeys) -> str:
'''Get id from keys'''
return keys.pk.split('#')[1]