5 from string import Template
9 import pygraph.algorithms.accessibility as access
10 import englishUtils as eU
11 import printUtils as pU
12 import graphQuestions as gQ
18 ageTable = "(SELECT diedYear-bornYear as age, name,ID,diedYear,bornYear"\
20 +" WHERE bornYear<>0 AND diedYear<>0 AND (diedMonth>bornMonth"\
21 +" OR (diedMonth==0)"\
22 +" OR (diedMonth = bornMonth AND (diedDay>=bornDay OR diedDay =0)))"\
24 +" SELECT diedYear-bornYear-1 as age,name,ID,diedYear,bornYear"\
26 +" WHERE bornYear<>0 AND diedYear<>0 AND diedMonth<>0"\
27 +" AND (diedMonth<bornMonth"\
28 +" OR (diedMonth = bornMonth AND (diedDay<bornDay AND diedDay <>0)))"\
32 ageNowTable = "(SELECT strftime('%Y',date('now'))-bornYear as age,"\
33 +" name,ID,diedYear,bornYear"\
35 +" WHERE bornYear<>0 AND (strftime('%m',date('now'))>bornMonth"\
36 +" OR (strftime('%m',date('now')) = bornMonth AND"\
37 +" (strftime('%d',date('now'))>=bornDay)))"\
39 +" SELECT strftime('%Y',date('now'))-bornYear-1 as age,"\
40 +" name,ID,strftime('%Y',date('now')),bornYear"\
42 +" WHERE bornYear<>0 AND (strftime('%m',date('now'))<bornMonth"\
43 +" OR (strftime('%m',date('now')) = bornMonth"\
44 +" AND (strftime('%d',date('now'))<bornDay)))"\
48 ageChildTable = "(SELECT c.bornYear - p.bornYear as age,"\
49 +" c.name as cName,c.ID as cID,"\
50 +" p.name as pname,p.ID as pID,"\
51 +" c.bornYear as bornYear"\
53 +" parents INNER JOIN people c"\
54 +" ON parents.ID = c.ID"\
55 +" INNER JOIN people p"\
56 +" ON parents.parentID = p.ID"\
57 +" WHERE p.bornYear <>0 AND c.bornYear<>0"\
58 +" AND (c.bornMonth>p.bornMonth"\
59 +" OR (c.bornMonth = p.bornMonth AND c.bornDay>=p.bornDay))"\
61 +" SELECT c.bornYear - p.bornYear-1 as age,"\
62 +" c.name as cName,c.ID as cID,"\
63 +" p.name as pName,p.ID as pID,"\
64 +" c.bornYear as bornYear"\
66 +" parents INNER JOIN people c"\
67 +" ON parents.ID = c.ID"\
68 +" INNER JOIN people p"\
69 +" ON parents.parentID = p.ID"\
70 +" WHERE p.bornYear <>0 AND c.bornYear<>0"\
71 +" AND (c.bornMonth<p.bornMonth"\
72 +" OR (c.bornMonth = p.bornMonth AND c.bornDay<p.bornDay))"\
81 def make_insert(table,fields):
85 for i in range(len(fields)):
87 values = values[:-1]+')'
89 s = 'INSERT INTO '+table+' VALUES'\
94 s = 'SELECT max(ID) FROM people;'
95 for r in run_query(s,()):
98 def calendar(newLine):
101 mLength = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
104 fcstart = '<FONT COLOR = "'
108 dcstart = fcstart+dcol+fcmid
112 n = count_born_on(m+1,0,b)
113 o = count_born_on(m+1,0,x)
114 p = count_born_in(m+1,b)
115 q = count_born_in(m+1,x)
117 script = 'birthday.py?date=%d-0' %(m+1)
119 url = pU.link_Template.substitute\
120 (script = script,title = eU.month_numbers(m+1),\
121 text = eU.month_numbers(m+1))
122 out += "%s %d %s%d%s %d %s%d%s%s" \
123 %(url,n,dcstart,o,fcend,p,dcstart,q,fcend,newLine)
128 for d in range(mLength[m]):
129 script = 'birthday.py?date=%d-%d' % (m+1,d+1)
130 url = pU.link_Template.substitute\
131 (script = script,title = str(d+1),\
134 n = count_born_on(m+1,d+1,b)
135 o = count_born_on(m+1,d+1,x)
139 out+="%s%s%s %s%d%s %s%s%s%s%s " %(s,url,e,\
140 s,n,e,s,dcstart,o,fcend,e)
141 if (d+1)%7 ==0 or (d+1)==mLength[m]:
147 script = 'birthday.py?date=0-0'
148 url = pU.link_Template.substitute\
149 (script = script,title = 'unknown',\
152 n = count_born_on(0,0,b)
153 o = count_born_on(0,0,x)
154 out+="%s %d %s%d%s%s" %(url,n,dcstart,o,fcend,newLine)
158 script = 'birthday.py?date=20-0'
159 url = pU.link_Template.substitute\
160 (script = script,title = 'on their birthday',\
161 text = 'on their birthday')
162 n = count_born_on(20,0,b)
163 out +="%d people died %s%s" %(n,url,newLine)
166 s = "select diedYear,count(*)"\
168 +" WHERE diedYear==?"\
169 +" GROUP BY diedYear;"
170 for row in run_query(s,(presentYear,)):
171 out += pU.print_age_death_count(row,newLine)
173 for bd in ('born','died'):
175 s = "SELECT "+bd+", count(*) AS n"\
176 +" FROM (SELECT "+bd + "," + bd + "Year"\
178 +" WHERE "+bd+"Month<>0 AND "+bd+"Day<>0)"\
181 +" ORDER BY "+bd+"Year;"
183 t = "SELECT name,id"\
185 +" WHERE "+bd+" = ?;"
188 out += "On the following days more than one person %s: %s" \
191 for row in run_query (s,()):
192 out+=row[0]+":"+newLine
193 for r in run_query(t,(str(row[0]),)):
194 out+=pU.name_html(r,newLine)+newLine
200 def count_born_on(month,day,bd):
202 s = "SELECT count(*) FROM people"\
203 +" WHERE bornMonth==diedMonth"\
204 +" AND bornDay==diedDay"\
205 +" AND bornMonth<>0"\
210 s = "SELECT count(*) FROM people"\
211 +" WHERE "+bd+"Month==?"\
212 +" AND "+bd+"Day==?"\
213 +" AND "+bd+"Year<>?;"
215 t = (month,day,presentYear)
217 for row in run_query(s,t):
220 def count_born_in(month,bd):
222 s = "SELECT count(*) FROM people"\
223 +" WHERE "+bd+"Month==?"\
224 +" AND "+bd+"Year<>?;"
225 t = (month,presentYear)
227 for row in run_query(s,t):
230 def born_on(date,newLine):
231 month = int(date.split('-')[0])
232 day = int(date.split('-')[1])
234 fcstart = '<FONT COLOR = "'
238 dcstart = fcstart+dcol+fcmid
245 n = count_born_on(month,day,'')
247 out+="%d %s died on their birthday: %s"\
248 %(n, eU.print_people(n),newLine)
250 s = "SELECT name,id,bornYear,diedYear,bornMonth,bornDay"\
252 +" WHERE bornMonth==diedMonth"\
253 +" AND bornDay==diedDay"\
254 +" AND bornMonth<>0"\
256 +" ORDER BY diedYear-bornYear;"
258 out+="Died on the day they were born:%s"%(newLine)
259 out+=pU.table_header(['link','born and died'],newLine)
261 for row in run_query(s,t):
262 if row[2]!=row[3] and header==0:
264 out+=pU.table_foot(newLine)
265 out+="%sDied on a later birthday:%s"\
267 out+=pU.table_header(['link','born','died','month','day'],\
269 link = pU.name_html(row,newLine)
270 born = pU.print_year(row[2])
271 died = pU.print_year(row[3])
272 month = eU.month_numbers(row[4])
275 out+=pU.table_row([link,born],newLine)
277 out+=pU.table_row([link,born,died,month,day],newLine)
278 out+=pU.table_foot(newLine)
283 %(eU.month_numbers(month),eU.ordinal_numbers(day),newLine)
286 %(eU.month_numbers(month),newLine)
289 for bd in ['born','died']:
290 s = "SELECT name,id,"+bd+"Year FROM people"\
291 +" WHERE "+bd+"Month==?"\
292 +" AND "+bd+"Day==?"\
293 +" AND "+bd+"Year<>?"\
294 +" ORDER BY "+bd+"Year;"
296 t = (month,day,presentYear)
297 n = count_born_on(month,day,bd)
302 out+="%s %s %s on this day %s" \
303 %(n,eU.print_people(n),bd, newLine)
305 out+="%s %s %s in this month on unknown day %s"\
306 %(n,eU.print_people(n),bd,newLine)
308 out+="%s %s %s in unknown month on unknown day %s"\
309 %(n,eU.print_people(n),bd,newLine)
312 out+=pU.table_header(['id','link',bd],newLine)
313 for row in run_query(s,t):
314 y = pU.print_year(row[2])
316 link = pU.name_html(row,newLine)
317 out+=pU.table_row([row[1],link,y],newLine)
319 out+=pU.table_foot(newLine)
322 if day==0 and month!=0:
323 for bd in ['born','died']:
324 s = "SELECT name,id,"+bd+"Year FROM people"\
325 +" WHERE "+bd+"Month==?"\
326 +" AND "+bd+"Year<>?"\
327 +" ORDER BY "+bd+"Year;"
329 t = (month,presentYear)
330 n = count_born_in(month,bd)
334 out+="%s %s %s in this month %s" \
335 %(n,eU.print_people(n),bd, newLine)
339 out+=pU.table_header(['link',bd],newLine)
340 for row in run_query(s,t):
341 link = pU.name_html(row,newLine)
342 y = pU.print_year(row[2])
344 out+=pU.table_row([link,y],newLine)
345 out+=pU.table_foot(newLine)
351 def alive_on(day,month,year,newLine):
353 bornBefore = "(bornMonth<? OR"\
354 +" (bornMonth==? AND bornDay<=?))"
356 diedAfter = "(diedMonth>? OR diedMonth==0"\
357 +" OR (diedMonth==? AND (diedDay>=? OR diedDay==0)))"
359 bornOrder = "bornyear,bornmonth,bornday"
360 diedOrder = "diedyear,diedmonth,diedday"
361 s = "SELECT name,ID,bornYear,diedYear FROM people"\
363 +" bornYear<>0 AND diedYear<>0 AND("\
364 +" (bornYear<? AND diedYear>?)"\
366 +" (bornYear==? AND diedYear>? AND "+bornBefore+")"\
368 +" (bornYear<? AND diedYear==? AND "+diedAfter+")"\
370 +" (bornYear==? and diedYear==? AND "\
371 +bornBefore+" AND "+diedAfter+")"\
373 +"ORDER BY "+bornOrder+"," + diedOrder+";"
376 bbt=(month,month,day)
377 dat = (month,month,day)
378 t = yeart+yeart+bbt+yeart+dat+yeart+bbt+dat
379 out=pU.table_header(['link','born','died'],newLine)
380 for row in run_query(s,t):
381 link=pU.name_html(row,newLine)
382 born = pU.print_year(row[2])
383 died = pU.print_year(row[3])
384 out+=pU.table_row([link,born,died],newLine)
386 out+=pU.table_foot(newLine)
390 def list_people_parents():
391 s = "SELECT name,id"\
396 for row in run_query(s,()):
399 [parents, parentIDs,parentNames] = find_parents(ID)
400 [spouses,spousesID,spousesNames,sD] = find_spouses(ID)
402 [self,myID,myName] = find_person(ID)
403 output.append([self,parents,spouses])
407 def list_people(newLine):
408 s = "SELECT name,id,bornyear,fullname,diedYear,url"\
410 +" ORDER BY bornyear,diedYear;"
412 out = pU.table_header(['ID','link','wiki','name','born','died'],\
415 out+=pU.table_row(['','',\
416 '<b>Unknown Year</b>',0,0,''],newLine)
417 for row in run_query(s,()):
418 if row[2]!=0 and row[2]/100==0:
419 out+=pU.table_row(['','','',\
420 '<b>1st century</b>'\
422 if row[2]/100!=year/100:
423 century = row[2]/100 + 1
424 start = century * 100 -99
426 out+=pU.table_row(['','','',\
427 '<b>'+eU.ordinal_numbers(century)+' century</b>',\
432 link = pU.name_html(row,newLine)
434 # struct = fetch_page(row[5])
435 # if struct.has_key('query'):
436 # pName = struct['query']['pages'].keys()[0]
437 # wikiName = struct['query']['pages'][pName]['title']
443 wikiLink = "<a href='" + wikiName + "'>"+wikiName+"</a>"
445 a = re.search('#',wikiName)
455 born = pU.print_year(row[2])
456 died = pU.print_year(row[4])
458 out+=pU.table_row([ID,link,wikiLink,name,born,died],newLine)
460 out+=pU.table_foot(newLine)
463 def list_page(newLine):
464 s = "SELECT name,ID,url FROM people ORDER BY ID;"
469 for row in run_query(s,t):
471 topLink = "%d <a href='%s'>%s</a>" %(row[1],row[2],row[0])
473 myLink = pU.name_html(row,newLine)
474 topLink = topLink+' '+myLink
478 [fam,count,checkMe] = find_fam(id,url,newLine)
480 out+=topLink+newLine+fam+newLine
482 check.append([id,topLink,url,fam,count])
485 out = 'MATCHED'+newLine
486 hold = 'CHECK COUNT'+newLine
496 links,countL = find_links(url)
499 this=topLink+newLine+fam+newLine
503 this+='<li>'+l+newLine+'\n'
507 if countL[i]<countF[i]:
513 this+newLine+str(countF)+str(countL)+newLine
522 out=newLine + 'CHECK NO INFO'+newLine
530 if re.search('#',url):
532 out+=topLink+newLine+fam
534 return [outDone,outMatched,outUnMatched,out]
536 def find_fam(id,link,newLine):
539 [parents, parentIDs,parentNames] = find_parents(id)
540 [spouses,spousesID,spousesNames,sD] = find_spouses(id)
541 [nodes,IDs,names,childrenBorn] = \
544 relIDs = parentIDs+spousesID
549 findUrl = 'SELECT url,name FROM people WHERE ID=?'
550 findName = 'SELECT name,ID FROM people WHERE ID=?'
558 elif ID in spousesID:
565 for row in run_query(findUrl,t):
571 out+='<li>'+type+" <a href="+url\
572 +">"+title+"</a>"+newLine
574 for row in run_query(findName,t):
575 # out+='<li>'+pU.name_html(row,newLine)\
579 out+='<li>'+type+' '+name + ' '+id+\
584 return [out,count, anyCheck]
589 title = url.split('/')[-1]
590 url = 'http://en.wikipedia.org/w/api.php?'\
591 +'format=json&action=query'\
593 +'&prop=revisions&rvprop=content&redirects'
595 r = urllib2.urlopen(url)
597 jd = json.JSONDecoder()
598 struct = jd.decode(t)
603 def find_infoBox(url):
604 struct = fetch_page(url)
606 pages = struct['query']['pages'].keys()
610 startPatt = re.compile('{{',re.DOTALL)
611 endPatt = re.compile('}}',re.DOTALL)
612 infoboxPatt = re.compile('{{( )*[Ii]nfobox',re.DOTALL)
617 page = struct['query']['pages'][p]['revisions'][0]['*']
621 title = struct['query']['pages'][p]['title']
622 iBox = re.search(infoboxPatt,page)
623 starts = re.finditer(startPatt,page)
624 ends = re.finditer(endPatt,page)
629 myStart = iBox.start()
635 start = starts.next().start()
638 end = ends.next().start()
642 start = starts.next().start()
646 end = ends.next().start()
649 info = page[myStart+2:myEnd]
658 info = find_infoBox(url)
661 l,c = wikilink_box(info)
666 def wikilink_box(text):
667 linkPatt = "([^=]*?)\[\[(.*?)\]\](.*?)"
668 #linkPatt = "\|(.*?)\[\[(.*?)\]\](.*?)\n"
669 #linkPatt = "(.*?)\[\[(.*?)\]\]([^<]*?)[\\n|<br.*?>|,]"
672 commentPatt = "<!--.*?>"
673 sizePatt = "<[/]*small>"
674 text=re.sub(commentPatt,'',text)
675 text = re.sub(sizePatt,'',text)
677 pscPatt = "father|mother|parent|spouse|issue|child"
679 lines = re.split('\\n',text)
682 if re.search(pscPatt,line):
683 t = re.search(typePatt,line)
687 if re.search('father|mother|parent',type):
689 elif re.search('spouse',type):
693 people = re.split('<br.*?>',line)
696 if re.search(linkPatt,p):
697 l = re.search(linkPatt,p)
698 url = wiki_to_url(l.group(2))
699 t = l.group(2).split('|')[-1]
701 myLink = '<a href="%s">%s</a>' \
703 link = l.group(1)+myLink+l.group(3)
711 def wiki_to_url(wiki):
712 base = 'https://en.wikipedia.org/wiki/'
713 u = wiki.split('|')[0]
714 u = re.sub(' ','_',u)
718 def check_true(newLine):
719 s = "SELECT name,id,url,born,died FROM people WHERE id>? and id<? ORDER BY id;"
722 out='<table border = "1" style = "width:100%">'
724 out+='<th style="width:20%">Name</th>'
725 out+='<th style="width:40%">Toy</th>'
726 out+='<th style="width:40%">Wiki</th>'
729 for row in run_query(s,t):
732 struct = fetch_page(row[2])
734 p = struct['query']['pages'].keys()[0]
738 title = struct['query']['pages'][p]['title']
740 topLink = "%d <a href='%s'>%s</a>" %(row[1],row[2],title)
742 myLink = pU.name_html(row,newLine)
743 topLink = topLink+newLine+myLink
747 [fam,count,checkMe] = find_fam(id,url,newLine)
748 infobox = find_infoBox(url)
754 out+='<td>'+topLink+'</td>'
762 out+='<td>'+fam+newLine+'born:'+row[3]+newLine\
763 +'died:'+row[4]+'</td>'
766 wikiLink='\\[\\[([^\\|\\]]*)\\|*(.*?)\\]\\]'
767 htmlLink=r'<a href="https://en.wikipedia.org/wiki/\1">\1</a>'
769 htmlBox = re.sub(wikiLink,htmlLink,infobox)
772 starts = re.finditer('{{',htmlBox)
773 stops = re.finditer('}}',htmlBox)
777 start = starts.next().start()
778 stop = stops.next().start()
786 for i in re.finditer('\|',htmlBox):
787 if i.start()>start and start<stop:
790 start= starts.next().start()
793 elif i.start()>stop and stop<start:
796 stop = stops.next().start()
801 lines.append(htmlBox[place:i.start()])
805 of_interest = 'father|mother|parent|child|issue'\
806 +'|spouse|birth_date|death_date'
816 if re.search('father|mother|parent',l):
818 if re.search('child|issue',l):
820 if re.search('spouse',l):
822 if re.search('birth_date|death_date',l):
842 def search_name(name,newLine):
843 s = "SELECT name, people.ID,fullname,BornYear,DiedYear"\
845 +" WHERE fullname LIKE ? or name LIKE ?"\
846 +" ORDER BY BornYear;"
848 s2 = "SELECT family FROM families where ID=?"
853 out = 'Names starting with %s:%s' % (name,newLine)
854 t = (name + '%',name+'%')
856 out+=pU.table_header(['link','name','born','died','family'],newLine)
857 for row in run_query(s,t):
858 b = pU.print_year(row[3])
859 d = pU.print_year(row[4])
863 for r in run_query(s2,(id,)):
866 out+=pU.table_row([pU.name_html(row,newLine),row[2],b,d,fams],\
869 fullIDs.append(row[1])
872 out+=pU.table_foot(newLine)
874 t = ('%' + name + '%','%'+name+'%')
875 out += '%sNames containing %s:%s' %(newLine,name,newLine)
876 out+=pU.table_header(['link','name','born','died','family'],newLine)
877 for row in run_query(s,t):
878 if row[1] not in fullIDs:
879 b = pU.print_year(row[3])
880 d = pU.print_year(row[4])
884 for r in run_query(s2,(id,)):
887 out+=pU.table_row([pU.name_html(row,newLine),\
888 row[2],b,d,fams],newLine)
889 names.append(row[0]+','+row[2])
891 out+=pU.table_foot(newLine)
893 s = '''SELECT name,people.ID,style,fullname,bornYear, diedYear,
894 startYear,stopYear,family
895 FROM people INNER JOIN styles
896 ON styles.id = people.id
897 LEFT JOIN families ON people.ID = families.ID
899 ORDER BY bornYear;'''
901 out += '%sStyles containing %s:%s' %(newLine,name,newLine)
902 out+=pU.table_header(['link','name','born',\
903 'died','style','from','to','family'],newLine)
905 for row in run_query(s,t):
906 b = pU.print_year(row[4])
907 d = pU.print_year(row[5])
908 start = pU.print_year(row[6])
909 stop = pU.print_year(row[7])
910 out+=pU.table_row([pU.name_html(row,newLine),\
911 row[3],b,d,row[2],start,stop,row[8]],newLine)
913 out+=pU.table_foot(newLine)
914 return [out,names,IDs]
916 def count_names(newLine):
917 s="SELECT names.name, count(*),"\
918 +" min(nullif(bornYear,0)),max(nullif(bornYear,0))"\
919 +" FROM names INNER JOIN people on people.ID = names.id"\
920 +" group by names.name"\
923 t = "SELECT count(*) FROM people WHERE firstName = ?"
925 out=pU.table_header(\
926 ['name','count','earliest born',\
927 'latest born','range','count as first name'],\
929 for row in run_query(s,()):
930 for r in run_query(t,(row[0],)):
932 if row[3] is not None and row[2] is not None:
933 range = row[3]-row[2]
936 out+=pU.print_name_count(row,range,c,newLine)
937 out+=pU.table_foot(newLine)
941 def people_with_name(name,newLine):
942 s = "SELECT name, ID,fullname,bornYear,diedYear"\
944 +" WHERE firstname = ?"\
945 +" ORDER BY bornYear;"
949 out='As a first name'+newLine
950 out += pU.table_header(['ID','name','link','born','died'],newLine)
951 for row in run_query(s,t):
952 myName = pU.name_html(row,newLine)
953 by = pU.print_year(row[3])
954 dy = pU.print_year(row[4])
956 out+=pU.table_row([row[1],row[2],myName,by,dy],newLine)
958 out+=pU.table_foot(newLine)
960 s = "SELECT people.name, people.ID,fullname,bornYear,diedYear"\
962 +" LEFT JOIN names ON people.ID = names.ID"\
963 +" WHERE names.name=? AND firstName<>?"\
964 +" ORDER BY bornYear;"
967 out+=newLine+'As a middle name'+newLine
968 out +=pU.table_header(['ID','name','link','born','died'],newLine)
969 for row in run_query(s,t):
970 myName = pU.name_html(row,newLine)
971 by = pU.print_year(row[3])
972 dy = pU.print_year(row[4])
974 out+=pU.table_row([row[1],row[2],myName,by,dy],newLine)
976 out+=pU.table_foot(newLine)
983 def count_children(newLine):
985 s = "SELECT count(*),nc"\
987 +" SELECT count(*) AS nc"\
988 +" FROM parents INNER JOIN people ON parentID = people.ID"\
989 +" GROUP BY parentID"\
990 +" HAVING parentID <>'?'"\
991 +" AND parentID <> '0')"\
995 for row in run_query(s,()):
996 out += pU.print_children_count(row,newLine)
999 def parents_with_children(nChildren,newLine):
1000 s = "SELECT name,parentID"\
1002 + " INNER JOIN people"\
1003 + " ON parentID = people.ID"\
1004 + " GROUP BY parentID"\
1005 + " HAVING count(*) = ?"\
1006 + " AND parentID <> 0"\
1007 + " ORDER BY bornYear;"
1010 u = "SELECT count(*)"\
1011 +" FROM parents INNER JOIN people"\
1012 +" ON parents.ID = people.ID"\
1013 +" WHERE parentID = ?"\
1014 +" AND (diedyear-bornyear>? OR died='present');"
1016 out = "People who had %s %s:%s" \
1017 %(nChildren,eU.print_children(nChildren),newLine)
1019 out+=pU.table_header(['link','survived infancy','survived to adulthood'],newLine)
1020 for row in run_query(s,(nChildren,)):
1021 link = pU.name_html(row,newLine)
1022 for r in run_query(u,(row[1],1)):
1024 for r in run_query(u,(row[1],18)):
1026 out +=pU.table_row([link,infant,adult],newLine)
1027 out+=pU.table_foot(newLine)
1031 def count_age_at_child(newLine):
1033 s = "SELECT age,count(*)"\
1034 +" FROM "+ageChildTable\
1035 +" GROUP BY age ORDER BY age;"
1038 for row in run_query(s,()):
1039 out +=pU.print_age_child_count(row,newLine)
1043 def people_had_child_at_age(age,newLine):
1046 s = "SELECT age,cname,cID,pname,pID,bornYear"\
1047 +" FROM "+ageChildTable\
1051 out = 'At age ' + str(age) + ' :'
1052 out+=pU.table_header(['parent','child','year'],newLine)
1053 for row in run_query(s,t):
1054 parent = pU.name_html([row[3],row[4]],newLine)
1055 child = pU.name_html([row[1],row[2]],newLine)
1056 year = pU.print_year(row[5])
1057 out+=pU.table_row([parent,child,year],newLine)
1058 # out += "%s%s had %s" % (newLine,parent,child)
1059 out+=pU.table_foot(newLine)
1062 def count_age_at_death(newLine):
1065 s = "SELECT age,count(*)"\
1068 +" WHERE diedYear<>?"\
1072 for row in run_query(s,(presentYear,)):
1073 out += pU.print_age_death_count(row,newLine)
1075 s = "select diedYear,count(*)"\
1077 +" WHERE diedYear==?"\
1078 +" GROUP BY diedYear;"
1079 for row in run_query(s,(presentYear,)):
1080 out += pU.print_age_death_count(row,newLine)
1084 def people_died_at_age(age,newLine):
1087 if age != str(presentYear):
1089 s = "SELECT age,name,ID,diedYear,bornYear"\
1093 +" ORDER BY diedYear;"
1097 out = 'Died at age ' +str(age)+newLine
1098 out+=pU.table_header(['link','in'],newLine)
1099 for row in run_query(s,t):
1100 link = pU.name_html([row[1],row[2]],newLine)
1101 year = pU.print_year(row[3])
1102 out+=pU.table_row([link,year],newLine)
1104 s = "SELECT diedYear, name,ID,bornYear,url"\
1106 +" WHERE diedYear = ?;"
1107 out = 'Still alive'+newLine
1108 out+=pU.table_header(['link','born in','wikipedia'],newLine)
1109 for row in run_query(s,(presentYear,)):
1110 link = pU.name_html([row[1],row[2]],newLine)
1111 born = pU.print_year(row[3])
1115 wlink = '<a href="'+row[4]+'">wikipedia</a>'
1116 out+=pU.table_row([link,born,wlink],newLine)
1117 out+=pU.table_foot(newLine)
1121 def all_ancestors(personID,newLine):
1124 s = "SELECT name,id FROM people WHERE id=?"
1126 out=pU.print_tagged_query("Ancestors of ",s,t,newLine)
1130 allAncestors,levelDict = gQ.ancestors(int(personID))
1132 for level in levelDict.keys():
1134 out += eU.parent_level(level,'parent')+':'+newLine
1136 for a in levelDict[level]:
1138 for r in run_query(s,(a,)):
1139 out+=pU.name_html(r,newLine)+newLine
1143 image = "<img src = ancestorGraph.py?id=%s>" %personID
1144 out +=newLine + image+newLine
1146 return [out, allAncestors,levelDict]
1147 def all_descendants(personID,newLine):
1148 s = "SELECT name,id,bornYear,diedYear FROM people WHERE id=?"
1150 out = pU.print_tagged_query("Descendants of ",s,t,newLine)
1152 allDescendants,levelDict = gQ.descendants(int(personID))
1153 for level in levelDict.keys():
1154 out+=eU.parent_level(level,'child')+':'+newLine
1157 out+=pU.table_header(['Name','Born','Died'],newLine)
1158 for a in levelDict[level]:
1160 for r in run_query(s,(a,)):
1161 n=pU.name_html(r,newLine)
1172 out+=pU.table_row([n,b,d],newLine)
1175 out+=pU.table_foot(newLine)
1177 return [out, allDescendants,levelDict]
1178 def check_relationship(a,b):
1179 s = 'SELECT related FROM marriages'\
1180 +' WHERE ida =? AND idb=? AND related<>0'
1181 t = (min(a,b),max(a,b))
1182 for r in run_query(s,t):
1185 def update_relationship(a,b,r,commit):
1186 s = 'SELECT related FROM marriages'\
1187 +' WHERE ida =? AND idb=?'
1188 t = (min(a,b),max(a,b))
1190 for row in run_query(s,t):
1191 u = "UPDATE marriages SET Related = ?"\
1192 +" WHERE ida = ? AND idb=?;"
1203 def find_relationship(IDA,IDB,commit):
1205 s = "SELECT name,id FROM people WHERE id ==?"
1206 for id in (IDA,IDB):
1208 for row in run_query(s,t):
1209 nameList.append(pU.name_html(row,'<br>'))
1212 mrca,orca,aL,bL = gQ.relationship(IDA,IDB)
1213 related = eU.relationship(aL,bL,nameList)
1214 update_relationship(IDA,IDB,related,commit)
1217 def relation_text(IDA,IDB,newLine):
1221 related = check_relationship(IDA,IDB)
1224 related = find_relationship(IDA,IDB,1)
1229 def common_ancestors(IDA,IDB,newLine):
1230 related = relation_text(IDA,IDB,newLine)
1231 out = related+newLine
1233 if related[-11:]!='not related':
1234 mrca,orca,aL,bL = gQ.relationship(int(IDA),int(IDB))
1235 s = 'SELECT name,id FROM people WHERE id=?'
1236 cText = 'Most recent common ancestors:'+newLine
1238 for row in run_query(s,(c,)):
1239 cText+=pU.name_html(row,newLine)+newLine
1241 findName = re.compile('(<a.*?</a>) and (<a.*?</a>).*')
1242 findNameP = re.compile('(<a.*?</a>) is (<a.*?</a>).*')
1243 found = findName.search(related)
1245 found = findNameP.search(related)
1246 nameA = found.group(1)
1247 nameB = found.group(2)
1248 cText+=nameA+"'s "+eU.parent_level(aL,'parent')\
1251 +eU.parent_level(bL,'parent')
1254 out += newLine+cText
1256 image = "<img src = jointAncestorGraph.py?id=%s&id2=%s&mL=%s>"\
1257 %(IDA,IDB,max(aL,bL))
1259 out +=newLine + image
1264 def list_territories(newLine):
1265 s = "SELECT DISTINCT short"\
1266 +" FROM styleDecomp"\
1271 for row in run_query(s,()):
1274 for i in range(len(eU.maleStyles)):
1275 m = eU.maleStyles[i]+' '
1276 if re.search(m,row[0]):
1282 t = eU.swap_gender(row[0])
1287 for i in range(len(terrs)):
1288 terrs[i] = eU.make_plural(terrs[i])
1291 out += pU.terr_html(terr,newLine,0,0) +newLine
1296 def list_families(newLine):
1297 s = "SELECT family, count(*)"\
1298 +" FROM families GROUP BY family ORDER BY family;"
1299 out = pU.table_header(['family','members'],newLine)
1301 for row in run_query(s,()):
1302 link=pU.fam_html(row[0],newLine)
1304 out+=pU.table_row([link,count],newLine)
1307 out+=pU.table_foot(newLine)
1310 def combine_states(aTerritory):
1314 predeccessorStates = eU.predeccessorStates
1315 successorStates = eU.successorStates
1317 ap = eU.make_male(aTerritory)
1318 while predeccessorStates.has_key(ap):
1319 ap = predeccessorStates[ap][0]
1323 ap = eU.make_male(aTerritory)
1324 while successorStates.has_key(ap):
1325 ap = successorStates[ap]
1330 def people_in(fam,newLine):
1331 s = 'SELECT name,people.id,bornYear,diedYear FROM'\
1332 +' people INNER JOIN families'\
1333 +' ON people.id = families.id'\
1334 +' WHERE family = ? ORDER BY bornYear;'
1337 out='Family: %s' %fam
1338 out+=newLine+newLine
1340 out += pU.table_header(['id','name','born','died'],newLine)
1342 for row in run_query(s,t):
1343 name=pU.name_html(row,newLine)
1353 out+=pU.table_row([row[1],name,b,d],newLine)
1356 out+=pU.table_foot(newLine)
1358 image = "<img src = rulersGraph.py?names=%s>"\
1364 def rulers_of(aTerritory,newLine):
1366 out = pU.terr_html(eU.make_male(aTerritory),newLine,0,0)+newLine
1368 otherStates = combine_states(aTerritory)
1369 if len(otherStates)>0:
1370 out+="Otherwise:"+newLine
1371 for s in otherStates:
1372 out+=pU.terr_html(s,newLine,0,0)+newLine
1374 out += find_rulers(aTerritory,newLine)[0]
1377 image = "<img src = rulersGraph.py?terr=%s>" \
1378 %(re.sub(' ','%20',aTerritory))
1382 def find_rulers(aTerritory,newLine):
1383 places = [eU.make_singular(aTerritory)]
1384 places+=combine_states(aTerritory)
1390 s = "SELECT DISTINCT short"\
1391 +" FROM styleDecomp"\
1392 +" WHERE short LIKE ?"\
1393 +" OR short LIKE ?"\
1395 t = ('%'+p+'%','%'+eU.swap_gender(p)+'%',\
1396 '%'+eU.make_plural(p)+'%')
1399 for row in run_query(s,t):
1400 fullTerr.append(row[0])
1403 if len(fullTerr)==0:
1406 tq = "SELECT styleDecomp.style,name,people.ID,"\
1407 +" startyear,stopyear,short"\
1408 +" FROM styleDecomp INNER JOIN styles"\
1409 +" ON styleDecomp.style = styles.style"\
1410 +" INNER JOIN people"\
1411 +" ON people.ID = styles.ID"\
1412 +" WHERE short IN ("
1414 for i in range(len(fullTerr)):
1417 tq+=" )ORDER BY startyear,stopyear;"
1422 out+=pU.table_header(['link','style','from', 'to','house'],newLine)
1423 for row in run_query(tq,t):
1425 # out +=newLine+row[0]+newLine
1427 fy = pU.print_year(row[3])
1428 ft = pU.print_year(row[4])
1432 u = "SELECT family FROM families WHERE id = ?"
1435 for r in run_query(u,v):
1436 house+=pU.fam_html(r[0],newLine)
1438 out+=pU.table_row([pU.name_html(row[1:],newLine),\
1439 row[0],fy,ft,house],newLine)
1441 if row[2] not in rulers:
1442 # rulers.append(row[2])
1443 rulers = [row[2]]+rulers
1444 out+=pU.table_foot(newLine)
1447 def find_adjacent(terr,start,stop,id):
1448 s = "SELECT name,people.id,stopyear"\
1449 +" FROM people INNER JOIN styles"\
1450 +" ON people.id = styles.id"\
1451 +" INNER JOIN styleDecomp"\
1452 +" ON styles.style = styleDecomp.style"\
1453 +" WHERE short = ? AND stopyear<=? AND stopyear <>0 AND people.id<>?"\
1454 +" ORDER BY stopyear DESC, startyear DESC;"
1460 for row in run_query(s,t):
1461 myPrevious = row[0:2]
1465 t = (eU.swap_gender(terr),start,id)
1466 for row in run_query(s,t):
1468 myPrevious = row[0:2]
1471 u = "SELECT name,people.id,startyear"\
1472 +" FROM people INNER JOIN styles"\
1473 +" ON people.id = styles.id"\
1474 +" INNER JOIN styleDecomp"\
1475 +" ON styles.style = styleDecomp.style"\
1476 +" WHERE short = ? AND startyear>=? AND startyear <>0 AND people.id<>?"\
1477 +" ORDER BY startyear;"
1482 for r in run_query(u,v):
1488 v = (eU.swap_gender(terr),stop,id)
1489 for r in run_query(u,v):
1494 return [myPrevious, myNext]
1497 def find_person(ID):
1498 s = "SELECT name||','||ID, name, ID FROM people WHERE ID=?"
1501 for row in run_query(s,t):
1505 return [Self, selfID,selfName]
1508 def find_parents(ID):
1509 s = "SELECT name, parentID"\
1510 +" FROM parents LEFT JOIN people"\
1511 +" ON people.ID = parentID"\
1512 +" WHERE parents.ID = ?;"
1519 for row in run_query(s,t):
1521 p = row[0] + ',' + str(row[1])
1525 p = row[1] + ',p' + str(ID)
1529 parentIDs.append(pID)
1530 parentNames.append(pN)
1532 if len(parents)>1 and parents[1]==parents[0]:
1533 parents[1] = parents[1] + ' 2'
1535 return [parents,parentIDs,parentNames]
1539 def is_married(IDa, IDb):
1541 s = "SELECT idb FROM marriages WHERE ida =?;"
1545 for row in run_query(s,t):
1546 if int(row[0])==max(IDa,IDb):
1550 def find_spouses(ID):
1559 s = "SELECT ida,a.name,idb,b.name,marriage,end,"\
1560 +" a.bornYear,a.diedYear,b.bornYear,b.diedYear,marriageYear,"\
1561 +" a.diedMonth,a.diedDay,b.diedMonth,b.diedDay"\
1563 +" LEFT JOIN people AS a"\
1565 +" LEFT JOIN people AS b"\
1567 +" WHERE ida = ? OR idb = ?"\
1568 +" ORDER BY marriageYear;"\
1571 for row in run_query(s,t):
1573 if row[0]!=int(ID): #spouse is ida
1575 s = row[1]+","+str(row[0])
1579 s = row[0]+",s"+str(ID)
1582 myDates = [row[1],row[0],row[4],row[5],row[6],\
1583 row[7],row[11],row[12]]
1584 else: #spouse is idb
1586 s = row[3]+","+str(row[2])
1590 s = row[2]+",s"+str(ID)
1593 myDates = [row[3],row[2],row[4],row[5],row[8],row[9],\
1595 for i in range(len(myDates)):
1596 if myDates[i] is None:
1600 spousesID.append(sID)
1601 spousesNames.append(sN)
1602 spouseDates.append(myDates)
1604 return [spouses,spousesID,spousesNames,spouseDates]
1607 def find_children(ID):
1608 s = "SELECT p1.name, p1.ID,p3.parentID,p4.name,p1.bornYear"\
1610 +" INNER JOIN parents p2"\
1611 +" ON p1.ID = p2.ID"\
1612 +" INNER JOIN parents p3"\
1613 +" ON p1.ID = p3.ID"\
1614 +" LEFT JOIN people p4"\
1615 +" ON p3.parentID = p4.ID"\
1616 +" WHERE p2.parentID = ?"\
1617 +" AND p3.parentID<>?"\
1618 +" ORDER BY p1.bornYear,p1.ID;"
1627 for row in run_query(s,t):
1628 c = row[0] + ',' + str(row[1])
1632 childrenBorn.append(born)
1634 op = row[3] + ',' + str(row[2])
1638 op = row[2] + ',s' + str(ID)
1642 nodes.append([c,op])
1643 IDs.append([cID,opID])
1644 names.append([cName,opN])
1646 return [nodes,IDs,names,childrenBorn]
1648 def person_info(personID,newLine):
1659 #Id, Name, Dates, Style, Style-Dates
1660 s = "SELECT id,name,fullname,url,picture,Born,Died,"\
1661 +" bornYear,diedYear,diedMonth,diedDay"\
1662 +" FROM people WHERE ID = ?"
1664 for row in run_query(s,t):
1667 preURL = pU.name_html(['previous',row[0]-1],newLine)
1668 postURL = pU.name_html(['next',row[0]+1],newLine)
1669 mainDiv += 'ID: %s(%s %s)%s' %(row[0],preURL,postURL ,newLine)
1670 mainDiv += pU.print_tagged_name('Name',[row[1], row[0]]\
1672 mainDiv+='Full Name: '+row[2]+newLine
1687 mainDiv += '%sBorn:%s%s '% (newLine,born,newLine)
1692 mainDiv +='Died: %s' % died
1694 if diedYear != 0 and bornYear !=0:
1696 u = "SELECT age FROM"+ageTable\
1699 for row in run_query(u,t):
1700 mainDiv += ", aged %s" %(row[0])
1702 u = "SELECT age FROM" + ageNowTable\
1705 for row in run_query(u,t):
1706 mainDiv +='Still Alive, aged %s' %(row[0])
1712 s = 'SELECT family FROM families WHERE ID = ?'
1714 for row in run_query(s,t):
1715 mainDiv+='Family: %s' % pU.fam_html(row[0],newLine)
1720 s = "SELECT * FROM styles WHERE ID = ?"
1721 for row in run_query(s,t):
1723 mainDiv +='%sStyle: %s%s'%(newLine,row[1],newLine)
1725 mainDiv += 'Territories:%s' % newLine
1727 w = "SELECT short FROM styleDecomp"\
1730 for r in run_query(w,(row[1],)):
1731 [p,n]=find_adjacent(r[0],row[3],row[5],personID)
1733 mainDiv +='%s|'%(pU.name_html(p,newLine))
1735 mainDiv+=pU.terr_html(r[0],newLine,row[3],row[5])
1738 mainDiv+='|%s'%(pU.name_html(n,newLine))
1741 mainDiv += 'From: '+row[2] + newLine
1742 mainDiv += 'To: '+row[4]
1754 [parents,parentIDs,parentNames] = find_parents(personID)
1756 for i in range(len(parents)):
1757 r = [parentNames[i],parentIDs[i]]
1758 mainDiv += pU.print_tagged_name('Parent',r,newLine)
1763 [spouses,spousesID,spousesNames,spouseDates] = find_spouses(personID)
1767 for i in range(len(spouses)):
1768 r = [spousesNames[i],spousesID[i]]
1769 d = spouseDates[i][2:8]
1771 mainDiv += pU.print_tagged_name('Spouse',r,newLine)
1773 mainDiv+='married: '+d[0]+newLine
1775 mainDiv+='marriage dates not yet recorded'\
1778 mainDiv+='marriage ended: '+d[1]+newLine
1782 y = pU.print_year(d[3])
1785 elif d[3]==diedYear and diedYear!=presentYear:
1790 elif d[4]>diedMonth:
1799 mainDiv+='until both of their deaths in %s%s'\
1803 if diedYear==presentYear:
1805 mainDiv+='still married%s'%(newLine)
1807 y = str(pU.print_year(diedYear))
1811 mainDiv+='until %s death in %s%s' %(ot,y,newLine)
1816 # pU.relationship_html(personID,r[1],newLine)\
1822 mStart = int(findYear.find_year(d[0]))
1823 mStop = int(findYear.find_year(d[1]))
1831 ss = "SELECT styles.style, short,startyear,"\
1833 +" FROM styles INNER JOIN styledecomp"\
1834 +" ON styles.style = styledecomp.style"\
1836 st = (spousesID[i],)
1838 for sr in run_query(ss,st):
1844 if mStop<starty and mStop != 0:
1853 if mStop<stopy and mStop !=0:
1855 elif diedYear<=stopy:
1856 to = str(pU.print_year(diedYear))
1862 marriageStyles+='%d to %s: %s%s' \
1863 %(fromy, to,eU.swap_gender(sr[1]),newLine)
1865 if len(marriageStyles)>0:
1866 mainDiv+="Through this marriage:"+newLine
1867 mainDiv+=marriageStyles+newLine
1869 mainDiv = mainDiv + newLine+endP
1872 [nodes,IDs,names,childrenBorn] = \
1873 find_children(personID)
1876 for i in range(len(nodes)):
1877 cr = [names[i][0],IDs[i][0]]
1878 thisChild = pU.print_tagged_name('Child',cr,newLine)
1880 opr=[names[i][1],IDs[i][1]]
1882 if i==0 or top != names[i-1][1]:
1885 mainDiv += pU.print_tagged_name\
1886 ('With',opr, newLine)
1889 #age when child born
1890 cb = childrenBorn[i]
1891 if cb!=0 and bornYear != 0:
1893 cs = "SELECT age,pID,cID FROM "+ageChildTable\
1894 +"WHERE pID=? AND cID =?"
1895 ct = (personID,IDs[i][0])
1897 for row in run_query(cs,ct):
1898 thisChild = thisChild[:-4]+\
1899 " at the age of "+str(row[0]) +newLine
1900 mainDiv += thisChild
1905 if newLine == '<br>':
1906 output = '<div id = "main" style = " float:left;width:75%">';
1907 output += mainDiv + "</div>"
1909 output += "<div id = 'image' "\
1910 +"style = 'float:left; margin-left:20px'>"
1914 imageDiv += "<a href=" + url+">"\
1915 +"<img src=" + picture +" alt = 'wiki link'"\
1916 +" title = 'wiki link'></a>"\
1919 elif url!='.' and url!='. ':
1920 imageDiv += "<a href=" + url +">"\
1921 +name + " (wiki link)</a>"+newLine
1923 output += imageDiv + "</div>"
1926 url = 'http://www.chiark.greenend.org.uk/ucgi/~naath/'\
1930 form += "<form id ='controlForm'"\
1931 +" action ="+ url +" method = 'get'>"
1934 "<input type = 'hidden' name = 'ID' value = "\
1938 "Generations of Parents: "\
1939 +"<input type = 'text' name = 'pl' value='1'>"
1942 "Generations of Children: "\
1943 +" <input type = 'text' name = 'cl' value = '1'>"
1946 "Show siblings: <select name = 's'>"+\
1947 "<option value = '0'>No</option>"+\
1948 "<option value = '1'>Yes</option>"+\
1952 "Show spouse's other spouses: <select name = 'os'>"+\
1953 "<option value = '0'>No</option>"+\
1954 "<option value = '1'>Yes</option>"+\
1958 "Show parents' other spouses: <select name = 'pos'>"+\
1959 "<option value = '0'>No</option>"+\
1960 "<option value = '1'>Yes</option>"+\
1965 "<input type = 'text' name = 'fs' value='8'>"
1969 graph = "smallGraph.py?ID="+str(personID)+"&fs=8"
1971 graph = "<img src ="+ graph + '>'
1973 output += "<div id = 'graph' style = 'clear:both'>"
1974 output += "<p id = 'agraph'>"+graph+"</p>"
1975 output += "Draw this graph with more relatives:"
1976 output += newLine + form
1978 output += "<button onclick='myFunction()'>"+\
1985 'function myFunction()'+\
1987 'var x = document.getElementById("controlForm");'+\
1988 'var txt = "<img src = " + x.action + "?";'+\
1989 'for (var i=0;i<x.length;i++)'+\
1991 'var n=x.elements[i].name;'+\
1992 'var v=x.elements[i].value;'+\
1993 'txt = txt + "&"+n+"="+v;'+\
1995 'txt = txt + ">";'+\
1996 'document.getElementById("agraph").innerHTML=txt;'+\
2007 conn = sqlite3.connect\
2008 ('/home/naath/familyTreeProject/familyTree/tree.db')
2009 conn.cursor().execute('PRAGMA journal_mode = WAL')
2012 return conn.cursor()
2014 def commit_changes():