Skip to content

Commit 40b2e9c

Browse files
authored
Merge pull request #1 from gridsmartercities/parse_lists
Added list parsing, brought library more in line with other Grid libraries
2 parents 88ae8f9 + b9933a2 commit 40b2e9c

File tree

8 files changed

+198
-146
lines changed

8 files changed

+198
-146
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ venv.bak/
114114
*/.DS_Store
115115

116116
# vscode
117-
.vscode
117+
.vscode

CONTRIBUTING.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@ aws_dynamodb_parser/utils.py 29 0 24 0 100%
2424
We require all code to adhere to our linting style as defined in [pylintrc](https://github.com/gridsmartercities/aws-dynamodb-parser/blob/master/pylintrc). We use [prospector](https://pypi.org/project/prospector/) to run linting checks.
2525
```sh
2626
$ prospector
27+
Check Information
28+
=================
29+
Started: 2020-10-01 17:55:32.697915
30+
Finished: 2020-10-01 17:55:33.725397
31+
Time Taken: 1.03 seconds
32+
Formatter: text
33+
Profiles: .prospector.yaml, full_pep8, no_doc_warnings, strictness_veryhigh, no_member_warnings
34+
Strictness: from profile
35+
Libraries Used:
36+
Tools Run: dodgy, mccabe, pep8, profile-validator, pyflakes, pylint
37+
Messages Found: 0
38+
External Config: pylint: /Users/rob/Documents/code/grid/aws-dynamodb-parser/pylintrc
39+
```
2740

28-
# clean prospector output will be added once the repo is cleaned up
41+
We require code to have correct type hinting using [Mypy](http://www.mypy-lang.org/) to check type hints.
42+
```sh
43+
$ ./tools/mypy.sh
44+
Success: no issues found in 2 source files
2945
```
3046

3147
And lastly that [Bandit](https://pypi.org/project/bandit/) security checks pass

aws_dynamodb_parser/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from aws_dynamodb_parser.utils import * # noqa
1+
__all__ = ["parse"]
2+
3+
from .utils import parse

aws_dynamodb_parser/utils.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
import base64
2+
from typing import Any, Union
23

34

4-
def parse(dynamodb_object):
5-
for key in dynamodb_object.keys():
6-
dynamodb_object[key] = parse_pair(dynamodb_object[key])
7-
return dynamodb_object
5+
def parse(dynamodb_object: Union[dict, list]) -> Union[dict, list]:
6+
if isinstance(dynamodb_object, list):
7+
return [parse(obj) for obj in dynamodb_object]
88

9+
return {key: _parse_property(value) for key, value in dynamodb_object.items()}
910

10-
def parse_pair(type_value_pair): # noqa: pylint - too-many-return-statements
11-
data_type = list(type_value_pair.keys())[0]
1211

13-
if data_type in ('S', 'SS', 'BOOL'):
14-
return type_value_pair[data_type]
12+
# pylint:disable=too-many-return-statements
13+
def _parse_property(prop: dict) -> Any:
14+
data_type = next(iter(prop))
15+
value = prop.get(data_type, [])
1516

16-
if data_type == 'N':
17-
return to_num(type_value_pair[data_type])
17+
if data_type in {"S", "SS", "BOOL"}:
18+
return value
1819

19-
if data_type == 'NS':
20-
return [to_num(data) for data in type_value_pair[data_type]]
20+
if data_type == "N":
21+
return _to_num(value)
2122

22-
if data_type == 'B':
23-
return base64.decodebytes(bytes(type_value_pair[data_type], 'utf-8'))
23+
if data_type == "NS":
24+
return [_to_num(item) for item in value]
2425

25-
if data_type == 'BS':
26-
return [base64.decodebytes(bytes(data, 'utf-8')) for data in type_value_pair[data_type]]
26+
if data_type == "B":
27+
return _to_bytes(value)
2728

28-
if data_type == 'M':
29-
return parse(type_value_pair[data_type])
29+
if data_type == "BS":
30+
return [_to_bytes(item) for item in value]
3031

31-
if data_type == 'L':
32-
return [parse_pair(item) for item in type_value_pair[data_type]]
32+
if data_type == "M":
33+
return parse(value)
3334

34-
if data_type == 'NULL' and type_value_pair[data_type]:
35+
if data_type == "L":
36+
return [_parse_property(item) for item in value]
37+
38+
if data_type == "NULL" and value:
3539
return None
3640

37-
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))
3842

3943

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

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
coverage
22
prospector
33
bandit
4-
pylint-quotes
4+
pylint-quotes
5+
mypy

setup.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
from setuptools import setup, find_packages
22

3-
LONG_DESCRIPTION = open('README.md').read()
3+
LONG_DESCRIPTION = open("README.md").read()
44

5-
setup(name='aws-dynamodb-parser',
6-
version='0.1',
7-
description='AWS DynamoDB utility for parsing DynamoDB responses',
8-
long_description=LONG_DESCRIPTION,
9-
long_description_content_type="text/markdown",
10-
url='https://github.com/gridsmartercities/aws-dynamodb-parser',
11-
author='Grid Smarter Cities',
12-
author_email='[email protected]',
13-
license='MIT',
14-
classifiers=['Intended Audience :: Developers',
15-
'Development Status :: 3 - Alpha',
16-
'Programming Language :: Python :: 3',
17-
'License :: OSI Approved :: MIT License',
18-
'Operating System :: OS Independent',
19-
'Natural Language :: English'
20-
],
21-
keywords='aws lambda decorator',
22-
packages=find_packages(exclude=('tests')),
23-
zip_safe=False
24-
)
5+
setup(
6+
name="aws-dynamodb-parser",
7+
version="0.1.2",
8+
description="AWS DynamoDB utility for parsing DynamoDB responses",
9+
long_description=LONG_DESCRIPTION,
10+
long_description_content_type="text/markdown",
11+
url="https://github.com/gridsmartercities/aws-dynamodb-parser",
12+
author="Grid Smarter Cities",
13+
author_email="[email protected]",
14+
license="MIT",
15+
classifiers=[
16+
"Intended Audience :: Developers",
17+
"Development Status :: 3 - Alpha",
18+
"Programming Language :: Python :: 3",
19+
"License :: OSI Approved :: MIT License",
20+
"Operating System :: OS Independent",
21+
"Natural Language :: English"
22+
],
23+
keywords="aws lambda decorator",
24+
packages=find_packages(exclude=("tests")),
25+
zip_safe=False
26+
)

0 commit comments

Comments
 (0)