python - OrderedDict for Parsimonious -


i'm using parsimonious parse csv. problem output generated not coming out in order expected. example, if input string

load,file,sample  

then i'd expect get:

import database sample 

what instead is:

from sample import database 

this consistent problem every input try: first token last item in entry ordereddict can't figure out why.

here's code:

from parsimonious.grammar import grammar parsimonious.nodes import nodevisitor collections import ordereddict  class entryparser(nodevisitor):     def __init__(self, grammar, text):         self.entry = ordereddict()         ast = grammar(grammar).parse(text)         self.visit(ast)     def visit_alt(self, n, vc):         self.entry['alt'] = "alter "     def visit_load(self, n, vc):         self.entry['load'] = "import database "     def visit_app(self, n, vc):         self.entry['app'] = "application "     def visit_db(self, n, vc):         self.entry['db'] = "database "     def visit_filter(self, n, vc):         self.entry['filter'] = "filter "     def visit_group(self, n, vc):         self.entry['group'] = "group "     def visit_obj(self, n, vc):         self.entry['obj'] = "object "     def visit_trigger(self, n, vc):         self.entry['trigger'] = "trigger "     def visit_user(self, n, vc):         self.entry['user'] = "user "     def visit_sql(self, n, vc):         self.entry['sql'] = "connect "     def visit_file(self, n, vc):         self.entry['file'] = "from "     def visit_dbname(self, n, vc):         self.entry['dbname'] = n.text + " "     def generic_visit(self, n, vc):         pass  grammar = """\ ts0 = alt / load sep = "," alt = "alt" sep altdomain altdomain = app / db / filter / group / obj / trigger / user load = "load" sep loaddomain loaddomain = (sql / file) sep dbname sql = "sql" file = "file" app = "app" db = "db" filter = "filter" group = "group" obj = "object" trigger = "trigger" user = "user" dbname = ~"[a-z]+" """  text = """\ alt,filter alt,app alt,db alt,group, alt,object alt,trigger alt,user load,sql,sample load,file,sample """  line in text.splitlines():     v in entryparser(grammar, line).entry.values():         print(v, end="")     print('\n') 

i think ordereddict causing problems. use regular dictionary instead {}.

basically change self.entry = ordereddict() self.entry = {}

also way grammar based parsing works, first innermost element matched, move towards outer matched rules(in terms of returning value).

so in order proper order right, have use stack(or use regular list , inverse array).


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -