Created
July 17, 2015 07:54
-
-
Save skatenerd/b71671adbbdfd16a77c4 to your computer and use it in GitHub Desktop.
pyparsing is pretty cool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyparsing | |
class Word(object): | |
def __init__(self, body): | |
self.body = body | |
def __repr__(self): | |
return self.body[0] | |
class Tag(object): | |
def __init__(self, body): | |
self.body = body[1] | |
def __repr__(self): | |
return "{%s}"%self.body | |
class Newline(object): | |
def __init__(self, body): | |
self.body = body | |
def __repr__(self): | |
return "\n" | |
customword = pyparsing.Regex("[^<^>]+") | |
customword.setParseAction(Word) | |
tag="<"+customword+">" | |
tag.setParseAction(Tag) | |
newline = "<br" + pyparsing.Optional("/") + ">" | |
newline.setParseAction(Newline) | |
shit=pyparsing.OneOrMore(pyparsing.Or([newline, tag, customword])) | |
print "".join(map(repr, shit.parseString('foo <greet> <br/><br> baz <farewell>'))).format(greet='hi', farewell='bye') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment