Skip to content

Commit b9933a2

Browse files
committed
Felt refactored, might delete later
1 parent d7da833 commit b9933a2

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

aws_dynamodb_parser/utils.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,47 @@ def parse(dynamodb_object: Union[dict, list]) -> Union[dict, list]:
66
if isinstance(dynamodb_object, list):
77
return [parse(obj) for obj in dynamodb_object]
88

9-
parsed = {}
10-
for key in dynamodb_object.keys():
11-
parsed[key] = _parse_pair(dynamodb_object[key])
12-
return parsed
9+
return {key: _parse_property(value) for key, value in dynamodb_object.items()}
1310

1411

1512
# pylint:disable=too-many-return-statements
16-
def _parse_pair(type_value_pair: dict) -> Any:
17-
data_type = list(type_value_pair.keys())[0]
13+
def _parse_property(prop: dict) -> Any:
14+
data_type = next(iter(prop))
15+
value = prop.get(data_type, [])
1816

1917
if data_type in {"S", "SS", "BOOL"}:
20-
return type_value_pair[data_type]
18+
return value
2119

2220
if data_type == "N":
23-
return _to_num(type_value_pair[data_type])
21+
return _to_num(value)
2422

2523
if data_type == "NS":
26-
return [_to_num(data) for data in type_value_pair[data_type]]
24+
return [_to_num(item) for item in value]
2725

2826
if data_type == "B":
29-
return base64.decodebytes(bytes(type_value_pair[data_type], "utf-8"))
27+
return _to_bytes(value)
3028

3129
if data_type == "BS":
32-
return [base64.decodebytes(bytes(data, "utf-8")) for data in type_value_pair[data_type]]
30+
return [_to_bytes(item) for item in value]
3331

3432
if data_type == "M":
35-
return parse(type_value_pair[data_type])
33+
return parse(value)
3634

3735
if data_type == "L":
38-
return [_parse_pair(item) for item in type_value_pair[data_type]]
36+
return [_parse_property(item) for item in value]
3937

40-
if data_type == "NULL" and type_value_pair[data_type]:
38+
if data_type == "NULL" and value:
4139
return None
4240

43-
raise TypeError("Unknown DynamoDB data type '%s' with value '%s'" % (data_type, type_value_pair[data_type]))
41+
raise TypeError("Unknown DynamoDB data type '%s' with value '%s'" % (data_type, value))
4442

4543

46-
def _to_num(number: Any) -> Union[float, int]:
44+
def _to_num(value: Any) -> Union[float, int]:
4745
try:
48-
return int(number)
46+
return int(value)
4947
except ValueError:
50-
return float(number)
48+
return float(value)
49+
50+
51+
def _to_bytes(value: Any) -> bytes:
52+
return base64.decodebytes(bytes(value, "utf-8"))

0 commit comments

Comments
 (0)