chiark / gitweb /
improved handling of ordinary numbers and description of relations
[familyTree.git] / familyTree / marriageSQL.py
1 #!/usr/bin/python
2
3 def add_quotes(s):
4         return '\''+s+'\''
5
6 def is_number(s):
7     try:
8         float(s)
9         return s
10     except ValueError:
11         return add_quotes(s)
12
13 def make_insert(table,fields):
14         s = 'INSERT INTO ' + table + ' VALUES('
15         for f in fields:
16                 s = s + f + ','
17         s = s[:-1]
18         s = s+');'
19         return s
20
21 f = open('tree','r')
22
23 lastline='';
24
25 s = 0;
26 for line in f:
27         thisline = line
28         if thisline[-2:] == '\r\n':
29                 thisline = thisline[:-2]
30         if lastline=='ID:':
31                 thisID = is_number(thisline)
32         if s == 1:
33                 if thisline =='':
34                         s=0
35                         for spouse in spouses:
36                                 try:
37                                         if float(thisID) < float(spouse):
38                                                 s = make_insert('marriages',
39                                                 [thisID,spouse])
40                                                 print s
41                                 except ValueError:
42                                         s = make_insert('marriages',
43                                         [thisID,spouse])
44                                         print s
45
46                 else:
47                         spouses.append(is_number(thisline))
48         if lastline == 'Spouses:':
49                 spouses = [is_number(thisline)]
50                 s = 1;
51
52         lastline = thisline
53
54