5 from string import Template
8 link_Template= Template("<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script>$text</a>")
14 def print_row(row,newLine):
17 out = out + str(item)+'|'
18 return out[:-1] + newLine
20 def print_query(s,t,newLine):
22 for row in run_query(s,t):
23 printMe = printMe + print_row(row,newLine)
33 def print_tagged_query(relationship,s,t,newLine):
35 for row in run_query(s,t):
36 mine = mine + print_tagged_name(relationship,row,newLine)
40 def relationship_html(ID,ID2,newLine):
42 relationship = common_ancestors(ID,ID2,newLine)[2]
43 script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
44 url = link_Template.substitute(script = script,text = "Common ancestors")
45 return relationship + ' '+url + newLine
49 def terr_html(terr,newLine):
51 return link_Template.substitute(script = "territory.py?terr="+terr, text=terr)
54 def name_html(row,html):
64 script = "person.py?ID=" + str(row[1])
66 return link_Template.substitute(script = script\
69 return row[0] + "," +str(row[1])
71 def print_age_count(row,newLine):
73 script = "age.py?age="+str(row[0])
74 link = link_Template.substitute(script = \
75 script, text = row[0])
76 return str(row[1]) + ' people had children at age '\
79 return print_row(row,newLine)
82 def print_name_count(row,newLine):
84 script = "name.py?name=" + row[0]
85 link = link_Template.substitute(script =\
86 script, text = row[0])
87 return str(row[1]) + " people called "+link + newLine
89 return print_row(row,newLine)
91 def print_tagged_name(relationship,row,newLine):
93 out = relationship + " not yet entered: " + row[1]
100 out = name_html(row,html) + ' '
102 out = relationship + ": " + name_html(row,html)
105 def month_numbers(monthN):
107 month ='unknown month'
133 month = 'Incorrectly entered month ' + str(monthN)
136 def ordinal_numbers(number):
138 if number % 10==1 and number/10 % 10 !=1:
139 out = str(number) +'st'
140 elif number % 10==2 and number/10 % 10 !=1:
141 out = str(number) +'nd'
142 elif number % 10==3 and number/10 % 10 !=1:
143 out = str(number) +'rd'
145 out = str(number) +'th'
148 def list_territories(newLine):
149 s = "SELECT DISTINCT territory"\
150 +" FROM territories"\
151 +" ORDER BY territory;"
154 for row in run_query(s,()):
155 out =out + terr_html(row[0],newLine) +newLine
159 def list_people(newLine):
160 s = "SELECT name,id,bornyear"\
162 +" ORDER BY bornyear;"
166 out = out + 'born in unknown year:' +newLine
167 for row in run_query(s,()):
168 if row[2]!=0 and row[2]/100==0:
169 out = out +newLine+ 'born in 1st century:' +newLine
171 if row[2]/100!=year/100:
172 century = row[2]/100 + 1
173 out = out +newLine+ 'born in '
175 out = out +ordinal_numbers(century) \
176 + ' century:' + newLine
178 out = out + name_html(row,newLine) +newLine
180 if row[2] == 0: #unknown year
182 t = (row[1],) #person ID
186 u = "SELECT diedyear FROM people WHERE ID = ?;"
189 for r in run_query(u,t):
191 out = out + "died: "\
192 + str(r[0]) + newLine
193 bornAfter = r[0] -100
196 u = "Select people.bornYear from"\
197 +" people INNER JOIN parents"\
198 +" ON people.ID = parents.ID"\
199 +" WHERE parents.parentID = ?"\
200 + " ORDER BY people.bornYear;"
204 for r in run_query(u,t):
206 hadChild.append(r[0])
210 out = out + "had children in: "
212 out = out + str(c) + ','
213 out = out[:-1] + newLine
215 bornBefore = hadChild[0]-12
217 bornAfter = hadChild[0]-100
221 out = out + "probably born "\
222 +"after " + str(bornAfter)
224 out = out + "probably born "\
225 +"betwen " + str(bornAfter)\
226 +" and " + str(bornBefore)
233 def count_names(newLine):
234 s = "SELECT firstName, count(*)"\
236 +" GROUP BY firstName"\
237 +" ORDER BY count(*) DESC;"
240 for row in run_query(s,()):
241 out = out + print_name_count(row,newLine)
245 def people_with_name(name,newLine):
246 s = "SELECT Name, ID"\
248 +" WHERE Name LIKE ?;"
254 for row in run_query(s,t):
255 out = out + name_html(row,newLine) + newLine
259 def count_birth_month(newLine):
260 s = "SELECT bornMonth, count(*)"\
262 +" GROUP BY bornMonth"\
263 +" ORDER BY bornMonth;"
265 t = "SELECT * FROM people WHERE bornMonth = ?;"
268 for row in run_query(s,()):
269 month = month_numbers(row[0])
270 out = out + month + ': ' + str(row[1]) + newLine
274 out = out +print_query(t,u,newLine)
278 def count_death_month(newLine):
279 s = "SELECT diedMonth, count(*)"\
281 +" GROUP BY diedMonth"\
282 +" ORDER BY diedMonth;"
284 t = "SELECT * FROM people WHERE diedMonth = ?;"
287 for row in run_query(s,()):
288 month = month_numbers(row[0])
289 out = out + month + ': ' + str(row[1]) + newLine
293 out = out +print_query(t,u,newLine)
297 def count_age_at_child(newLine):
299 s = "select p1.bornYear - p2.bornYear as age, count(*)"\
301 +" parents INNER JOIN people p1"\
302 +" ON parents.ID = p1.ID"\
303 +" INNER JOIN people p2"\
304 +" ON parents.parentID = p2.ID"\
305 +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
309 for row in run_query(s,()):
310 out = out + print_age_count(row,newLine)
314 def people_had_child_at_age(age,newLine):
316 s = "select p1.bornYear - p2.bornYear as age, p1.name, p1.ID"\
317 +",p2.name,p2.ID FROM"\
318 +" parents INNER JOIN people p1"\
319 +" ON parents.ID = p1.ID"\
320 +" INNER JOIN people p2"\
321 +" ON parents.parentID = p2.ID"\
322 +" WHERE age = ? AND p1.bornYear<>0 AND p2.bornYear<>0"
327 out = 'At age ' + str(age) + ' :'
328 for row in run_query(s,t):
330 out =out + name_html([row[3],row[4]],newLine) + ' had '\
331 +name_html([row[1],row[2]],newLine)
336 def all_ancestors(personID,newLine):
338 s = "SELECT people.Name,parents.parentID FROM"\
339 +" parents LEFT JOIN people"\
340 +" ON parents.parentID = people.ID"\
341 +" WHERE parents.ID = ?"\
342 +" AND parents.parentID <> '.';"
345 ancestors = [personID]
346 allAncestors = [personID]
350 t = "SELECT name,id FROM people WHERE id==?"
353 out = "Ancestors of "
354 for row in run_query(t,id):
355 out = out + name_html(row,newLine)+newLine
357 while len(ancestors)>0:
360 thisout = newLine + parent_level(level,'parent') +':' + newLine
361 for ancestor in ancestors:
363 for row in run_query(s,id):
364 thisout = thisout + name_html(row,newLine)+newLine
365 if row[1] not in allAncestors and is_number(row[1])!=0:
367 allAncestors.append(row[1])
368 trackLevel.append(level)
374 return [out, allAncestors,trackLevel]
377 def common_ancestors(IDA, IDB,newLine):
378 out = 'Common ancestors of:' + newLine
380 s = "SELECT name,id FROM people WHERE id==?"
386 for row in run_query(s,t):
387 out = out + name_html(row,newLine)+newLine
394 related = 'No details held on one party'
396 return [out,[],related]
399 a = all_ancestors(IDA,newLine)
400 b = all_ancestors(IDB,newLine)
402 ancestorsB = set(b[1])
403 ancestorsA = set(a[1])
405 common = ancestorsA.intersection(ancestorsB)
406 common = list(common)
413 aLevels.append(a[2][i])
415 bLevels.append(b[2][i])
417 s = "SELECT Name, ID, bornyear"\
420 for i in range(len(common)):
426 s = s+") ORDER BY bornyear;"
430 related = names[0]+' and '+names[1]+' are not related'
431 out = out + newLine + related
432 return [out, common,related]
435 out = out + print_tagged_query('',s,common,newLine)
440 for i in range(len(common)):
441 if aLevels[i] == min(aLevels):
443 if bLevels[i] == min(bLevels):
448 s = "SELECT name, id"\
452 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
455 out = out + print_tagged_query('',s,t,newLine)
457 out = out + 'and' + newLine
459 out = out + parent_level(aLevels[indexA[0]],'parent')
463 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
468 out = out + print_tagged_query('',s,t,newLine)
470 out = out + 'and' + newLine
472 out = out + parent_level(bLevels[indexB[0]],'parent')
475 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
478 al = aLevels[indexA[0]]
479 bl = bLevels[indexB[0]]
481 related = relationship(al,bl,names)
482 out = out+newLine + related
484 return [out,common,related]
486 def relationship(level1, level2,names):
488 if level1==0 and level2==0:
489 return names[0] + ' is ' +names[1]
491 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
493 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
497 remove = level1-level2
500 remove = level2-level1
504 uaLevel = parent_level(remove,'uncle or aunt')
506 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
509 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
511 c=ordinal_numbers(cousinNum)
517 rem = str(remove) + ' times'
519 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
521 r = r+ rem + ' removed'
525 def parent_level(level,type):
540 for i in range(2,level):
544 def rulers_of(aTerritory,newLine):
546 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
547 +" FROM territories INNER JOIN people"\
548 +" ON people.ID = territories.ID"\
549 +" WHERE territory LIKE ?"\
550 +" ORDER BY territory,startyear,stopyear;"
557 for row in run_query(tq,(aTerritory+'%',)):
558 if row[4]!=last and last!='':
559 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
562 thisT = thisT +name_html(row,newLine)
563 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
566 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
570 def person_info(personID,newLine):
575 #Id, Name, Dates, Style, Style-Dates
576 s = "SELECT * FROM people WHERE ID = ?"
577 for row in run_query(s,t):
578 output = output + 'ID: '+str(row[0]) +newLine
579 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
580 output = output + 'Born: '+row[3] + newLine
582 output = output + 'Died: '+row[5] + newLine
584 s = "SELECT * FROM styles WHERE ID = ?"
585 for row in run_query(s,t):
586 output = output +newLine+ 'Style: '+row[1] + newLine
588 output = output + 'Territories:' + newLine
590 u = "SELECT * FROM territories"\
591 +" WHERE ID =? AND startYear =? AND stopYear=?"
592 v=(personID,row[3],row[5])
595 for r in run_query(u,v):
596 output = output + terr_html(r[1],newLine) +','
599 output = output[:-1] + newLine
601 output = output + 'From: '+row[2] + newLine
602 output = output + 'To: '+row[4] + newLine
604 s = "SELECT people.Name,consort "\
605 +"FROM consorts LEFT JOIN people"\
606 +" ON people.ID = consorts.consort"\
607 +" WHERE consorts.ID = ?"
608 for row in run_query(s,t):
609 output = output + print_tagged_name('Consort',row,newLine)
611 output = output + newLine
613 s = "SELECT people.Name,parents.parentID FROM"\
614 +" parents LEFT JOIN people"\
615 +" ON parents.parentID = people.ID"\
616 +" WHERE parents.ID = ?"
617 for row in run_query(s,t):
618 output = output + print_tagged_name('Parent',row,newLine)
621 s = "SELECT people.NAME, marriages.IDb from"\
622 +" marriages LEFT JOIN people"\
623 +" ON people.ID = marriages.IDb"\
624 +" WHERE marriages.IDa = ?"
625 for row in run_query(s,t):
626 output = output + newLine
627 output = output + print_tagged_name('Spouse',row,newLine)
628 output = output + relationship_html(personID,row[1],newLine)
630 s = "SELECT people.NAME, marriages.IDa from"\
631 +" marriages LEFT JOIN people"\
632 +" ON people.ID = marriages.IDa"\
633 +" WHERE marriages.IDb = ?"
634 for row in run_query(s,t):
635 output = output + newLine
636 output = output + print_tagged_name('Spouse',row,newLine)
637 output = output + relationship_html(personID,row[1],newLine)
639 output = output + newLine
642 s = "Select people.NAME, people.ID ,people.bornYear"\
643 +" FROM people INNER JOIN parents"\
644 +" ON people.ID = parents.ID"\
645 +" WHERE parents.parentID = ?"\
646 +" ORDER BY people.bornYear;"
648 for row in run_query(s,t):
649 output = output + print_tagged_name('Child',row,newLine)
651 #find children's other parent
652 u = "Select people.NAME, parents.parentID FROM"\
653 +" parents LEFT JOIN people"\
654 +" ON people.ID = parents.parentID"\
655 +" WHERE parents.ID = ? AND parents.parentID <> ?"
659 for r in run_query(u,ids):
660 output = output + print_tagged_name('With',r,newLine)
663 age = row[2]-bornYear
664 output = output[:-4] + " at the age of "+str(age) + newLine
666 output = output + newLine
672 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
683 # [c, conn] = connect()