1 |
2620
|
aaronmk
|
--
|
2 |
|
|
-- PostgreSQL database dump
|
3 |
|
|
--
|
4 |
|
|
|
5 |
|
|
SET statement_timeout = 0;
|
6 |
11667
|
aaronmk
|
SET lock_timeout = 0;
|
7 |
2620
|
aaronmk
|
SET client_encoding = 'UTF8';
|
8 |
6213
|
aaronmk
|
SET standard_conforming_strings = on;
|
9 |
2620
|
aaronmk
|
SET check_function_bodies = false;
|
10 |
|
|
SET client_min_messages = warning;
|
11 |
|
|
|
12 |
|
|
--
|
13 |
8183
|
aaronmk
|
-- Name: py_util; Type: SCHEMA; Schema: -; Owner: bien
|
14 |
2620
|
aaronmk
|
--
|
15 |
|
|
|
16 |
8183
|
aaronmk
|
CREATE SCHEMA py_util;
|
17 |
2620
|
aaronmk
|
|
18 |
|
|
|
19 |
8183
|
aaronmk
|
ALTER SCHEMA py_util OWNER TO bien;
|
20 |
3430
|
aaronmk
|
|
21 |
4982
|
aaronmk
|
--
|
22 |
8183
|
aaronmk
|
-- Name: SCHEMA py_util; Type: COMMENT; Schema: -; Owner: bien
|
23 |
4982
|
aaronmk
|
--
|
24 |
|
|
|
25 |
8183
|
aaronmk
|
COMMENT ON SCHEMA py_util IS 'IMPORTANT: Functions must always return NULL in place of '''' (the empty string). This ensures that empty strings do not find their way into VegBIEN.';
|
26 |
4982
|
aaronmk
|
|
27 |
|
|
|
28 |
8183
|
aaronmk
|
SET search_path = py_util, pg_catalog;
|
29 |
2630
|
aaronmk
|
|
30 |
2620
|
aaronmk
|
--
|
31 |
8183
|
aaronmk
|
-- Name: _date(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
|
32 |
4549
|
aaronmk
|
--
|
33 |
|
|
|
34 |
|
|
CREATE FUNCTION _date(date timestamp with time zone) RETURNS timestamp with time zone
|
35 |
|
|
LANGUAGE sql IMMUTABLE STRICT
|
36 |
|
|
AS $_$
|
37 |
|
|
SELECT $1
|
38 |
|
|
$_$;
|
39 |
|
|
|
40 |
|
|
|
41 |
8183
|
aaronmk
|
ALTER FUNCTION py_util._date(date timestamp with time zone) OWNER TO bien;
|
42 |
4549
|
aaronmk
|
|
43 |
|
|
--
|
44 |
11667
|
aaronmk
|
-- Name: _date(integer, integer, integer); Type: FUNCTION; Schema: py_util; Owner: bien
|
45 |
|
|
--
|
46 |
|
|
|
47 |
|
|
CREATE FUNCTION _date(year integer DEFAULT NULL::integer, month integer DEFAULT NULL::integer, day integer DEFAULT NULL::integer) RETURNS text
|
48 |
|
|
LANGUAGE sql IMMUTABLE STRICT
|
49 |
|
|
AS $_$
|
50 |
|
|
SELECT py_util._date($1::text, $2::text, $3::text)
|
51 |
|
|
$_$;
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
ALTER FUNCTION py_util._date(year integer, month integer, day integer) OWNER TO bien;
|
55 |
|
|
|
56 |
|
|
--
|
57 |
8183
|
aaronmk
|
-- Name: _date(text, text, text); Type: FUNCTION; Schema: py_util; Owner: bien
|
58 |
3415
|
aaronmk
|
--
|
59 |
|
|
|
60 |
6218
|
aaronmk
|
CREATE FUNCTION _date(year text DEFAULT NULL::text, month text DEFAULT NULL::text, day text DEFAULT NULL::text) RETURNS text
|
61 |
6204
|
aaronmk
|
LANGUAGE plpython3u IMMUTABLE
|
62 |
3415
|
aaronmk
|
AS $$
|
63 |
|
|
global date, year, month, day
|
64 |
|
|
|
65 |
|
|
import datetime
|
66 |
|
|
|
67 |
3553
|
aaronmk
|
def e_msg(e): return e.args[0].rstrip()
|
68 |
3415
|
aaronmk
|
|
69 |
6218
|
aaronmk
|
# Year is required
|
70 |
|
|
if year == None:
|
71 |
|
|
if month == None and day == None: return None # entire date is empty
|
72 |
|
|
else: raise AssertionError(
|
73 |
|
|
'null value in column "year" violates not-null constraint')
|
74 |
3415
|
aaronmk
|
|
75 |
6218
|
aaronmk
|
# Convert month name to number
|
76 |
|
|
if month != None and not month.isdigit(): # month is name
|
77 |
|
|
month = str(datetime.datetime.strptime(month, '%b').month)
|
78 |
3553
|
aaronmk
|
|
79 |
6218
|
aaronmk
|
if month == None: month = 1
|
80 |
|
|
if day == None: day = 1
|
81 |
|
|
year, month, day = map(int, (year, month, day))
|
82 |
3415
|
aaronmk
|
|
83 |
6218
|
aaronmk
|
for try_num in range(2):
|
84 |
|
|
try:
|
85 |
|
|
date = datetime.date(year, month, day)
|
86 |
|
|
break
|
87 |
|
|
except ValueError as e:
|
88 |
|
|
if try_num > 0: raise # exception still raised after retry
|
89 |
|
|
msg = e_msg(e)
|
90 |
|
|
if msg == 'month must be in 1..12': # try swapping month and day
|
91 |
|
|
month, day = day, month
|
92 |
|
|
else: raise
|
93 |
3415
|
aaronmk
|
|
94 |
3416
|
aaronmk
|
return str(date)
|
95 |
3415
|
aaronmk
|
$$;
|
96 |
|
|
|
97 |
|
|
|
98 |
8183
|
aaronmk
|
ALTER FUNCTION py_util._date(year text, month text, day text) OWNER TO bien;
|
99 |
3430
|
aaronmk
|
|
100 |
3415
|
aaronmk
|
--
|
101 |
8183
|
aaronmk
|
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_util; Owner: bien
|
102 |
2951
|
aaronmk
|
--
|
103 |
|
|
|
104 |
|
|
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
|
105 |
3634
|
aaronmk
|
LANGUAGE sql IMMUTABLE STRICT
|
106 |
2951
|
aaronmk
|
AS $_$
|
107 |
8183
|
aaronmk
|
SELECT (py_util.parse_date_range($1))[2]
|
108 |
2951
|
aaronmk
|
$_$;
|
109 |
|
|
|
110 |
|
|
|
111 |
8183
|
aaronmk
|
ALTER FUNCTION py_util."_dateRangeEnd"(value text) OWNER TO bien;
|
112 |
3430
|
aaronmk
|
|
113 |
2951
|
aaronmk
|
--
|
114 |
8183
|
aaronmk
|
-- Name: _dateRangeEnd(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
|
115 |
4542
|
aaronmk
|
--
|
116 |
|
|
|
117 |
|
|
CREATE FUNCTION "_dateRangeEnd"(value timestamp with time zone) RETURNS timestamp with time zone
|
118 |
|
|
LANGUAGE sql IMMUTABLE STRICT
|
119 |
|
|
AS $_$
|
120 |
|
|
SELECT $1
|
121 |
|
|
$_$;
|
122 |
|
|
|
123 |
|
|
|
124 |
8183
|
aaronmk
|
ALTER FUNCTION py_util."_dateRangeEnd"(value timestamp with time zone) OWNER TO bien;
|
125 |
4542
|
aaronmk
|
|
126 |
|
|
--
|
127 |
8183
|
aaronmk
|
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_util; Owner: bien
|
128 |
2951
|
aaronmk
|
--
|
129 |
|
|
|
130 |
|
|
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
|
131 |
3634
|
aaronmk
|
LANGUAGE sql IMMUTABLE STRICT
|
132 |
2951
|
aaronmk
|
AS $_$
|
133 |
8183
|
aaronmk
|
SELECT (py_util.parse_date_range($1))[1]
|
134 |
2951
|
aaronmk
|
$_$;
|
135 |
|
|
|
136 |
|
|
|
137 |
8183
|
aaronmk
|
ALTER FUNCTION py_util."_dateRangeStart"(value text) OWNER TO bien;
|
138 |
3430
|
aaronmk
|
|
139 |
2951
|
aaronmk
|
--
|
140 |
8183
|
aaronmk
|
-- Name: _dateRangeStart(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
|
141 |
4542
|
aaronmk
|
--
|
142 |
|
|
|
143 |
|
|
CREATE FUNCTION "_dateRangeStart"(value timestamp with time zone) RETURNS timestamp with time zone
|
144 |
|
|
LANGUAGE sql IMMUTABLE STRICT
|
145 |
|
|
AS $_$
|
146 |
|
|
SELECT $1
|
147 |
|
|
$_$;
|
148 |
|
|
|
149 |
|
|
|
150 |
8183
|
aaronmk
|
ALTER FUNCTION py_util."_dateRangeStart"(value timestamp with time zone) OWNER TO bien;
|
151 |
4542
|
aaronmk
|
|
152 |
|
|
--
|
153 |
8183
|
aaronmk
|
-- Name: _namePart(text, text, text); Type: FUNCTION; Schema: py_util; Owner: bien
|
154 |
2952
|
aaronmk
|
--
|
155 |
|
|
|
156 |
|
|
CREATE FUNCTION "_namePart"(first text DEFAULT NULL::text, middle text DEFAULT NULL::text, last text DEFAULT NULL::text) RETURNS text
|
157 |
6204
|
aaronmk
|
LANGUAGE plpython3u IMMUTABLE
|
158 |
2635
|
aaronmk
|
AS $$
|
159 |
2952
|
aaronmk
|
params = dict(first=first, middle=middle, last=last)
|
160 |
2635
|
aaronmk
|
|
161 |
4983
|
aaronmk
|
def none_if(val, *none_vals):
|
162 |
|
|
for none_val in none_vals:
|
163 |
|
|
if val == none_val: return None
|
164 |
|
|
return val
|
165 |
|
|
|
166 |
2635
|
aaronmk
|
_name_parts_slices_items = [
|
167 |
4948
|
aaronmk
|
('first', slice(None, -1)),
|
168 |
2635
|
aaronmk
|
('middle', slice(1, -1)),
|
169 |
|
|
('last', slice(-1, None)),
|
170 |
|
|
]
|
171 |
|
|
name_parts_slices = dict(_name_parts_slices_items)
|
172 |
|
|
name_parts = [name for name, slice_ in _name_parts_slices_items]
|
173 |
|
|
|
174 |
|
|
def _name(items):
|
175 |
|
|
items = dict(items)
|
176 |
|
|
parts = []
|
177 |
|
|
for part in name_parts:
|
178 |
|
|
if part in items: parts.append(items[part])
|
179 |
4983
|
aaronmk
|
return none_if(' '.join(parts), '')
|
180 |
2635
|
aaronmk
|
|
181 |
|
|
out_items = []
|
182 |
2952
|
aaronmk
|
for part, value in params.iteritems():
|
183 |
2635
|
aaronmk
|
if value == None: continue
|
184 |
|
|
|
185 |
|
|
try: slice_ = name_parts_slices[part]
|
186 |
|
|
except KeyError: pass # a non-value column
|
187 |
|
|
else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
|
188 |
|
|
|
189 |
2952
|
aaronmk
|
return _name(out_items)
|
190 |
2635
|
aaronmk
|
$$;
|
191 |
|
|
|
192 |
|
|
|
193 |
8183
|
aaronmk
|
ALTER FUNCTION py_util."_namePart"(first text, middle text, last text) OWNER TO bien;
|
194 |
3430
|
aaronmk
|
|
195 |
2635
|
aaronmk
|
--
|
196 |
8183
|
aaronmk
|
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_util; Owner: bien
|
197 |
2630
|
aaronmk
|
--
|
198 |
|
|
|
199 |
|
|
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
|
200 |
6204
|
aaronmk
|
LANGUAGE plpython3u IMMUTABLE STRICT
|
201 |
2630
|
aaronmk
|
AS $$
|
202 |
|
|
import re
|
203 |
|
|
|
204 |
|
|
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
|
205 |
|
|
|
206 |
|
|
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
|
207 |
|
|
|
208 |
|
|
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
|
209 |
|
|
|
210 |
|
|
range_sep='-'
|
211 |
|
|
part_sep=' '
|
212 |
|
|
|
213 |
4019
|
aaronmk
|
default = (str_, str_)
|
214 |
7107
|
aaronmk
|
if str_.find(':') >= 0: return default
|
215 |
2630
|
aaronmk
|
# range_sep might be used as date part separator instead
|
216 |
|
|
if str_.find(part_sep) < 0: return default
|
217 |
|
|
|
218 |
|
|
start, sep, end = str_.partition(range_sep)
|
219 |
|
|
if sep == '': return default # not a range
|
220 |
|
|
start, end = (single_space(d).split(part_sep) for d in (start, end))
|
221 |
|
|
|
222 |
|
|
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
|
223 |
|
|
if len(start) == 2 and (len(end) == 1 or (
|
224 |
|
|
len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
|
225 |
|
|
and could_be_year(end[-1])
|
226 |
|
|
)):
|
227 |
|
|
end.insert(0, start[0]) # make end fully specified
|
228 |
|
|
ct_diff = len(end) - len(start)
|
229 |
|
|
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
|
230 |
|
|
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
|
231 |
|
|
# Other forms are invalid and will be left as-is
|
232 |
|
|
|
233 |
|
|
return [part_sep.join(d) for d in (start, end)]
|
234 |
|
|
$$;
|
235 |
|
|
|
236 |
|
|
|
237 |
8183
|
aaronmk
|
ALTER FUNCTION py_util.parse_date_range(str_ text) OWNER TO bien;
|
238 |
3430
|
aaronmk
|
|
239 |
2630
|
aaronmk
|
--
|
240 |
2620
|
aaronmk
|
-- PostgreSQL database dump complete
|
241 |
|
|
--
|