In [1]:
# To install the most recent version
!pip install git+https://github.com/msramalho/pyhparser --upgrade
In [0]:
from pyhparser import Pyhparser, readFile
In [3]:
inputVar = "10" #if the data is in a file do readFile("input.txt")
parserVar = "(int, n)" #if the parser is in a file do readFile("parser.txt")
p = Pyhparser(inputVar, parserVar) #create a pyhparser instance
p.parse() #execute the parsing
p.printRecursive(p.parserRead) #display the parse grammar hierarchy
tempGlobals = p.getVariables() #get the values of the created variables
for key, value in tempGlobals.items():
globals()[key] = value #make the created var acessible from every scope
print(n) #print the value of n, the parsed variable
In [0]:
inputText = """
3 Bartholomew JoJo Simpson
101 120 5455
Andrew American
Bernard Bolivian
Carl Canadian
10 11 12
20 21 22
30 31 32
69 lol
169 lel
333 threeHundredAndThirtyThree
666 sixHundredAndSixtySix
this is my first tuple
5
3 limited to three
2 only two
2 two more
6 what a big old string list
8 this sentence is the biggest of them all
10.23 55
3.141592653 70 1300 veryNice
3.141592653 70 4 string with four words
"""
In [0]:
parserText = """
(int, n) (str, name, {n})
[list, {n}, (int), myInts]
[list, {n}, (str, 2), Names]
[list, {n},
[list, {n}, (int)]
,Numbers]
{(int), (str), myDicts, 2}
[list, 2, {(int), (str)}, myDictsList]
[tuple, 5, (str), myTuple]
(int, total)
[list, {total}, {(int, sizeOfLine), (str, {sizeOfLine})}, sizesList]
#classes
[class, Complex, {realpart: (float), imagpart: (int)}, cn1]
[class, Complex, {realpart: (float), imagpart: (int), special: {(int), (str)}}, cn2]
[class, Complex, {realpart: (float), imagpart: (int), special: {(int, myInt), (str, {myInt})}}, cn3]
"""
In [0]:
class Complex:
def __init__(self, realpart, imagpart, special = "not special"):
self.realpart = realpart
self.imagpart = imagpart
self.special = special
def __str__(self):
return ("%s Is the special of %di + %d" % (self.special, self.realpart, self.imagpart))
In [7]:
p = Pyhparser(inputText, parserText, [Complex]) #create a pyhparser instance
p.parse() #execute the parsing
#p.printRecursive(p.parserRead) #display the parse grammar hierarchy
tempGlobals = p.getVariables() #get the values of the created variables
for key, value in tempGlobals.items():
globals()[key] = value #make the created var acessible from every scope
print(cn3)