From 0bba09c6961be426d77f64ad9d02230d317222dc Mon Sep 17 00:00:00 2001 From: naath Date: Fri, 18 Apr 2014 15:15:42 +0100 Subject: [PATCH] Change a = a+ to a+= and used better string formatting --- familyTree/askQuestion.py | 366 ++++++++++++++++++-------------------- 1 file changed, 175 insertions(+), 191 deletions(-) diff --git a/familyTree/askQuestion.py b/familyTree/askQuestion.py index d0380d7..8dbe0b1 100755 --- a/familyTree/askQuestion.py +++ b/familyTree/askQuestion.py @@ -49,7 +49,7 @@ def relationship_html(ID,ID2,newLine): relationship = common_ancestors(ID,ID2,newLine)[2] if relationship[-11:] != 'not related': - script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2) + script = "ancestors.py?ID=%s&ID2=%s" % (ID,ID2) url = link_Template.substitute\ (script = script,title = "Common ancestors"\ ,text = "Common ancestors") @@ -74,8 +74,8 @@ def terr_html(terr,newLine,start,stop): t = (terr,start) myTitle = '' for row in run_query(s,t): - myTitle = myTitle +"previous - " + row[0] \ - + ',' + str(row[1]) + myTitle += "previous - %s,%s" \ + % (row[0],row[1]) break u = "SELECT name,people.id"\ @@ -86,8 +86,8 @@ def terr_html(terr,newLine,start,stop): v = (terr,stop) for r in run_query(u,v): - myTitle = myTitle + ' ' +"next - " + r[0] \ - + ',' + str(r[1]) + myTitle += ' next - %s,%s'\ + %(r[0],r[1]) break myTitle = add_quotes(myTitle) @@ -109,59 +109,59 @@ def name_html(row,html): return row[0] else: if html==1: - script = "person.py?ID=" + str(row[1]) + script = "person.py?ID=%s" %(row[1]) name = row[0] return link_Template.substitute(script = script\ ,title = add_quotes(name),text = name) else: - return row[0] + "," +str(row[1]) + return "%s,%s" % (row[0],row[1]) def print_people(n): if n>1: - return ' people ' + return 'people' else: - return ' person ' + return 'person' +def print_children(n): + if n>1: + return 'children' + else: + return 'child' def print_age_child_count(row,newLine): if newLine == '
': - script = "listAge.py?age="+str(row[0]) + script = "listAge.py?age=%s" % (row[0]) link = link_Template.substitute(script = \ script, title = add_quotes(row[0]), text = row[0]) - out = str(row[1])+print_people(row[1]) - out = out + 'had children at age '+ link + newLine + out = '%s %s had children at age %s %s'\ + % (row[1],print_people(row[1]),link,newLine) return out else: return print_row(row,newLine) def print_age_death_count(row,newLine): if newLine =='
': - script = "listAgeDeath.py?age="+str(row[0]) + script = "listAgeDeath.py?age=%s" % (row[0]) link = link_Template.substitute(script = script,\ title = add_quotes(row[0]),text = row[0]) - out = str(row[1])+print_people(row[1]) - out = out + "died at age " + link + newLine + out = "%s %s died at age %s %s " \ + %(row[1],print_people(row[1]), link,newLine) return out else: return print_row(row,newLine) def print_name_count(row,newLine): if newLine=='
': - script = "name.py?name=" + row[0] + script = "name.py?name=%s" % (row[0]) link = link_Template.substitute(script =\ script, title = add_quotes(row[0]),text = row[0]) - return str(row[1]) + " people called "+link + newLine + return "%s people called %s%s"\ + %(row[1],link, newLine) else: return print_row(row,newLine) def print_children_count(row,newLine): - out = str(row[0]) - - if row[0]==1: - out = out + ' person ' - else: - out = out + ' people ' - out = out +'had ' + out = "%s %s had " % (row[0],print_people(row[0])) if newLine == '
': script = "listChildCount.py?nc="+str(row[1]) @@ -170,30 +170,24 @@ def print_children_count(row,newLine): else: link = str(row[1]) - out = out + link - - if row[1] == 1: - out = out + ' child ' - else: - out = out + ' children ' - - - out = out + newLine + out += "%s %s %s" % (link,print_children(row[1]),newLine) return out def print_tagged_name(relationship,row,newLine): if row[0]==None: out = relationship + " not yet entered: " + row[1] + out = "%s not yet entered: %s" % (relationship,row[1]) else: if newLine == '
': html = 1 else: html=0 if relationship =='': - out = name_html(row,html) + ' ' + out = '%s ' % (name_html(row,html)) else: out = relationship + ": " + name_html(row,html) + out = '%s: %s' % (relationship,name_html(row,html)) return out + newLine def month_numbers(monthN): @@ -229,14 +223,15 @@ def month_numbers(monthN): def ordinal_numbers(number): number = int(number) + out = '%d' % (number) if number % 10==1 and number/10 % 10 !=1: - out = str(number) +'st' + out +='st' elif number % 10==2 and number/10 % 10 !=1: - out = str(number) +'nd' + out +='nd' elif number % 10==3 and number/10 % 10 !=1: - out = str(number) +'rd' + out +='rd' else: - out = str(number) +'th' + out +='th' return out def list_territories(newLine): @@ -246,7 +241,7 @@ def list_territories(newLine): out = '' for row in run_query(s,()): - out =out + terr_html(row[0],newLine,0,0) +newLine + out += terr_html(row[0],newLine,0,0) +newLine return out def list_people_parents(): @@ -276,16 +271,15 @@ def list_people(newLine): out = out + 'born in unknown year:' +newLine for row in run_query(s,()): if row[2]!=0 and row[2]/100==0: - out = out +newLine+ 'born in 1st century:' +newLine + out +='%sborn in 1st century:%s' % (newLine,newLine) if row[2]/100!=year/100: century = row[2]/100 + 1 - out = out +newLine+ 'born in ' + out +='%sborn in %s century: %s'\ + %(newLine,ordinal_numbers(century),newLine) - out = out +ordinal_numbers(century) \ - + ' century:' + newLine - out = out + name_html(row,newLine) +newLine + out+= name_html(row,newLine) +newLine if row[2] == 0: #unknown year @@ -296,12 +290,13 @@ def list_people(newLine): u = "SELECT diedyear FROM people WHERE ID = ?;" bornAfter = 0 + bornBefore = 0 for r in run_query(u,t): if r[0] !=0: - out = out + "died: "\ - + str(r[0]) + newLine bornAfter = r[0] -100 - + bornBefore = r[0] + out += "Died: %s %s"\ + %(r[0],newLine) #find children u = "Select people.bornYear from"\ +" people INNER JOIN parents"\ @@ -315,13 +310,11 @@ def list_people(newLine): if r[0] != 0: hadChild.append(r[0]) - bornBefore = 0 if len(hadChild)!=0: - out = out + "had children in: " + out += "had children in: " for c in hadChild: - out = out + str(c) + ',' + out+= '%s,' % (c) out = out[:-1] + newLine - bornBefore = hadChild[0]-12 if bornAfter==0: bornAfter = hadChild[0]-100 @@ -334,20 +327,19 @@ def list_people(newLine): +" ORDER BY styles.startYear;" for r in run_query(u,t): - out = out + r[1] + " from " + str(r[0])\ - + newLine + out += "%s from %s %s"\ + %(r[1],r[0],newLine) if bornAfter ==0: bornAfter = r[0] -100 break if bornAfter!=0: if bornBefore == 0: - out = out + "probably born "\ - +"after " + str(bornAfter) + out += "probably born after %s"\ + %(bornAfter) else: - out = out + "probably born "\ - +"betwen " + str(bornAfter)\ - +" and " + str(bornBefore) + out +="probably born between %s and %s"\ + %(bornAfter, bornBefore) out = out + newLine year = row[2] @@ -361,7 +353,7 @@ def count_names(newLine): out = '' for row in run_query(s,()): - out = out + print_name_count(row,newLine) + out += print_name_count(row,newLine) return out @@ -380,7 +372,7 @@ def count_children(newLine): out = '' for row in run_query(s,()): - out = out + print_children_count(row,newLine) + out += print_children_count(row,newLine) return out def parents_with_children(nChildren,newLine): @@ -400,21 +392,15 @@ def parents_with_children(nChildren,newLine): +" WHERE parentID = ?"\ +" AND (diedyear-bornyear>? OR died='present');" - out = "People who had " + str(nChildren) - if nChildren==1: - out = out + " child" - else: - out = out + " children" - out = out + ':' + newLine - - + out = "People who had %s %s:%s" \ + %(nChildren,print_children(nChildren),newLine) for row in run_query(s,(nChildren,)): - out = out + name_html(row,newLine) + out += name_html(row,newLine) for r in run_query(u,(row[1],1)): out += " %d survived infancy" % r[0] for r in run_query(u,(row[1],18)): - out = out +" and " +str(r[0])+" survived to adulthood" + out += " and %s survived to adulthood" % r[0] out = out +newLine return out @@ -424,24 +410,23 @@ def search_name(name,newLine): +" FROM people"\ +" WHERE name LIKE ?;" - out = '' IDs=[] names=[] - out = out + 'Names starting with ' + name + ':' + newLine + out = 'Names starting with %s:%s' % (name,newLine) t = (name + '%',) fullIDs=[] for row in run_query(s,t): - out = out + name_html(row,newLine) + newLine + out += name_html(row,newLine) + newLine fullIDs.append(row[1]) names.append(row[0]) IDs.append(row[1]) t = ('%' + name + '%',) - out = out+newLine + 'Names containing ' + name + ':' + newLine + out += '%sNames containing %s:%s' %(newLine,name,newLine) for row in run_query(s,t): if row[1] not in fullIDs: - out = out + name_html(row,newLine) + newLine + out += name_html(row,newLine) + newLine names.append(row[0]) IDs.append(row[1]) @@ -449,10 +434,9 @@ def search_name(name,newLine): +" FROM people INNER JOIN styles"\ +" ON styles.id = people.id"\ +" WHERE style LIKE ?;" - out = out +newLine+ 'Styles containing ' + name + ':' + newLine + out += '%sStyles containing %s:%s' %(newLine,name,newLine) for row in run_query(s,t): - out = out + name_html(row,newLine)+' ' + row[2] + newLine - + out +="%s %s %s" %(name_html(row,newLine),row[2],newLine) return [out,names,IDs] @@ -466,7 +450,7 @@ def people_with_name(name,newLine): t = (name,) for row in run_query(s,t): - out = out + name_html(row,newLine) + newLine + out += name_html(row,newLine) + newLine return out @@ -481,11 +465,11 @@ def count_birth_month(newLine): out = '' for row in run_query(s,()): month = month_numbers(row[0]) - out = out + month + ': ' + str(row[1]) + newLine + out += "%s:%s%s" %( month,row[1],newLine) if row[0]>12: u = (row[0],) - out = out +print_query(t,u,newLine) + out +=print_query(t,u,newLine) return out @@ -500,11 +484,11 @@ def count_death_month(newLine): out = '' for row in run_query(s,()): month = month_numbers(row[0]) - out = out + month + ': ' + str(row[1]) + newLine + out += '%s:%s%s'%(month,row[1], newLine) if row[0]>12: u = (row[0],) - out = out +print_query(t,u,newLine) + out +=print_query(t,u,newLine) return out @@ -521,7 +505,7 @@ def count_age_at_child(newLine): out = '' for row in run_query(s,()): - out = out + print_age_child_count(row,newLine) + out +=print_age_child_count(row,newLine) return out @@ -540,10 +524,9 @@ def people_had_child_at_age(age,newLine): out = '' out = 'At age ' + str(age) + ' :' for row in run_query(s,t): - out = out + newLine - out =out + name_html([row[3],row[4]],newLine) + ' had '\ - +name_html([row[1],row[2]],newLine) - + parent = name_html([row[3],row[4]],newLine) + child = name_html([row[1],row[2]],newLine) + out += "%s%s had %s" % (newLine,parent,child) return out def count_age_at_death(newLine): @@ -553,7 +536,7 @@ def count_age_at_death(newLine): +" GROUP BY age;" out='' for row in run_query(s,()): - out = out + print_age_death_count(row,newLine) + out += print_age_death_count(row,newLine) return out @@ -565,8 +548,7 @@ def people_died_at_age(age,newLine): out ='' out = 'These people died at age ' +str(age) + ' :' for row in run_query(s,t): - out = out +newLine - out = out + name_html([row[1],row[2]],newLine) + out += newLine+ name_html([row[1],row[2]],newLine) return out def all_ancestors(personID,newLine): @@ -582,20 +564,20 @@ def all_ancestors(personID,newLine): out = "Ancestors of " for row in run_query(t,id): - out = out + name_html(row,newLine)+newLine + out += name_html(row,newLine)+newLine while len(ancestors)>0: level = level+1 newA =[] - thisout = newLine + parent_level(level,'parent') +\ - ':' + newLine + thisout = "%s %s:%s" \ + % (newLine,parent_level(level,'parent'),newLine) + for ancestor in ancestors: [parents, parentIDs,parentNames] \ = find_parents(ancestor) for i in range(len(parents)): r = [parentNames[i],parentIDs[i]] - thisout = thisout + \ - name_html(r,newLine)+newLine + thisout +=name_html(r,newLine)+newLine if r[1] not in allAncestors\ and r[1]!=0: @@ -604,11 +586,11 @@ def all_ancestors(personID,newLine): trackLevel.append(level) ancestors = newA - out = out+thisout + out +=thisout - image = "" - out = out+newLine + image+newLine + image = "" %personID + out +=newLine + image+newLine return [out, allAncestors,trackLevel] @@ -622,15 +604,15 @@ def common_ancestors(IDA, IDB,newLine): for id in (IDA,IDB): t = (id,) for row in run_query(s,t): - out = out + name_html(row,newLine)+newLine + out += name_html(row,newLine)+newLine names.append(row[0]) if id==IDA: - out = out + 'and' + out += 'and' out = out + newLine if len(names)!=2: related = 'No details held on one party' - out = out + related + out += related return [out,[],related] @@ -656,19 +638,19 @@ def common_ancestors(IDA, IDB,newLine): +" FROM people"\ +" WHERE ID IN (" for i in range(len(common)): - s = s+ "?," + s += "?," if len(common)>0: s = s[:-1] s = s+") ORDER BY bornyear;" if len(common)==0: - related = names[0]+' and '+names[1]+' are not related' + related = '%s and %s are not related' %(names[0], names[1]) out = out + newLine + related return [out, common,related] - out = out + print_tagged_query('',s,common,newLine) + out += print_tagged_query('',s,common,newLine) indexA=[] indexB=[] @@ -685,52 +667,57 @@ def common_ancestors(IDA, IDB,newLine): +" FROM people"\ +" WHERE id=?" - out = out + newLine + 'Most Recent Common Ancestors:' + newLine + out += '%sMost Recent Common Ancestors:%s' %(newLine,newLine) mrca = [] for a in indexA: t = (common[a],) mrca.append(common[a]) - out = out + print_tagged_query('',s,t,newLine) + out += print_tagged_query('',s,t,newLine) if a!=indexA[-1]: - out = out + 'and' + newLine + out += 'and' + newLine - out = out + parent_level(aLevels[indexA[0]],'parent') + out += parent_level(aLevels[indexA[0]],'parent') if len(indexA) >1: - out = out + 's' + out += 's' - out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine + out +=' of %s%s'%( name_html([names[0],IDA],newLine),newLine) - out = out + parent_level(bLevels[indexB[0]],'parent') + out += parent_level(bLevels[indexB[0]],'parent') if len(indexB)>1: - out = out + 's' - out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine + out += 's' + out += ' of %s%s' %(name_html([names[1],IDB],newLine),newLine) al = aLevels[indexA[0]] bl = bLevels[indexB[0]] related = relationship(al,bl,names) - out = out+newLine + related + out +=newLine + related image = "" + image = ""\ + %(IDA,IDB,min(aLevels),min(bLevels)) - out = out+newLine + image+newLine + out +=newLine + image+newLine return [out,common,related] def relationship(level1, level2,names): if level1==0 and level2==0: - return names[0] + ' is ' +names[1] + return "%s is %s" %(names[0],names[1]) if level1==0: - return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent') + return "%s is %s's %s" \ + %(names[0],names[1],parent_level(level2,'parent')) + if level2==0: - return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent') + return "%s is %s's %s" \ + %(names[1],names[0],parent_level(level1,'parent')) if level1>=level2: @@ -743,10 +730,10 @@ def relationship(level1, level2,names): if cousinNum==0: uaLevel = parent_level(remove,'uncle or aunt') if level1<= level2: - return names[0] + ' is ' + names[1] + '\'s ' + uaLevel + return "%s is %s's %s" % (names[0],names[1],uaLevel) if level2': output = '
'; - output = output + mainDiv + "
" + output += mainDiv + "" - output = output + "
" imageDiv = '' if picture!='.': - imageDiv = imageDiv + ""\ + imageDiv += ""\ +"wiki link"\ + newLine elif url!='.' and url!='. ': - imageDiv = imageDiv + ""\ + imageDiv += ""\ +name + " (wiki link)"+newLine - output = output + imageDiv + "
" + output += imageDiv + "" url = 'http://www.chiark.greenend.org.uk/ucgi/~naath/'\ +'smallGraph.py' form = '' - form = form + "
" - form = form +\ + form +=\ "
" - form = form +\ + form +=\ "Generations of Parents: "\ +"" - form = form + newLine - form = form + \ + form += newLine + form += \ "Generations of Children: "\ +" " - form = form + newLine - form = form + \ + form += newLine + form += \ "Show siblings: " - form = form + newLine - form = form + \ + form += newLine + form += \ "Show spouse's other spouses: " - form = form + newLine - form = form + \ + form += newLine + form += \ "Show parents' other spouses: " - form = form + newLine - form = form + \ + form += newLine + form += \ "Fount size: "+\ "" - form = form + newLine - form = form + "
" + form += newLine + form += "" graph = "smallGraph.py?ID="+str(personID)+"&fs=8" graph = "" - output = output + "

"+graph+"

" - output = output + "Draw this graph with more relatives:" - output = output + newLine + form + output += "
" + output += "

"+graph+"

" + output += "Draw this graph with more relatives:" + output += newLine + form - output = output + "" - output = output + "
" + output += "" - output = output +\ + output +=\ '