python-table-processor 0.3.8__tar.gz → 0.3.10__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.
Files changed (23) hide show
  1. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/PKG-INFO +1 -1
  2. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/pyproject.toml +1 -1
  3. python_table_processor-0.3.10/table_processor/__init__.py +2 -0
  4. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/commands/merge_tables.py +13 -0
  5. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/actions.py +5 -2
  6. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/merge.py +42 -19
  7. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/types.py +1 -0
  8. python_table_processor-0.3.8/table_processor/__init__.py +0 -2
  9. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/LICENSE +0 -0
  10. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/README.md +0 -0
  11. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/cli.py +0 -0
  12. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/commands/convert_tables.py +0 -0
  13. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/config.py +0 -0
  14. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/constants.py +0 -0
  15. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/convert.py +0 -0
  16. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/assign_id.py +0 -0
  17. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/flatten_row.py +0 -0
  18. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/get_nested_field_value.py +0 -0
  19. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/nest_row.py +0 -0
  20. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/search_column_value.py +0 -0
  21. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/set_flat_field_value.py +0 -0
  22. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/set_nested_field_value.py +0 -0
  23. {python_table_processor-0.3.8 → python_table_processor-0.3.10}/table_processor/core/functions/set_row_value.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-table-processor
3
- Version: 0.3.8
3
+ Version: 0.3.10
4
4
  Summary: A table data processor
5
5
  Home-page: https://github.com/akivajp/python-table-processor
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-table-processor"
3
- version = "0.3.8"
3
+ version = "0.3.10"
4
4
  description = "A table data processor"
5
5
  authors = ["Akiva Miura <akiva.miura@gmail.com>"]
6
6
  license = "MIT"
@@ -0,0 +1,2 @@
1
+ __version__ = "0.3.10"
2
+ __version_tuple__ = (0, 3, 10)
@@ -17,6 +17,8 @@ def run(
17
17
  ignore_not_found=args.ignore_not_found,
18
18
  output_base_data_file=args.output_base_data_file,
19
19
  output_modified_data_file=args.output_modified_data_file,
20
+ output_remaining_data_file=args.output_remaining_data_file,
21
+ merge_fields=args.merge_fields,
20
22
  )
21
23
 
22
24
  def setup_parser(
@@ -60,4 +62,15 @@ def setup_parser(
60
62
  required=False,
61
63
  help='Path to output modified data file',
62
64
  )
65
+ parser.add_argument(
66
+ '--output-remaining-data-file', '--output-remaining', '--output-remain',
67
+ required=False,
68
+ help='Path to output remaining data file',
69
+ )
70
+ parser.add_argument(
71
+ '--merge-fields', '--merge-field', '--merge-keys', '--merge-key',
72
+ nargs='+',
73
+ required=False,
74
+ help='Fields to merge',
75
+ )
63
76
  parser.set_defaults(handler=run)
@@ -196,8 +196,10 @@ def setup_actions_with_args(
196
196
  ))
197
197
  continue
198
198
  if action_name == 'omit':
199
+ purge = options.get('purge', False)
199
200
  config.actions.append(OmitConfig(
200
201
  field = target,
202
+ purge = purge,
201
203
  ))
202
204
  continue
203
205
  if action_name == 'parse':
