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
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'])
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())