From d494996b82d985aca75ce720fa4fa203f352c1cf Mon Sep 17 00:00:00 2001 From: naath Date: Sat, 29 Mar 2014 21:07:00 +0000 Subject: [PATCH] refactoring askQuestion --- familyTree/askQuestion.py | 61 ++++++++++++++++++++++----------------- familyTree/printLists.py | 7 +++-- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/familyTree/askQuestion.py b/familyTree/askQuestion.py index 502bfd5..82684ac 100755 --- a/familyTree/askQuestion.py +++ b/familyTree/askQuestion.py @@ -26,16 +26,19 @@ def is_number(s): except ValueError: return 0 -def print_relation(relationship,s,t,newLine): +def print_tagged_query(relationship,s,t,newLine): mine = '' for row in run_query(s,t): - mine = mine + print_none_value(relationship,row,newLine) + mine = mine + print_tagged_name(relationship,row,newLine) return mine +def base_url(): + return "http://www.chiark.greenend.org.uk/ucgi/~naath/" + def relationship_html(ID,ID2,newLine): if newLine=='
': relationship = common_ancestors(ID,ID2,newLine)[2] - url="http://www.chiark.greenend.org.uk/ucgi/~naath/ancestors.py?ID="+ID+"&ID2="\ + url=base_url()+"ancestors.py?ID="+ID+"&ID2="\ +str(ID2) return relationship + ': Common Ancestors'+newLine else: @@ -43,22 +46,26 @@ def relationship_html(ID,ID2,newLine): def terr_html(terr,newLine): if newLine=='
': - url = "http://www.chiark.greenend.org.uk/ucgi/~naath/territory.py?terr=" + url = base_url()+"territory.py?terr=" return ''+terr+'' else: return terr -def row_html(row,html): +def name_html(row,html): + if html=='
': + html=1 + elif html=='\n': + html=0 if html == 1: - url = "http://www.chiark.greenend.org.uk/ucgi/~naath/person.py?ID=" + str(row[1]) + url = base_url()+"person.py?ID=" + str(row[1]) name = row[0] - return ""+name+"" + return ""+str(name)+"" if row[0] == None: return row[1] else: return row[0] + "," +str(row[1]) -def print_none_value(relationship,row,newLine): +def print_tagged_name(relationship,row,newLine): if row[0]==None: out = relationship + " not yet entered: " + row[1] else: @@ -67,9 +74,9 @@ def print_none_value(relationship,row,newLine): else: html=0 if relationship =='': - out = row_html(row,html) + ' ' + out = name_html(row,html) + ' ' else: - out = relationship + ": " + row_html(row,html) + out = relationship + ": " + name_html(row,html) return out + newLine def list_territories(newLine): @@ -107,7 +114,7 @@ def list_people(newLine): out = out + ' century:' + newLine - out = out + print_none_value('',row,newLine) + out = out + name_html(row,newLine) +newLine year = row[2] return out @@ -141,7 +148,7 @@ def all_ancestors(personID,newLine): out = "Ancestors of " for row in run_query(t,id): - out = out + print_none_value('',row,newLine) + out = out + name_html(row,newLine)+newLine while len(ancestors)>0: level = level+1 @@ -150,7 +157,7 @@ def all_ancestors(personID,newLine): for ancestor in ancestors: id = (ancestor,) for row in run_query(s,id): - out = out + print_none_value('',row,newLine) + out = out + name_html(row,newLine)+newLine if row[1] not in allAncestors and is_number(row[1])!=0: newA.append(row[1]) allAncestors.append(row[1]) @@ -171,7 +178,7 @@ def common_ancestors(IDA, IDB,newLine): for id in (IDA,IDB): t = (id,) for row in run_query(s,t): - out = out + print_none_value('',row,newLine) + out = out + name_html(row,newLine)+newLine names.append(row[0]) if id==IDA: out = out + 'and' @@ -219,7 +226,7 @@ def common_ancestors(IDA, IDB,newLine): return [out, common,related] - out = out + print_relation('',s,common,newLine) + out = out + print_tagged_query('',s,common,newLine) indexA=[] indexB=[] @@ -239,7 +246,7 @@ def common_ancestors(IDA, IDB,newLine): out = out + newLine + 'Most Recent Common Ancestors:' + newLine for a in indexA: t = (common[a],) - out = out + print_relation('',s,t,newLine) + out = out + print_tagged_query('',s,t,newLine) if a!=indexA[-1]: out = out + 'and' + newLine @@ -247,19 +254,19 @@ def common_ancestors(IDA, IDB,newLine): if len(indexA) >1: out = out + 's' - out = out + ' of ' + print_none_value('',[names[0],IDA],newLine) + out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine out = out + newLine for b in indexB: t = (common[b],) - out = out + print_relation('',s,t,newLine) + out = out + print_tagged_query('',s,t,newLine) if b!=indexB[-1]: out = out + 'and' + newLine out = out + parent_level(bLevels[indexB[0]]) if len(indexB)>1: out = out + 's' - out = out + ' of ' + print_none_value('',[names[1],IDB],newLine) + out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine al = aLevels[indexA[0]] @@ -353,8 +360,8 @@ def rulers_of(aTerritory,newLine): out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine thisT = '' - thisT = thisT +print_none_value('',row,newLine) - thisT = thisT[:-4] + ' from ' + str(row[2])+' to '+str(row[3]) + newLine + thisT = thisT +name_html(row,newLine) + thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine last = row[4] out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT @@ -370,7 +377,7 @@ def person_info(personID,newLine): s = "SELECT * FROM people WHERE ID = ?" for row in run_query(s,t): output = output + 'ID: '+str(row[0]) +newLine - output = output + 'Name: '+print_none_value('',[row[1], row[0]],newLine) +newLine + output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine output = output + 'Born: '+row[3] + newLine output = output + 'Died: '+row[5] + newLine @@ -401,7 +408,7 @@ def person_info(personID,newLine): +" ON parents.parentID = people.ID"\ +" WHERE parents.ID = ?" for row in run_query(s,t): - output = output + print_none_value('Parent',row,newLine) + output = output + print_tagged_name('Parent',row,newLine) #find spouses s = "SELECT people.NAME, marriages.IDb from"\ @@ -410,7 +417,7 @@ def person_info(personID,newLine): +" WHERE marriages.IDa = ?" for row in run_query(s,t): output = output + newLine - output = output + print_none_value('Spouse',row,newLine) + output = output + print_tagged_name('Spouse',row,newLine) output = output + relationship_html(personID,row[1],newLine) s = "SELECT people.NAME, marriages.IDa from"\ @@ -419,7 +426,7 @@ def person_info(personID,newLine): +" WHERE marriages.IDb = ?" for row in run_query(s,t): output = output + newLine - output = output + print_none_value('Spouse',row,newLine) + output = output + print_tagged_name('Spouse',row,newLine) output = output + relationship_html(personID,row[1],newLine) output = output + newLine @@ -431,7 +438,7 @@ def person_info(personID,newLine): +" WHERE parents.parentID = ?" for row in run_query(s,t): - output = output + print_none_value('Child',row,newLine) + output = output + print_tagged_name('Child',row,newLine) #find children's other parent u = "Select people.NAME, parents.parentID FROM"\ @@ -442,7 +449,7 @@ def person_info(personID,newLine): ids = (row[1],t[0]) for row in run_query(u,ids): - output = output + print_none_value('With',row,newLine) + output = output + print_tagged_name('With',row,newLine) output = output + newLine diff --git a/familyTree/printLists.py b/familyTree/printLists.py index f825b28..1b681e0 100755 --- a/familyTree/printLists.py +++ b/familyTree/printLists.py @@ -7,13 +7,14 @@ import sys conn = askQuestion.connect() -#o = askQuestion.count_names('\n') +o = askQuestion.count_names('\n') -#print o +print o o = askQuestion.list_territories('\n') -#o = askQuestion.list_people('\n') +print o +o = askQuestion.list_people('\n') print o -- 2.30.2