Installing Pyhparser

Pyhparser.


In [1]:
# To install the most recent version
!pip install git+https://github.com/msramalho/pyhparser --upgrade


Collecting git+https://github.com/msramalho/pyhparser
  Cloning https://github.com/msramalho/pyhparser to /tmp/pip-todpgknc-build
Requirement already up-to-date: pyparsing in /usr/local/lib/python3.6/dist-packages (from pyhparser==1.0)
Installing collected packages: pyhparser
  Running setup.py install for pyhparser ... - done
Successfully installed pyhparser-1.0

Import pyhparser

And one useful function that comes with it (readFile)


In [0]:
from pyhparser import Pyhparser, readFile

Hello World

Readin an int into a variable n that can later be used


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


primitive: ['int', 'n']
    Leaf: int
    Leaf: n
10

Multiple data types example

Requires:

  • Input data
  • Parser format
  • Python code that invokes Pyhparser

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]
"""

Define the Complex class used to parse the data


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)


{4: 'string with four words'} Is the special of 3i + 70