chiark / gitweb /
Added list by number of children; refarctored other list-by-count
[familyTree.git] / familyTree / askQuestion.py
index a5a6b3082b756875be7ef7ff4aeec282545c6c27..d0380d734cbf9bcc435130f860f536d2ba6b3deb 100755 (executable)
@@ -124,7 +124,7 @@ def print_people(n):
 
 def print_age_child_count(row,newLine):
        if newLine == '<br>':
-               script = "age.py?age="+str(row[0])
+               script = "listAge.py?age="+str(row[0])
                link = link_Template.substitute(script = \
                        script, title = add_quotes(row[0]), text = row[0])
                out = str(row[1])+print_people(row[1])
@@ -136,7 +136,7 @@ def print_age_child_count(row,newLine):
 
 def print_age_death_count(row,newLine):
        if newLine =='<br>':
-               script = "ageDeath.py?age="+str(row[0])
+               script = "listAgeDeath.py?age="+str(row[0])
                link = link_Template.substitute(script = script,\
                        title = add_quotes(row[0]),text = row[0])
                out = str(row[1])+print_people(row[1])
@@ -154,6 +154,34 @@ def print_name_count(row,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 ' 
+
+       if newLine == '<br>':
+               script = "listChildCount.py?nc="+str(row[1])
+               link = link_Template.substitute(script =\
+                       script, title = add_quotes(row[1]),text = row[1])
+       else:
+               link = str(row[1])
+
+       out = out + link
+
+       if row[1] == 1:
+               out = out  + ' child '
+       else:
+               out = out + ' children '
+
+
+       out  = out  + newLine
+
+       return out
+
 def print_tagged_name(relationship,row,newLine):
        if row[0]==None:
                        out = relationship + " not yet entered: " + row[1]
@@ -338,6 +366,59 @@ def count_names(newLine):
        return out
 
 
+
+def count_children(newLine):
+
+       s = "SELECT count(*),nc"\
+       +" FROM ("\
+       +" SELECT count(*) AS nc"\
+       +" FROM parents"\
+       +" GROUP BY parentID"\
+       +" HAVING parentID <>'?'"\
+       +" AND parentID <> '0')"\
+       +" GROUP BY nc;"
+
+       out = ''
+       for row in run_query(s,()):
+               out = out + print_children_count(row,newLine)
+       return out
+
+def parents_with_children(nChildren,newLine):
+       s = "SELECT name,parentID"\
+       + " FROM parents"\
+       + " LEFT JOIN people"\
+       + " ON parentID = people.ID"\
+       + " GROUP BY parentID"\
+       + " HAVING count(*) = ?"\
+       + " AND parentID <> 0"\
+       + " ORDER BY bornYear;"
+
+
+       u = "SELECT count(*)"\
+       +" FROM  parents INNER JOIN people"\
+       +" ON parents.ID = people.ID"\
+       +" 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
+
+
+
+       for row in run_query(s,(nChildren,)):
+               out = 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 = out +newLine
+       
+       return out
+
 def search_name(name,newLine):
        s = "SELECT name, ID"\
         +" FROM people"\
@@ -475,6 +556,7 @@ def count_age_at_death(newLine):
                out = out + print_age_death_count(row,newLine)
 
        return out
+
 def people_died_at_age(age,newLine):
        s = "SELECT diedYear-bornYear as age, name,ID"\
                +" FROM people"\
@@ -742,11 +824,11 @@ def isKing(ID):
        t = (ID,)       
 
        k = 0
+       spellingsOfKing = ['King','Queen','king','queen']
        for row in run_query(s,t):
-               a=re.match('.*King',row[0])
-               b=re.match('.*Queen',row[0])
-               if a!=None or b!=None:
-                       k=1
+               for s in spellingsOfKing:
+                       if re.match('.*'+s,row[0]) != None:
+                               k = 1
 
        return k