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])
78 out = out + ' people '
80 out = out + ' person '
82 out = out + 'had children at age '+ link + newLine
85 return print_row(row,newLine)
88 def print_name_count(row,newLine):
90 script = "name.py?name=" + row[0]
91 link = link_Template.substitute(script =\
92 script, text = row[0])
93 return str(row[1]) + " people called "+link + newLine
95 return print_row(row,newLine)
97 def print_tagged_name(relationship,row,newLine):
99 out = relationship + " not yet entered: " + row[1]
101 if newLine == '<br>':
105 if relationship =='':
106 out = name_html(row,html) + ' '
108 out = relationship + ": " + name_html(row,html)
111 def month_numbers(monthN):
113 month ='unknown month'
139 month = 'Incorrectly entered month ' + str(monthN)
142 def ordinal_numbers(number):
144 if number % 10==1 and number/10 % 10 !=1:
145 out = str(number) +'st'
146 elif number % 10==2 and number/10 % 10 !=1:
147 out = str(number) +'nd'
148 elif number % 10==3 and number/10 % 10 !=1:
149 out = str(number) +'rd'
151 out = str(number) +'th'
154 def list_territories(newLine):
155 s = "SELECT DISTINCT territory"\
156 +" FROM territories"\
157 +" ORDER BY territory;"
160 for row in run_query(s,()):
161 out =out + terr_html(row[0],newLine) +newLine
165 def list_people(newLine):
166 s = "SELECT name,id,bornyear"\
168 +" ORDER BY bornyear;"
172 out = out + 'born in unknown year:' +newLine
173 for row in run_query(s,()):
174 if row[2]!=0 and row[2]/100==0:
175 out = out +newLine+ 'born in 1st century:' +newLine
177 if row[2]/100!=year/100:
178 century = row[2]/100 + 1
179 out = out +newLine+ 'born in '
181 out = out +ordinal_numbers(century) \
182 + ' century:' + newLine
184 out = out + name_html(row,newLine) +newLine
186 if row[2] == 0: #unknown year
188 t = (row[1],) #person ID
192 u = "SELECT diedyear FROM people WHERE ID = ?;"
195 for r in run_query(u,t):
197 out = out + "died: "\
198 + str(r[0]) + newLine
199 bornAfter = r[0] -100
202 u = "Select people.bornYear from"\
203 +" people INNER JOIN parents"\
204 +" ON people.ID = parents.ID"\
205 +" WHERE parents.parentID = ?"\
206 + " ORDER BY people.bornYear;"
210 for r in run_query(u,t):
212 hadChild.append(r[0])
216 out = out + "had children in: "
218 out = out + str(c) + ','
219 out = out[:-1] + newLine
221 bornBefore = hadChild[0]-12
223 bornAfter = hadChild[0]-100
225 u = "Select styles.startYear, styles.style from"\
226 +" people INNER JOIN styles"\
227 +" ON people.ID = styles.ID"\
228 +" WHERE people.ID = ? and"\
229 +" styles.startYear <>0"\
230 +" ORDER BY styles.startYear;"
232 for r in run_query(u,t):
233 out = out + r[1] + " from " + str(r[0])\
236 bornAfter = r[0] -100
241 out = out + "probably born "\
242 +"after " + str(bornAfter)
244 out = out + "probably born "\
245 +"betwen " + str(bornAfter)\
246 +" and " + str(bornBefore)
252 def count_names(newLine):
253 s = "SELECT firstName, count(*)"\
255 +" GROUP BY firstName"\
256 +" ORDER BY count(*) DESC;"
259 for row in run_query(s,()):
260 out = out + print_name_count(row,newLine)
264 def people_with_name(name,newLine):
265 s = "SELECT Name, ID"\
267 +" WHERE Name LIKE ?;"
273 for row in run_query(s,t):
274 out = out + name_html(row,newLine) + newLine
278 def count_birth_month(newLine):
279 s = "SELECT bornMonth, count(*)"\
281 +" GROUP BY bornMonth"\
282 +" ORDER BY bornMonth;"
284 t = "SELECT * FROM people WHERE bornMonth = ?;"
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_death_month(newLine):
298 s = "SELECT diedMonth, count(*)"\
300 +" GROUP BY diedMonth"\
301 +" ORDER BY diedMonth;"
303 t = "SELECT * FROM people WHERE diedMonth = ?;"
306 for row in run_query(s,()):
307 month = month_numbers(row[0])
308 out = out + month + ': ' + str(row[1]) + newLine
312 out = out +print_query(t,u,newLine)
316 def count_age_at_child(newLine):
318 s = "select p1.bornYear - p2.bornYear as age, count(*)"\
320 +" parents INNER JOIN people p1"\
321 +" ON parents.ID = p1.ID"\
322 +" INNER JOIN people p2"\
323 +" ON parents.parentID = p2.ID"\
324 +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
328 for row in run_query(s,()):
329 out = out + print_age_count(row,newLine)
333 def people_had_child_at_age(age,newLine):
335 s = "select p1.bornYear - p2.bornYear as age, p1.name, p1.ID"\
336 +",p2.name,p2.ID FROM"\
337 +" parents INNER JOIN people p1"\
338 +" ON parents.ID = p1.ID"\
339 +" INNER JOIN people p2"\
340 +" ON parents.parentID = p2.ID"\
341 +" WHERE age = ? AND p1.bornYear<>0 AND p2.bornYear<>0"
346 out = 'At age ' + str(age) + ' :'
347 for row in run_query(s,t):
349 out =out + name_html([row[3],row[4]],newLine) + ' had '\
350 +name_html([row[1],row[2]],newLine)
355 def all_ancestors(personID,newLine):
357 s = "SELECT people.Name,parents.parentID FROM"\
358 +" parents LEFT JOIN people"\
359 +" ON parents.parentID = people.ID"\
360 +" WHERE parents.ID = ?"\
361 +" AND parents.parentID <> '.';"
364 ancestors = [personID]
365 allAncestors = [personID]
369 t = "SELECT name,id FROM people WHERE id==?"
372 out = "Ancestors of "
373 for row in run_query(t,id):
374 out = out + name_html(row,newLine)+newLine
376 while len(ancestors)>0:
379 thisout = newLine + parent_level(level,'parent') +':' + newLine
380 for ancestor in ancestors:
382 for row in run_query(s,id):
383 thisout = thisout + \
384 name_html(row,newLine)+newLine
385 if row[1] not in allAncestors and \
386 is_number(row[1])!=0:
388 allAncestors.append(row[1])
389 trackLevel.append(level)
395 return [out, allAncestors,trackLevel]
398 def common_ancestors(IDA, IDB,newLine):
399 out = 'Common ancestors of:' + newLine
401 s = "SELECT name,id FROM people WHERE id==?"
407 for row in run_query(s,t):
408 out = out + name_html(row,newLine)+newLine
415 related = 'No details held on one party'
417 return [out,[],related]
420 a = all_ancestors(IDA,newLine)
421 b = all_ancestors(IDB,newLine)
423 ancestorsB = set(b[1])
424 ancestorsA = set(a[1])
426 common = ancestorsA.intersection(ancestorsB)
427 common = list(common)
434 aLevels.append(a[2][i])
436 bLevels.append(b[2][i])
438 s = "SELECT Name, ID, bornyear"\
441 for i in range(len(common)):
447 s = s+") ORDER BY bornyear;"
451 related = names[0]+' and '+names[1]+' are not related'
452 out = out + newLine + related
453 return [out, common,related]
456 out = out + print_tagged_query('',s,common,newLine)
461 for i in range(len(common)):
462 if aLevels[i] == min(aLevels):
464 if bLevels[i] == min(bLevels):
469 s = "SELECT name, id"\
473 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
476 out = out + print_tagged_query('',s,t,newLine)
478 out = out + 'and' + newLine
480 out = out + parent_level(aLevels[indexA[0]],'parent')
484 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
489 # out = out + print_tagged_query('',s,t,newLine)
491 # out = out + 'and' + newLine
493 out = out + parent_level(bLevels[indexB[0]],'parent')
496 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
499 al = aLevels[indexA[0]]
500 bl = bLevels[indexB[0]]
502 related = relationship(al,bl,names)
503 out = out+newLine + related
505 return [out,common,related]
507 def relationship(level1, level2,names):
509 if level1==0 and level2==0:
510 return names[0] + ' is ' +names[1]
512 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
514 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
518 remove = level1-level2
521 remove = level2-level1
525 uaLevel = parent_level(remove,'uncle or aunt')
527 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
530 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
532 c=ordinal_numbers(cousinNum)
538 rem = str(remove) + ' times'
540 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
542 r = r+ rem + ' removed'
546 def parent_level(level,type):
561 for i in range(2,level):
565 def rulers_of(aTerritory,newLine):
567 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
568 +" FROM territories INNER JOIN people"\
569 +" ON people.ID = territories.ID"\
570 +" WHERE territory LIKE ?"\
571 +" ORDER BY territory,startyear,stopyear;"
578 for row in run_query(tq,(aTerritory+'%',)):
579 if row[4]!=last and last!='':
580 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
583 thisT = thisT +name_html(row,newLine)
584 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
587 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
591 def person_info(personID,newLine):
596 #Id, Name, Dates, Style, Style-Dates
597 s = "SELECT * FROM people WHERE ID = ?"
598 for row in run_query(s,t):
599 output = output + 'ID: '+str(row[0]) +newLine
600 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
601 output = output + 'Born: '+row[3] + newLine
603 output = output + 'Died: '+row[5] + newLine
605 s = "SELECT * FROM styles WHERE ID = ?"
606 for row in run_query(s,t):
607 output = output +newLine+ 'Style: '+row[1] + newLine
609 output = output + 'Territories:' + newLine
611 u = "SELECT * FROM territories"\
612 +" WHERE ID =? AND startYear =? AND stopYear=?"
613 v=(personID,row[3],row[5])
616 for r in run_query(u,v):
617 output = output + terr_html(r[1],newLine) +','
620 output = output[:-1] + newLine
622 output = output + 'From: '+row[2] + newLine
623 output = output + 'To: '+row[4] + newLine
625 s = "SELECT people.Name,consort "\
626 +"FROM consorts LEFT JOIN people"\
627 +" ON people.ID = consorts.consort"\
628 +" WHERE consorts.ID = ?"
629 for row in run_query(s,t):
630 output = output + print_tagged_name('Consort',row,newLine)
632 output = output + newLine
634 s = "SELECT people.Name,parents.parentID FROM"\
635 +" parents LEFT JOIN people"\
636 +" ON parents.parentID = people.ID"\
637 +" WHERE parents.ID = ?"
638 for row in run_query(s,t):
639 output = output + print_tagged_name('Parent',row,newLine)
642 s = "SELECT people.NAME, marriages.IDb from"\
643 +" marriages LEFT JOIN people"\
644 +" ON people.ID = marriages.IDb"\
645 +" WHERE marriages.IDa = ?"
646 for row in run_query(s,t):
647 output = output + newLine
648 output = output + print_tagged_name('Spouse',row,newLine)
649 output = output + relationship_html(personID,row[1],newLine)
651 s = "SELECT people.NAME, marriages.IDa from"\
652 +" marriages LEFT JOIN people"\
653 +" ON people.ID = marriages.IDa"\
654 +" WHERE marriages.IDb = ?"
655 for row in run_query(s,t):
656 output = output + newLine
657 output = output + print_tagged_name('Spouse',row,newLine)
658 output = output + relationship_html(personID,row[1],newLine)
660 output = output + newLine
663 s = "Select people.NAME, people.ID ,people.bornYear"\
664 +" FROM people INNER JOIN parents"\
665 +" ON people.ID = parents.ID"\
666 +" WHERE parents.parentID = ?"\
667 +" ORDER BY people.bornYear;"
669 for row in run_query(s,t):
670 output = output + print_tagged_name('Child',row,newLine)
672 #find children's other parent
673 u = "Select people.NAME, parents.parentID FROM"\
674 +" parents LEFT JOIN people"\
675 +" ON people.ID = parents.parentID"\
676 +" WHERE parents.ID = ? AND parents.parentID <> ?"
680 for r in run_query(u,ids):
681 output = output + print_tagged_name('With',r,newLine)
684 if row[2] !=0 and bornYear != 0:
685 age = row[2]-bornYear
686 output = output[:-4] + " at the age of "+str(age) + newLine
688 output = output + newLine
694 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
705 # [c, conn] = connect()