changeset 10:85c9aa9d90c5

implement refactor and present results, to output to csv
author DaveM
date Mon, 22 Jan 2018 22:31:20 +0000
parents 27c9ee2ea187
children 903559cb34d0
files V3/runme.py timesheet.xlsx
diffstat 2 files changed, 60 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/V3/runme.py	Mon Jan 22 09:21:30 2018 +0000
+++ b/V3/runme.py	Mon Jan 22 22:31:20 2018 +0000
@@ -58,9 +58,13 @@
 		del data['COB']
 		del data['pCOB']
 		del data['horiscope']
-	keys = data[0].keys()
+	# keys = data[0].keys()
+	keys = []
+	for d in data:
+		keys = keys + d.keys()
+	keys = sorted(uniqueList(keys))
 	with open(filename,'w') as stream:
-		dict_writer = csv.DictWriter(stream, keys)
+		dict_writer = csv.DictWriter(stream, keys, extrasaction='ignore')
 		dict_writer.writeheader()
 		dict_writer.writerows(data)
 
@@ -100,12 +104,62 @@
 				person[d] = person['horiscope'][d]
 			horiscopeList.append(person)
 			if saveFile is not None:
-				savePick(saveFilen,person)
+				savePick(saveFile,horiscopeList)
 	return horiscopeList
 	# savePick(pickFile,person)
 	# savePick('2'+pickFile,horiscopeList)
 	# printToFile('final_'+outFile,horiscopeList)
 
+def printDict(d):
+	for d_ in d:
+		print (d,d_)
+
+def refactorHoriscope(hor):
+	d = {}
+	d['ID'] = hor['ID']
+	for h in hor['horiscope']:
+		hs = sorted(h)
+		d[(hs[0], hs[1], hor['horiscope'][h][0])] = 1
+		d[(hs[0], hs[1])] = float(str(hor['horiscope'][h][1]) + '.' + str(hor['horiscope'][h][2]))
+	return d
+
+def uniqueList(seq): 
+   # order preserving
+   noDupes = []
+   [noDupes.append(i) for i in seq if not noDupes.count(i)]
+   return noDupes
+
+def merge_two_dicts(x, y):
+    z = x.copy()   # start with x's keys and values
+    z.update(y)    # modifies z with y's keys and values & returns None
+    return z
+
+def findMissing(unique,keyList):
+	missing = []
+	for u in unique:
+		if u not in keyList:
+			missing.append(u)
+	return u
+
+def presentResults(saveFile):
+	data = []
+	data2 = []
+	hlist = loadPick(saveFile)
+	keyList = []
+	for h in hlist:
+		d = refactorHoriscope(h)
+		keyList.append(d.keys())
+		data.append(d)
+	uniqueKeys = uniqueList(keyList)
+	# for da in data:
+	# 	missingKeys = findMissing(uniqueKeys,da.keys())
+	# 	# pdb.set_trace()
+	# 	d2 = dict(zip(missingKeys,[0]*len(missingKeys)))
+	# 	da = merge_two_dicts(da,d2)
+	# 	data2.append(da)
+	return data
+		
+
 def testMain():
 	people = dp.parseCSV('individuals.csv')
 
@@ -119,7 +173,9 @@
 	else:
 		print 'read in ' + pickFile
 		people = loadPick(pickFile)
-	parseHoriscope(people)
+	parseSaveFile = pickFile.split('.')[0]+'_collect.pick'
+	parseHoriscope(people,parseSaveFile)
+	presentResults(parseSaveFile)
 	
 
 if __name__ == "__main__":
Binary file timesheet.xlsx has changed