From 72c4746578042edce393bbf913720ea7d3e4b65c Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Tue, 9 Jun 2020 12:05:36 +0200 Subject: [PATCH] Add function to convert the rename property to a python dict Probably less error prone, and I can remove code duplication. --- relational/parser.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/relational/parser.py b/relational/parser.py index 7f0b682..593272e 100644 --- a/relational/parser.py +++ b/relational/parser.py @@ -24,7 +24,7 @@ # # Language definition here: # http://ltworf.github.io/relational/grammar.html -from typing import Optional, Union, List, Any +from typing import Optional, Union, List, Any, Dict from dataclasses import dataclass from relational import rtypes @@ -233,13 +233,24 @@ class Unary(Node): if self.name == PROJECTION: prop = '\"%s\"' % prop.replace(' ', '').replace(',', '\",\"') elif self.name == RENAME: - prop = '{\"%s\"}' % prop.replace( - ',', '\",\"').replace(ARROW, '\":\"').replace(' ', '') + prop = repr(self.rename_dict()) else: # Selection prop = repr(prop) return '%s.%s(%s)' % (self.child._toPython(), op_functions[self.name], prop) + def rename_dict(self) -> Dict[str, str]: + ''' + Returns the dictionary that the rename operation wants + ''' + if self.name != RENAME: + raise ValueError('This is only supported on rename nodes') + r = {} + for i in self.prop.split(','): + q = i.split(ARROW) + r[q[0].strip()] = q[1].strip() + return r + def parse_tokens(expression: List[Union[list, str]]) -> Node: