feilian 1.1.8__py3-none-any.whl → 1.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of feilian might be problematic. Click here for more details.
- feilian/__init__.py +2 -0
- feilian/_dist_ver.py +2 -2
- feilian/utils.py +64 -0
- {feilian-1.1.8.dist-info → feilian-1.1.9.dist-info}/METADATA +1 -1
- {feilian-1.1.8.dist-info → feilian-1.1.9.dist-info}/RECORD +7 -6
- {feilian-1.1.8.dist-info → feilian-1.1.9.dist-info}/WHEEL +0 -0
- {feilian-1.1.8.dist-info → feilian-1.1.9.dist-info}/top_level.txt +0 -0
feilian/__init__.py
CHANGED
|
@@ -6,6 +6,7 @@ from .dataframe import is_empty_text, is_nonempty_text, is_blank_text, is_non_bl
|
|
|
6
6
|
from .datetime import format_time, format_date
|
|
7
7
|
from .arg import ArgValueParser
|
|
8
8
|
from .json import read_json, save_json
|
|
9
|
+
from .utils import flatten_dict
|
|
9
10
|
from .version import __version__
|
|
10
11
|
|
|
11
12
|
__all__ = [
|
|
@@ -15,5 +16,6 @@ __all__ = [
|
|
|
15
16
|
'format_time', 'format_date',
|
|
16
17
|
'ArgValueParser',
|
|
17
18
|
'read_json', 'save_json',
|
|
19
|
+
'flatten_dict',
|
|
18
20
|
'__version__',
|
|
19
21
|
]
|
feilian/_dist_ver.py
CHANGED
feilian/utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
from typing import Dict, Any, Union, Collection
|
|
5
|
+
|
|
6
|
+
def flatten_dict(data: Dict[str, Any], prefix="", joiner=".",
|
|
7
|
+
exclude: Union[None, str, Collection[str]] = None,
|
|
8
|
+
frozen: Union[None, str, Collection[str]] = None,
|
|
9
|
+
empty_as_default=False, empty_value=None,
|
|
10
|
+
res: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
11
|
+
"""
|
|
12
|
+
flatten dict as a flat one layer dict
|
|
13
|
+
:param data: origin dict
|
|
14
|
+
:param prefix: prefix for key in the dict
|
|
15
|
+
:param joiner: join symbol for different layer key
|
|
16
|
+
:param exclude: prefix to be excluded from result
|
|
17
|
+
:param frozen: keys not to be flattened
|
|
18
|
+
:param empty_as_default: should set a default value if value is an empty dict
|
|
19
|
+
:param empty_value: if `empty_as_default` is `True`, used as the default value for empty dict
|
|
20
|
+
:param res: the result flat layer dict, create a new one if not given.
|
|
21
|
+
"""
|
|
22
|
+
if res is None:
|
|
23
|
+
res = {}
|
|
24
|
+
if isinstance(exclude, str):
|
|
25
|
+
exclude = {exclude}
|
|
26
|
+
if isinstance(frozen, str):
|
|
27
|
+
frozen = {frozen}
|
|
28
|
+
|
|
29
|
+
# all keys are start with the prefix, ignore data
|
|
30
|
+
if exclude and prefix in exclude:
|
|
31
|
+
return res
|
|
32
|
+
|
|
33
|
+
# all keys in data should be frozen
|
|
34
|
+
if frozen and prefix in frozen:
|
|
35
|
+
for k, v in data.items():
|
|
36
|
+
res[prefix+k] = v
|
|
37
|
+
return res
|
|
38
|
+
|
|
39
|
+
for k, v in data.items():
|
|
40
|
+
k = prefix + k
|
|
41
|
+
|
|
42
|
+
if exclude and k in exclude:
|
|
43
|
+
# only the key should be excluded
|
|
44
|
+
continue
|
|
45
|
+
|
|
46
|
+
if frozen and k in frozen:
|
|
47
|
+
# frozen key, keep it as original value
|
|
48
|
+
res[k] = v
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
if isinstance(v, dict):
|
|
52
|
+
if len(v) == 0:
|
|
53
|
+
# empty dict, set as default value if set
|
|
54
|
+
if empty_as_default:
|
|
55
|
+
res[k] = empty_value
|
|
56
|
+
else:
|
|
57
|
+
# value is a dict, flatten recursively
|
|
58
|
+
flatten_dict(v, prefix=k+joiner, joiner=joiner, exclude=exclude, frozen=frozen, res=res)
|
|
59
|
+
else:
|
|
60
|
+
# normal value, keep it as original value
|
|
61
|
+
res[k] = v
|
|
62
|
+
|
|
63
|
+
return res
|
|
64
|
+
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
feilian/__init__.py,sha256=
|
|
2
|
-
feilian/_dist_ver.py,sha256=
|
|
1
|
+
feilian/__init__.py,sha256=Flac-edWcAozHlL4aAdSJX0rphgSdplFrUUlkr5q7ro,815
|
|
2
|
+
feilian/_dist_ver.py,sha256=gRZX3slfIQ_HsndEWgYKrFuTPKqi9wcoGNPZZAaGPTI,148
|
|
3
3
|
feilian/arg.py,sha256=n2nIcmC_3rb9A6BOzm9C5z3-T4lnubaGzH2sFhtqwZQ,8402
|
|
4
4
|
feilian/dataframe.py,sha256=G7Ai_JsMS7kNfqRNptqGOOxGWjyYwcJrT73IttDO1vo,10653
|
|
5
5
|
feilian/datetime.py,sha256=IONvWhLeGEy9IVe6GWKEW3FhrfRrShyhGP8-RTf9r3c,763
|
|
6
6
|
feilian/io.py,sha256=aYN3QwWcLoRKzhGMNutqdkmxArVcXfeWXzxCB07LcFc,155
|
|
7
7
|
feilian/json.py,sha256=PSjDJ3MCdolKwfAOmT9DuS8KnJZo9oGABKgJDduCliU,1187
|
|
8
8
|
feilian/string.py,sha256=G_X3dnR0Oxmi4hXF-6E5jm5M7GPjGoMYrSMyI1dj6Z4,370
|
|
9
|
+
feilian/utils.py,sha256=DqBKjpRBbSNipRDau9sYnoCfSDR_Of-xTOCoNxGWUJk,2180
|
|
9
10
|
feilian/version.py,sha256=oH_DvE7jRCWlCCX9SSadwxwRJXFas_rIisYLBGPYZn4,350
|
|
10
|
-
feilian-1.1.
|
|
11
|
-
feilian-1.1.
|
|
12
|
-
feilian-1.1.
|
|
13
|
-
feilian-1.1.
|
|
11
|
+
feilian-1.1.9.dist-info/METADATA,sha256=KA03FrgOCulwqzLrodlh76uKxVnISbxpchq8GGDQfvw,902
|
|
12
|
+
feilian-1.1.9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
13
|
+
feilian-1.1.9.dist-info/top_level.txt,sha256=1Q2-B6KJrcTr7drW_kik35PTVEUJLPP4wVrn0kYKwGw,8
|
|
14
|
+
feilian-1.1.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|