Skip to content

Commit

Permalink
Added test and fixed leaf naming
Browse files Browse the repository at this point in the history
  • Loading branch information
phoughton committed Aug 14, 2024
1 parent e37cb72 commit fcb6e33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions pyiso20022/tools/camt053_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2853,16 +2853,17 @@

def _iso20022_term_translator(mnemonic):
new_name = ""
list_of_mnems = re.split(r'(?<=[a-z])(?=[A-Z])', mnemonic)
list_of_mnems = re.split(r'(?<=[a-z\/ ])(?=[ \/A-Z])', mnemonic)
for key in list_of_mnems:
new_name += mnemonics.get(key, key)
new_name += " "

return new_name.strip()
squashed = ' '.join(new_name.split())

return squashed.strip()

def _modify_key(key, translate=True):
clean_mnems = key.split('}')[-1]

def _modify_key(clean_mnems, translate=True):
if translate:
clean_mnems = _iso20022_term_translator(clean_mnems)
return clean_mnems
Expand All @@ -2871,7 +2872,7 @@ def _modify_key(key, translate=True):
def _parse_element(element, parent_name='', translate=True):
data_dict = {}
for child in element:
child_name = f"{parent_name}_{child.tag}" if parent_name else child.tag
child_name = f"{parent_name} / {child.tag}" if parent_name else child.tag

if len(child):
data_dict.update(_parse_element(child,
Expand All @@ -2894,7 +2895,15 @@ def camt053_to_df(xml_fname, translate=True):

root = etree.fromstring(xml_data)
data = []
elements = root.xpath('//*[local-name()="Ntry"]')

for elem in root.getiterator():
if not hasattr(elem.tag, 'find'):
continue
ind = elem.tag.find('}')
if ind > 0:
elem.tag = elem.tag[ind+1:]

elements = root.xpath('//Ntry')

for record in elements:
record_data = _parse_element(record, translate=translate)
Expand Down
4 changes: 2 additions & 2 deletions tests/tools_camt053_msgs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_pandas_df_camt053_001_02(entry_reference, msg_id):

df = camt053_to_df("example_files/gs_camt/camt053_001_02.xml")

result = df[df['Entry Reference'] == entry_reference]["Message Identification"]
result = df[df['Entry Reference'] == entry_reference]["Entry Details / Transaction Details / References / Message Identification"]

assert result.values[0] == msg_id

Expand All @@ -30,5 +30,5 @@ def test_excel_camt053_001_02(entry_reference, msg_id):

tmp_file.close()

result = df[df['Entry Reference'].astype(str) == str(entry_reference)]["Message Identification"]
result = df[df['Entry Reference'].astype(str) == str(entry_reference)]["Entry Details / Transaction Details / References / Message Identification"]
assert result.values[0] == msg_id

0 comments on commit fcb6e33

Please sign in to comment.