Parser scratchpad

This notebook is used to prototype and tweak requirements parser


In [9]:
# basic data parsing, load everything from the root
import yaml
from pprint import pprint
import loader



#pprint(data['requirements'])

In [2]:
# requirement class

class Requirement():
    
    def __init__(self,tag,parent,properties):
        """ 
        tag: identifier, parent: parent requirement tag, properties: dict of parameters 
        """
        
        
        self.tag = tag # unique identifier
        self.parent = parent # parent requirement
        
        if 'requirements' in properties.keys():
            self.children = list(properties['requirements'].keys())
        else:
            self.children = None
            
    
    def __repr__(self):
        return '%s parent:%s children:%s' % (self.tag,self.parent, str(self.children))

In [28]:
# walk the tree

fName = '../requirements/simpleSE.yml'
data = yaml.load(open(fName,'r'),loader.Loader)


def walk_gen(d, level=0, parent=None, fcn=None):
    """
    tree walker.
    
    Parameters
    -----------
    d : dict
        dictionary to iterate though
    level : int
        iteration level
    parent : tag (default None)
        used for labeling parent and passing data through iterations
    fcn : function
        optional function to pass dict through before returning data. 
        could be done with a decorator.
    
    """
   
    for k in sorted(d.keys()):
        v = d[k]
    
        #yield {'id':k, 'parent': parent}
        result = dict(tag=k,parent=parent,values=v)
        
        print(k, 'level ', level)
        
        if fcn is None:
            yield result
        else:
           
            yield fcn(result)
        
        if 'requirements' in v.keys():
            print('deeper... into ', k)
            for r in walk_gen(v['requirements'],level=level+1, parent=k) :
                yield r
            print('...and back up')
            

#for r in walk_gen(testDict):
#    print(r)

def testFcn(args):
    #print(args['tag'])
    pass
    
for r in walk_gen(data['requirements'], fcn=testFcn):
    
    pass


A01 level  0
A02 level  0
deeper... into  A02
R006 level  1
...and back up
A03 level  0
deeper... into  A03
R008 level  1
deeper... into  R008
R008a level  2
R008b level  2
R008c level  2
R008d level  2
...and back up
R009 level  1
deeper... into  R009
R009a level  2
R009b level  2
...and back up
...and back up
A04 level  0
A05 level  0
deeper... into  A05
R004 level  1
...and back up
A06 level  0
A07 level  0
deeper... into  A07
R003 level  1
...and back up
A08 level  0
deeper... into  A08
R004a level  1
...and back up
A09 level  0
deeper... into  A09
R007 level  1
...and back up
A10 level  0
deeper... into  A10
R008 level  1
...and back up
yaml.dump(data,open('dump.yml','w'))

In [ ]:
#  attempt to walk the tree
# recursive stuff here, makes my head spin 8-)

# todo: maybe rewrite this into generator. but this also works...
reqs = {}
parent = 'ROOT'

testDict = {'0':{'requirements':{'01':{'requirements':{'001':{},'002':{}}},'02':{}}}}


def walk(d):
 
    for k,v in d.items():
        print(k)
        #reqs[k] = {'req':v['req'], 'parent': parent}
        if 'requirements' in v.keys():
            
            print('Children:', list(v['requirements'].keys()))
            #parent = k
            walk(v['requirements']) 

walk(testDict)  
print('-------------------------')
walk(data['requirements'])
%qtconsole

In [ ]:
# check how to dump structures

req = [ dict(object='requirement',txt='text %i' % i) for i in range(3)]

print(yaml.dump(req))

In [ ]:
d = dict(a=1,b=2,c=3)
sorted(d.keys())