@@ -607,8 +609,9 @@ def omit_field(
607
609
  value, found = pop_row_value(row, config.field)
608
610
  if not found:
609
611
  return row
610
- if f'{STAGING_FIELD}.{config.field}' not in row.flat:
611
- set_row_staging_value(row, config.field, value)
612
+ if not config.purge:
613
+ if f'{STAGING_FIELD}.{config.field}' not in row.flat:
614
+ set_row_staging_value(row, config.field, value)
612
615
  return row
613
616
 
614
617
  def join_field(
@@ -39,6 +39,19 @@ from . convert import (
39
39
  save,
40
40
  )
41
41
 
42
+ def get_primary_key(
43
+ row: Mapping,
44
+ keys: list[str],
45
+ ):
46
+ list_keys = []
47
+ for key in keys:
48
+ value, found = search_column_value(row, key)
49
+ if not found:
50
+ raise KeyError(f'Column not found: {key}, existing columns: {row.keys()}')
51
+ list_keys.append(value)
52
+ primary_key = tuple(list_keys)
53
+ return primary_key
54
+
42
55
  def merge(
43
56
  previous_files: list[str],
44
57
  modification_files: list[str],
@@ -47,13 +60,17 @@ def merge(
47
60
  ignore_not_found: bool = False,
48
61
  output_base_data_file: str | None = None,
49
62
  output_modified_data_file: str | None = None,
63
+ output_remaining_data_file: str | None = None,
64
+ merge_fields: list[str] | None = None,
50
65
  ):
51
66
  ic.enable()
52
67
  ic()
53
68
  ic(previous_files)
54
69
  ic(modification_files)
55
70
  ic(keys)
56
- dict_key_to_row = {}
71
+ #dict_key_to_row = {}
72
+ dict_key_to_row = OrderedDict()
73
+ set_modified_keys = set()
57
74
  all_base_rows = []
58
75
  all_modified_rows = []
59
76
  list_ignored_keys = []
@@ -74,13 +91,7 @@ def merge(
74
91
  total=len(df),
75
92
  ):
76
93
  row = prepare_row(flat_row)
77
- list_keys = []
78
- for key in keys:
79
- value, found = search_column_value(row.nested, key)
80
- if not found:
81
- raise KeyError(f'Column not found: {key}, existing columns: {row.flat.keys()}')
82
- list_keys.append(value)
83
- primary_key = tuple(list_keys)
94
+ primary_key = get_primary_key(row.flat, keys)
84
95
  #ic(key)
85
96
  if not allow_duplicate_keys:
86
97
  if primary_key in dict_key_to_row:
@@ -104,13 +115,7 @@ def merge(
104
115
  total=len(df),
105
116
  ):
106
117
  row = prepare_row(flat_row)
107
- list_keys = []
108
- for key in keys:
109
- value, found = search_column_value(row.nested, key)
110
- if not found:
111
- raise KeyError(f'Column not found: {key}, existing columns: {row.flat.keys()}')
112
- list_keys.append(value)
113
- primary_key = tuple(list_keys)
118
+ primary_key = get_primary_key(row.flat, keys)
114
119
  #ic(key)
115
120
  #if key not in dict_key_to_row:
116
121
  # dict_key_to_row[key] = row
@@ -128,15 +133,22 @@ def merge(
128
133
  all_modified_rows.append(previous_row)
129
134
  #ic(previous_row)
130
135
  #ic(previous_row.flat)
131
- for key, value in row.flat.items():
132
- if key.startswith('__staging__.'):
133
- continue
136
+ if merge_fields is None:
137
+ merge_fields = []
138
+ for field in row.flat.keys():
139
+ if field.startswith('__staging__.'):
140
+ continue
141
+ merge_fields.append(field)
142
+ for field in merge_fields:
143
+ value, found = search_column_value(row.flat, field)
134
144
  #ic(key)
135
145
  #ic(key, value)
136
- set_row_value(previous_row, key, value)
146
+ if found:
147
+ set_row_value(previous_row, field, value)
137
148
  #ic(previous_row)
138
149
  #ic(previous_row.flat)
139
150
  #raise
151
+ set_modified_keys.add(primary_key)
140
152
  num_modified += 1
141
153
  ic(num_modified)
142
154
  if ignore_not_found:
@@ -147,6 +159,17 @@ def merge(
147
159
  ic('Saving to: ', output_base_data_file)
148
160
  save(all_df, output_base_data_file)
149
161
  if output_modified_data_file:
162
+ #all_modified_rows = []
163
+ #for key in set_modified_keys:
164
+ # all_modified_rows.append(dict_key_to_row[key])
150
165
  all_df = pd.DataFrame([row.flat for row in all_modified_rows])
151
166
  ic('Saving to: ', output_modified_data_file)
152
167
  save(all_df, output_modified_data_file)
168
+ if output_remaining_data_file:
169
+ remaining_rows = []
170
+ for key, row in dict_key_to_row.items():
171
+ if key not in set_modified_keys:
172
+ remaining_rows.append(row)
173
+ all_df = pd.DataFrame([row.flat for row in remaining_rows])
174
+ ic('Saving to: ', output_remaining_data_file)
175
+ save(all_df, output_remaining_data_file)
@@ -80,6 +80,7 @@ class JoinConfig:
80
80
  @dataclasses.dataclass
81
81
  class OmitConfig:
82
82
  field: str
83
+ purge: bool = False
83
84
 
84
85
  @dataclasses.dataclass
85
86
  class ParseConfig:
@@ -1,2 +0,0 @@
1
- __version__ = "0.3.8"
2
- __version_tuple__ = (0, 3, 8)