Skip to content

Commit 41381b9

Browse files
committed
add data format checker
1 parent 707e7e8 commit 41381b9

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/pystrong/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .checker import check_data_types
12
from .decorators import check_arg_type, check_return_types
23
from .enforcer import InferredTypeEnforcer, TypeEnforcer
34

@@ -6,4 +7,5 @@
67
"InferredTypeEnforcer",
78
"check_return_types",
89
"check_arg_type",
10+
"check_data_types",
911
]

src/pystrong/checker.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from .exceptions import DataFormatCheckError
2+
3+
4+
def check_data_types(**kwargs):
5+
type_format = kwargs.get("type_format")
6+
data = kwargs.get("data")
7+
if not data or not type_format:
8+
raise DataFormatCheckError(
9+
"Arguments type_format and data are required. Example: check_data_types(data=[1,2,3], type_format=[int, int, int])"
10+
)
11+
if type(data) is not list:
12+
raise DataFormatCheckError("Please pass data and type_format as a list")
13+
14+
if len(data) != len(type_format):
15+
raise DataFormatCheckError("Length of arguments data and type format differ.")
16+
17+
checks = [type(datapoint) is _type for datapoint, _type in zip(data, type_format)]
18+
return all(checks)

src/pystrong/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
class DataFormatCheckError(Exception):
2+
pass
3+
4+
15
class InitError(Exception):
26
pass
37

tests/test_data_checker.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
from src.pystrong import check_data_types
3+
from src.pystrong.exceptions import DataFormatCheckError
4+
5+
6+
clean_data = [
7+
[1, "hello", {}],
8+
[1, "hello", {}],
9+
[1, "hello", {}],
10+
[1, "hello", {}],
11+
[1, "hello", {}],
12+
[1, "hello", {}],
13+
[1, "hello", {}],
14+
]
15+
dirty_data = [
16+
[1, "hello", {}],
17+
["hello", "hello", {}],
18+
[1, "hello", {}],
19+
[1, "hello", {}],
20+
[1, "hello", {}],
21+
[1, "hello", {}],
22+
[1, "hello", {}],
23+
]
24+
unequal_len_data = [
25+
[1, "hello", {}, 33, 45],
26+
[1, "hello", {}],
27+
[1, "hello", {}],
28+
[1, "hello", {}],
29+
[1, "hello", {}],
30+
[1, "hello", {}],
31+
[1, "hello", {}],
32+
]
33+
34+
clean_type_format = [int, str, dict]
35+
dirty_type_format = [int, str, str]
36+
unequal_len_type_format = [int, str, dict, list]
37+
38+
39+
def test_data_checker():
40+
success = all(
41+
[check_data_types(data=d, type_format=clean_type_format) for d in clean_data]
42+
)
43+
fail_dirty_data = all(
44+
[check_data_types(data=d, type_format=clean_type_format) for d in dirty_data]
45+
)
46+
fail_dirty_format = all(
47+
[check_data_types(data=d, type_format=dirty_type_format) for d in clean_data]
48+
)
49+
assert success
50+
assert not fail_dirty_data
51+
assert not fail_dirty_format
52+
with pytest.raises(DataFormatCheckError):
53+
fail_unequal_len_type_format = all(
54+
[
55+
check_data_types(data=d, type_format=unequal_len_type_format)
56+
for d in clean_data
57+
]
58+
)

0 commit comments

Comments
 (0)