Skip to content

Commit 9f1d5b1

Browse files
committed
Add parse extra (optional dependencies).
1 parent 521e49a commit 9f1d5b1

File tree

6 files changed

+85
-10
lines changed

6 files changed

+85
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Here the hierarchy of possible installation targets available when running `pip
6161
- `[xls]`
6262
- `[xml]`
6363
- `[yaml]`
64+
- `[parse]`
6465
- `[s3]`
6566

6667
## Usage

benedict/dicts/parse/parse_util.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
from datetime import datetime
33
from decimal import Decimal, DecimalException
44

5-
import ftfy
6-
import phonenumbers
7-
from dateutil import parser as date_parser
8-
from MailChecker import MailChecker
9-
from phonenumbers import PhoneNumberFormat, phonenumberutil
5+
try:
6+
import ftfy
7+
import phonenumbers
8+
from dateutil import parser as date_parser
9+
from MailChecker import MailChecker
10+
from phonenumbers import PhoneNumberFormat, phonenumberutil
11+
12+
parse_installed = True
13+
except ModuleNotFoundError:
14+
parse_installed = False
15+
16+
1017
from slugify import slugify
1118

19+
from benedict.extras import require_parse
1220
from benedict.serializers import JSONSerializer
1321
from benedict.utils import type_util
1422

@@ -66,6 +74,7 @@ def _parse_datetime_from_timestamp(val):
6674

6775

6876
def parse_datetime(val, format=None):
77+
require_parse(installed=parse_installed)
6978
if type_util.is_datetime(val):
7079
return val
7180
s = str(val)
@@ -124,6 +133,7 @@ def _parse_email(val, check_blacklist=True):
124133

125134

126135
def parse_email(val, check_blacklist=True):
136+
require_parse(installed=parse_installed)
127137
return _parse_with(val, None, _parse_email, check_blacklist=check_blacklist)
128138

129139

@@ -183,6 +193,7 @@ def _parse_phonenumber(val, country_code=None):
183193

184194

185195
def parse_phonenumber(val, country_code=None):
196+
require_parse(installed=parse_installed)
186197
s = parse_str(val)
187198
if not s:
188199
return None
@@ -205,6 +216,7 @@ def parse_slug(val):
205216

206217

207218
def parse_str(val):
219+
require_parse(installed=parse_installed)
208220
if type_util.is_string(val):
209221
val = ftfy.fix_text(val)
210222
else:

benedict/extras.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from benedict.exceptions import ExtrasRequireModuleNotFoundError
22

33
__all__ = [
4+
"require_parse",
45
"require_s3",
56
"require_toml",
67
"require_xls",
@@ -14,6 +15,10 @@ def _require_optional_dependencies(*, target, installed):
1415
raise ExtrasRequireModuleNotFoundError(target=target)
1516

1617

18+
def require_parse(*, installed):
19+
_require_optional_dependencies(target="parse", installed=installed)
20+
21+
1722
def require_s3(*, installed):
1823
_require_optional_dependencies(target="s3", installed=installed)
1924

pyproject.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ classifiers = [
8787
"Topic :: Utilities",
8888
]
8989
dependencies = [
90-
"ftfy >= 6.0.0, < 7.0.0",
91-
"mailchecker >= 4.1.0, < 6.0.0",
92-
"phonenumbers >= 8.12.0, < 9.0.0",
93-
"python-dateutil >= 2.8.0, < 3.0.0",
9490
"python-fsutil >= 0.9.3, < 1.0.0",
9591
"python-slugify >= 7.0.0, < 9.0.0",
9692
"requests >= 2.26.0, < 3.0.0",
@@ -118,11 +114,17 @@ Twitter = "https://twitter.com/fabiocaccamo"
118114

119115
[project.optional-dependencies]
120116
all = [
121-
"python-benedict[io,s3]",
117+
"python-benedict[io,parse,s3]",
122118
]
123119
io = [
124120
"python-benedict[toml,xls,xml,yaml]",
125121
]
122+
parse = [
123+
"ftfy >= 6.0.0, < 7.0.0",
124+
"mailchecker >= 4.1.0, < 6.0.0",
125+
"phonenumbers >= 8.12.0, < 9.0.0",
126+
"python-dateutil >= 2.8.0, < 3.0.0",
127+
]
126128
s3 = [
127129
"boto3 >= 1.24.89, < 2.0.0",
128130
]

tests/dicts/parse/test_parse_dict.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import unittest
22
from datetime import datetime
33
from decimal import Decimal
4+
from unittest.mock import patch
45

56
from benedict.dicts.parse import ParseDict
7+
from benedict.exceptions import ExtrasRequireModuleNotFoundError
68

79

810
class parse_dict_test_case(unittest.TestCase):
@@ -185,6 +187,16 @@ def test_get_datetime_without_format(self):
185187
r = datetime(2019, 5, 1, 0, 0)
186188
self.assertEqual(b.get_datetime("a"), r)
187189

190+
@patch("benedict.dicts.parse.parse_util.parse_installed", False)
191+
def test_get_datetime_with_with_extra_not_installed(self):
192+
with self.assertRaises(ExtrasRequireModuleNotFoundError):
193+
d = {
194+
"a": "2019-05-01",
195+
}
196+
b = ParseDict(d)
197+
r = datetime(2019, 5, 1, 0, 0)
198+
self.assertEqual(b.get_datetime("a", format="%Y-%m-%d"), r)
199+
188200
def test_get_datetime_list(self):
189201
d = {
190202
"a": ["2019-05-01", "2018-12-31", "Hello World"],
@@ -335,6 +347,15 @@ def test_get_email(self):
335347
# invalid key
336348
self.assertEqual(b.get_email("e"), "")
337349

350+
@patch("benedict.dicts.parse.parse_util.parse_installed", False)
351+
def test_get_email_with_extra_not_installed(self):
352+
with self.assertRaises(ExtrasRequireModuleNotFoundError):
353+
d = {
354+
355+
}
356+
b = ParseDict(d)
357+
b.get_email("a")
358+
338359
def test_get_int(self):
339360
d = {
340361
"a": 1,
@@ -504,6 +525,15 @@ def test_get_phonenumber(self):
504525
p = b.get_phonenumber("z")
505526
self.assertEqual(p, {})
506527

528+
@patch("benedict.dicts.parse.parse_util.parse_installed", False)
529+
def test_get_phonenumber_with_extra_not_installed(self):
530+
with self.assertRaises(ExtrasRequireModuleNotFoundError):
531+
d = {
532+
"a": "3334445566",
533+
}
534+
b = ParseDict(d)
535+
b.get_phonenumber("a")
536+
507537
def test_get_slug(self):
508538
d = {
509539
"a": " Hello World ",
@@ -550,6 +580,15 @@ def test_get_str(self):
550580
self.assertEqual(b.get_str("b"), "Hello World")
551581
self.assertEqual(b.get_str("c"), "1")
552582

583+
@patch("benedict.dicts.parse.parse_util.parse_installed", False)
584+
def test_get_str_with_extra_not_installed(self):
585+
with self.assertRaises(ExtrasRequireModuleNotFoundError):
586+
d = {
587+
"a": "Hello World",
588+
}
589+
b = ParseDict(d)
590+
b.get_str("a")
591+
553592
def test_get_str_fix_encoding(self):
554593
d = {
555594
"a": "Sexâ\x80\x99n Drug",

tests/dicts/parse/test_parse_util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def test_parse_datetime(self):
3434
# TODO
3535
pass
3636

37+
def test_parse_datetime_with_extra_not_installed(self):
38+
# TODO
39+
pass
40+
3741
def test_parse_decimal(self):
3842
# TODO
3943
pass
@@ -50,6 +54,10 @@ def test_parse_email(self):
5054
# TODO
5155
pass
5256

57+
def test_parse_email_with_extra_not_installed(self):
58+
# TODO
59+
pass
60+
5361
def test_parse_int(self):
5462
# TODO
5563
pass
@@ -90,6 +98,10 @@ def test_parse_phonenumber(self):
9098
# TODO
9199
pass
92100

101+
def test_parse_phonenumber_with_extra_not_installed(self):
102+
# TODO
103+
pass
104+
93105
def test_parse_slug(self):
94106
# TODO
95107
pass
@@ -98,6 +110,10 @@ def test_parse_str(self):
98110
# TODO
99111
pass
100112

113+
def test_parse_str_with_extra_not_installed(self):
114+
# TODO
115+
pass
116+
101117
def test_parse_uuid(self):
102118
# TODO
103119
pass

0 commit comments

Comments
 (0)