python-table-processor 0.3.2__tar.gz → 0.3.4__tar.gz
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.
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/PKG-INFO +1 -1
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/pyproject.toml +1 -1
- python_table_processor-0.3.4/table_processor/__init__.py +2 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/actions.py +108 -14
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/types.py +16 -1
- python_table_processor-0.3.2/table_processor/__init__.py +0 -2
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/LICENSE +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/README.md +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/cli.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/commands/convert_tables.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/commands/merge_tables.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/config.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/constants.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/convert.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/assign_id.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/flatten_row.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/get_nested_field_value.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/nest_row.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/search_column_value.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/set_flat_field_value.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/set_nested_field_value.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/functions/set_row_value.py +0 -0
- {python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/merge.py +0 -0
{python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/actions.py
RENAMED
|
@@ -29,6 +29,7 @@ from . types import (
|
|
|
29
29
|
AssignFormatConfig,
|
|
30
30
|
AssignIdConfig,
|
|
31
31
|
AssignLengthConfig,
|
|
32
|
+
CastConfig,
|
|
32
33
|
FilterConfig,
|
|
33
34
|
GlobalStatus,
|
|
34
35
|
JoinConfig,
|
|
@@ -147,6 +148,31 @@ def setup_actions_with_args(
|
|
|
147
148
|
source = source,
|
|
148
149
|
))
|
|
149
150
|
continue
|
|
151
|
+
if action_name == 'cast':
|
|
152
|
+
required = options.get('required', False)
|
|
153
|
+
as_type = options.get('as', 'literal')
|
|
154
|
+
if as_type in ['boolean']:
|
|
155
|
+
as_type = 'bool'
|
|
156
|
+
if as_type not in ['bool', 'int', 'float', 'str']:
|
|
157
|
+
raise ValueError(
|
|
158
|
+
f'Unsupported as type: {as_type}'
|
|
159
|
+
)
|
|
160
|
+
assign_default = False
|
|
161
|
+
default_value = None
|
|
162
|
+
if 'default' in options:
|
|
163
|
+
assign_default = True
|
|
164
|
+
default_value = options['default']
|
|
165
|
+
if default_value in ['None', 'none', 'Null', 'null']:
|
|
166
|
+
default_value = None
|
|
167
|
+
config.actions.append(CastConfig(
|
|
168
|
+
target = target,
|
|
169
|
+
source = source,
|
|
170
|
+
as_type = as_type,
|
|
171
|
+
required = required,
|
|
172
|
+
assign_default = assign_default,
|
|
173
|
+
default_value = default_value,
|
|
174
|
+
))
|
|
175
|
+
continue
|
|
150
176
|
if action_name == 'filter-empty':
|
|
151
177
|
config.actions.append(FilterConfig(
|
|
152
178
|
field = target,
|
|
@@ -177,15 +203,26 @@ def setup_actions_with_args(
|
|
|
177
203
|
if action_name == 'parse':
|
|
178
204
|
as_type = options.get('as', 'literal')
|
|
179
205
|
required = options.get('required', False)
|
|
180
|
-
if as_type
|
|
206
|
+
if as_type in ['boolean']:
|
|
207
|
+
as_type = 'bool'
|
|
208
|
+
if as_type not in ['bool', 'json', 'literal']:
|
|
181
209
|
raise ValueError(
|
|
182
210
|
f'Unsupported as type: {as_type}'
|
|
183
211
|
)
|
|
212
|
+
assign_default = False
|
|
213
|
+
default_value = None
|
|
214
|
+
if 'default' in options:
|
|
215
|
+
assign_default = True
|
|
216
|
+
default_value = options['default']
|
|
217
|
+
if default_value in ['None', 'none', 'Null', 'null']:
|
|
218
|
+
default_value = None
|
|
184
219
|
config.actions.append(ParseConfig(
|
|
185
220
|
target = target,
|
|
186
221
|
source = source,
|
|
187
222
|
as_type = as_type,
|
|
188
223
|
required = required,
|
|
224
|
+
assign_default = assign_default,
|
|
225
|
+
default_value = default_value,
|
|
189
226
|
))
|
|
190
227
|
continue
|
|
191
228
|
if action_name == 'parse-json':
|
|
@@ -312,6 +349,8 @@ def do_action(
|
|
|
312
349
|
return assign_id(status.id_context_map, row, action)
|
|
313
350
|
if isinstance(action, AssignLengthConfig):
|
|
314
351
|
return assign_length(row, action)
|
|
352
|
+
if isinstance(action, CastConfig):
|
|
353
|
+
return cast(row, action)
|
|
315
354
|
if isinstance(action, FilterConfig):
|
|
316
355
|
if filter_row(row, action):
|
|
317
356
|
return row
|
|
@@ -599,27 +638,48 @@ def parse(
|
|
|
599
638
|
f'Required field not found, field: {config.source}'
|
|
600
639
|
)
|
|
601
640
|
if found:
|
|
602
|
-
if
|
|
603
|
-
|
|
604
|
-
|
|
641
|
+
if config.as_type == 'literal':
|
|
642
|
+
try:
|
|
643
|
+
if type(value) is str:
|
|
605
644
|
parsed = ast.literal_eval(value)
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
645
|
+
else:
|
|
646
|
+
parsed = value
|
|
647
|
+
except:
|
|
648
|
+
raise ValueError(
|
|
649
|
+
f'Failed to parse literal: {value}'
|
|
650
|
+
)
|
|
651
|
+
elif config.as_type == 'json':
|
|
652
|
+
try:
|
|
653
|
+
if type(value) is str:
|
|
612
654
|
parsed = json.loads(value)
|
|
613
|
-
|
|
655
|
+
else:
|
|
656
|
+
parsed = value
|
|
657
|
+
except:
|
|
658
|
+
raise ValueError(
|
|
659
|
+
f'Failed to parse JSON: {value}'
|
|
660
|
+
)
|
|
661
|
+
elif config.as_type == 'bool':
|
|
662
|
+
if config.assign_default and value in [None, '']:
|
|
663
|
+
value = config.default_value
|
|
664
|
+
if type(value) is bool:
|
|
665
|
+
parsed = value
|
|
666
|
+
elif type(value) is str:
|
|
667
|
+
if value.lower() in ['true', 'yes', 'on', '1']:
|
|
668
|
+
parsed = True
|
|
669
|
+
elif value.lower() in ['false', 'no', 'off', '0']:
|
|
670
|
+
parsed = False
|
|
671
|
+
else:
|
|
614
672
|
raise ValueError(
|
|
615
|
-
f'Failed to parse
|
|
673
|
+
f'Failed to parse bool: {value}'
|
|
616
674
|
)
|
|
617
675
|
else:
|
|
618
676
|
raise ValueError(
|
|
619
|
-
f'
|
|
677
|
+
f'Failed to parse bool: {value}'
|
|
620
678
|
)
|
|
621
679
|
else:
|
|
622
|
-
|
|
680
|
+
raise ValueError(
|
|
681
|
+
f'Unsupported as type: {config.as_type}'
|
|
682
|
+
)
|
|
623
683
|
set_row_staging_value(row, config.target, parsed)
|
|
624
684
|
return row
|
|
625
685
|
|
|
@@ -653,3 +713,37 @@ def assign_length(
|
|
|
653
713
|
if found:
|
|
654
714
|
set_row_staging_value(row, config.target, len(value))
|
|
655
715
|
return row
|
|
716
|
+
|
|
717
|
+
def cast(
|
|
718
|
+
row: Row,
|
|
719
|
+
config: CastConfig,
|
|
720
|
+
):
|
|
721
|
+
value, found = search_column_value(row.nested, config.source)
|
|
722
|
+
if config.required:
|
|
723
|
+
if not found:
|
|
724
|
+
raise ValueError(
|
|
725
|
+
f'Required field not found, field: {config.source}'
|
|
726
|
+
)
|
|
727
|
+
if config.as_type == 'bool':
|
|
728
|
+
cast_func = bool
|
|
729
|
+
elif config.as_type == 'int':
|
|
730
|
+
cast_func = int
|
|
731
|
+
elif config.as_type == 'float':
|
|
732
|
+
cast_func = float
|
|
733
|
+
elif config.as_type == 'str':
|
|
734
|
+
cast_func = str
|
|
735
|
+
else:
|
|
736
|
+
raise ValueError(
|
|
737
|
+
f'Unsupported as type: {config.as_type}'
|
|
738
|
+
)
|
|
739
|
+
try:
|
|
740
|
+
casted = cast_func(value)
|
|
741
|
+
except:
|
|
742
|
+
if config.assign_default:
|
|
743
|
+
casted = config.default_value
|
|
744
|
+
else:
|
|
745
|
+
raise ValueError(
|
|
746
|
+
f'Failed to cast: {value}'
|
|
747
|
+
)
|
|
748
|
+
set_row_staging_value(row, config.target, casted)
|
|
749
|
+
return row
|
|
@@ -51,6 +51,17 @@ class AssignLengthConfig:
|
|
|
51
51
|
target: str
|
|
52
52
|
source: str
|
|
53
53
|
|
|
54
|
+
@dataclasses.dataclass
|
|
55
|
+
class CastConfig:
|
|
56
|
+
target: str
|
|
57
|
+
source: str
|
|
58
|
+
as_type: Literal[
|
|
59
|
+
'bool', 'int', 'float', 'str'
|
|
60
|
+
]
|
|
61
|
+
required: bool = False
|
|
62
|
+
assign_default: bool = False
|
|
63
|
+
default_value: Any = None
|
|
64
|
+
|
|
54
65
|
@dataclasses.dataclass
|
|
55
66
|
class FilterConfig:
|
|
56
67
|
field: str
|
|
@@ -74,8 +85,12 @@ class OmitConfig:
|
|
|
74
85
|
class ParseConfig:
|
|
75
86
|
target: str
|
|
76
87
|
source: str
|
|
77
|
-
as_type: Literal[
|
|
88
|
+
as_type: Literal[
|
|
89
|
+
'bool', 'json', 'literal'
|
|
90
|
+
]
|
|
78
91
|
required: bool = False
|
|
92
|
+
assign_default: bool = False
|
|
93
|
+
default_value: Any = None
|
|
79
94
|
|
|
80
95
|
@dataclasses.dataclass
|
|
81
96
|
class PickConfig:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/config.py
RENAMED
|
File without changes
|
{python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/constants.py
RENAMED
|
File without changes
|
{python_table_processor-0.3.2 → python_table_processor-0.3.4}/table_processor/core/convert.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|