-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv_iterate.py
234 lines (180 loc) · 6.52 KB
/
csv_iterate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import csv
import datetime
from pathlib import Path
from typing import Iterator
import utils
def LoadMin(s):
"""Convert s to an integer defaulting to 1 if it is None or <= 0"""
if not s:
return 1
num = int(s)
return max(num, 1)
def ParseInt(int_str : str) -> int | None:
if not int_str:
return None
return int(int_str)
def ParseBool(bool_str : str) -> bool | None:
if bool_str == "1":
return True
elif bool_str == "0":
return False
else:
return None
def ParseDate(date_str : str) -> datetime.date | None:
if not date_str or date_str == "0":
return None
year = int(date_str[:4])
# TODO: What do we want to do with month/day not specified?
month = LoadMin(date_str[4:6])
day = LoadMin(date_str[6:])
try:
return datetime.date(year, month, day)
except:
print(date_str, year, month, day)
raise
def ParseTimestamp(timestamp_str : str) -> datetime.datetime | None:
if not timestamp_str or timestamp_str == "0":
return None
year = int(timestamp_str[:4])
month = int(timestamp_str[4:6])
day = int(timestamp_str[6:8])
hour = int(timestamp_str[8:10])
min = int(timestamp_str[10:12])
sec = int(timestamp_str[12:])
try:
return datetime.datetime(year, month, day, hour, min, sec)
except:
print(timestamp_str, year, month, day, hour, min, sec)
raise
def StringOrNone(s : str) -> str | None:
if s:
return s
else:
# If string is empty, return None.
return None
class Row:
def __init__(self, row, key) -> None:
self.row = row
self.key = key
def lookup(self, col_name : str):
try:
return self.row[self.key[col_name]]
except (IndexError, KeyError):
return None
class UserRow(Row):
def user_num(self):
return ParseInt(self.lookup("User ID"))
def wikitree_id(self) -> str:
return self.lookup("WikiTree ID_DB")
def father_num(self) -> int | None:
return ParseInt(self.lookup("Father"))
def mother_num(self) -> int | None:
return ParseInt(self.lookup("Mother"))
def birth_name(self) -> str:
first_name = self.lookup("First Name")
if not first_name:
first_name = self.lookup("Preferred Name")
if not first_name:
first_name = "(Unlisted)"
return first_name + " " + self.lookup("Last Name at Birth")
def gender_code(self) -> int | None:
# Note: Gender is stored as 0 = Blank, 1 = Male, 2 = Female.
# Also possible is "" which might mean private info?
return ParseInt(self.lookup("Gender"))
def birth_date(self) -> datetime.date | None:
return ParseDate(self.lookup("Birth Date"))
def death_date(self) -> datetime.date | None:
return ParseDate(self.lookup("Death Date"))
def birth_location(self) -> str | None:
return StringOrNone(self.lookup("Birth Location"))
def death_location(self) -> str | None:
return StringOrNone(self.lookup("Death Location"))
def no_more_children(self) -> bool | None:
return ParseBool(self.lookup("No Children"))
def no_more_siblings(self) -> bool | None:
return ParseBool(self.lookup("No Children"))
# TODO: Doesn't exist yet.
# def no_more_spouses(self) -> bool | None:
# return ParseBool(self.lookup("No Spouses"))
def registered_time(self) -> datetime.datetime | None:
return ParseTimestamp(self.lookup("Registration"))
def touched_time(self) -> datetime.datetime | None:
return ParseTimestamp(self.lookup("Touched"))
def edit_count(self) -> int | None:
return ParseInt(self.lookup("Edit Count"))
def privacy_level(self) -> int | None:
""" Levels:
"UNLISTED": 10,
"PRIVATE": 20,
"SEMIPRIVATE_BIO": 30,
"SEMIPRIVATE_TREE": 35,
"SEMIPRIVATE_BIOTREE": 40,
"PUBLIC": 50,
"OPEN": 60
"""
return ParseInt(self.lookup("Privacy"))
def manager_num(self) -> int | None:
return ParseInt(self.lookup("Manager"))
def iterate_users_file(filename : Path) -> Iterator[UserRow]:
with open(filename, "r") as f:
reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
# First, figure out the order of column names. These change
# over time as new columns are added, so we cannot hardcode values.
header = next(reader)
key = {}
for index, name in enumerate(header):
key[name] = index
# Now iterate through all data rows.
for row in reader:
yield UserRow(row, key)
def iterate_users(*, version : str, only_custom : bool = False) -> Iterator[UserRow]:
custom_user_nums : set[int] = set()
# Note: We don't use version dir for custom_users. Just use global one.
for user in iterate_users_file(Path("data/custom_users.csv")):
custom_user_nums.add(user.user_num())
yield user
if not only_custom:
for user in iterate_users_file(Path(utils.data_version_dir(version),
"dump_people_users.csv")):
# Ignore "official" versions of any custom defined users.
if user.user_num() not in custom_user_nums:
yield user
class MarriageRow(Row):
def user_nums(self) -> list[int]:
return [int(self.lookup("User ID1")),
int(self.lookup("UserID2"))]
def marriage_date(self) -> datetime.date | None:
return ParseDate(self.lookup("Marriage Date"))
def marriage_location(self) -> str:
return self.lookup("Marriage Location")
def iterate_marriages_file(filename : Path) -> Iterator[MarriageRow]:
with open(filename, "r") as f:
reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
# First, figure out the order of column names. These change
# over time as new columns are added, so we cannot hardcode values.
header = next(reader)
key = {}
for index, name in enumerate(header):
key[name] = index
# Now iterate through all data rows.
for row in reader:
yield MarriageRow(row, key)
def iterate_marriages(*, version : str, only_custom : bool = False) -> Iterator[MarriageRow]:
if not only_custom:
for marriage in iterate_marriages_file(Path(utils.data_version_dir(version),
"dump_people_marriages.csv")):
yield marriage
# Note: We don't use version dir for custom_users. Just use global one.
for marriage in iterate_marriages_file(Path("data/custom_marriages.csv")):
yield marriage
if __name__ == "__main__":
import sys
import time
id = sys.argv[1]
i = 0
for user in iterate_users(version = "default"):
i += 1
if (i % 1000000) == 0:
print("... Read", i, "records", time.process_time())
if user.wikitree_id() == id:
print(user.wikitree_id(), user.user_num(), user.father_num(), user.mother_num())