Date Tags python

Recently I have been playing around with a parsing library for Python called Parsimonious. So far, I have been very impressed with it. Examples can be a bit hard to find; the following is a simple CSV parser:

from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor

class EntryParser(NodeVisitor):
    def __init__(self, grammar, text):
        self.entry = {}
        ast = Grammar(grammar).parse(text)
        self.visit(ast)
    def visit_name(self, n, vc):
        self.entry['name'] = n.text
    def visit_gender(self, n, vc):
        self.entry['gender'] = n.text
    def visit_age(self, n, vc):
        self.entry['age'] = n.text
    def generic_visit(self, n, vc):
        pass

grammar = """\
entry = name sep gender? (sep age)?
sep = ws "," ws
ws = " "*
name = ~"[A-z]*"
gender = "male" / "female"
age = ~"[0-9]*"
"""

text = """\
Bob, male, 26
Kim,female,30
Joe,male
"""

for line in text.splitlines():
    print EntryParser(grammar, line).entry

The resulting output is the following:

{'gender': 'male', 'age': '26', 'name': 'Bob'}
{'gender': 'female', 'age': '30', 'name': 'Kim'}
{'gender': 'male', 'name': 'Joe'}

Hi, I am Jeff Rimko!
A computer engineer and software developer in the greater Pittsburgh, Pennsylvania area.