update to use currencies info from wise camt statement#36
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the CAMT statement parser by introducing a more robust parse_entry method and improving currency detection logic, particularly for foreign currency transactions and exchange rates. Key feedback includes addressing a shallow copy bug where multiple transactions could share the same narration dictionary, preventing potential KeyError exceptions when removing the currency key, and optimizing XPath queries for better performance. Additionally, a minor PEP 8 formatting adjustment for method separation was recommended.
| ccy_nodes = node.xpath(".//ns:AmtDtls//@Ccy", namespaces={"ns": ns}) | ||
| for ccy in ccy_nodes: | ||
| if ccy != transaction.get("currency"): | ||
| val_node = node.xpath(f".//ns:AmtDtls//*[@Ccy='{ccy}']", | ||
| namespaces={"ns": ns}) | ||
| if val_node: | ||
| ntry_dtls_currency = ccy | ||
| currency_amount = float(val_node[0].text) | ||
| add_currency = True | ||
| break |
There was a problem hiding this comment.
This loop is inefficient because it performs a new XPath search (.//ns:AmtDtls//*[@Ccy='{ccy}']) for every currency attribute found. It is better to select the nodes that have the attribute directly and then access the attribute and text from those nodes.
ccy_nodes = node.xpath(".//ns:AmtDtls//*[@Ccy]", namespaces={"ns": ns})
for ccy_node in ccy_nodes:
ccy = ccy_node.get("Ccy")
if ccy != transaction.get("currency"):
ntry_dtls_currency = ccy
currency_amount = float(ccy_node.text)
add_currency = True
break| transaction.pop("currency") | ||
| self.generate_narration(transaction) | ||
| yield transaction | ||
| def add_value_from_node(self, ns, node, xpath_str, obj, attr_name, join_str=None): |
There was a problem hiding this comment.
According to PEP 8, method definitions inside a class should be separated by a single blank line. Please add a blank line before add_value_from_node.
def add_value_from_node(self, ns, node, xpath_str, obj, attr_name, join_str=None):References
- Method definitions inside a class are separated by a single blank line. (link)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
update the currencies function to retrieve from camt wise statement (already in prod)