Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 55 lines (42 sloc) 1.569 kb
129be7e @malcolmt Initial code examples for modeling challenges talk.
authored
1 """
2 Some sample date data to use for experimenting with the Date and DateRange
3 models.
4
5 This data is loaded as part of an initial fixture if you ran "syncdb", but the
6 code is included here in order to regenerate things from scratch.
7 """
8
9 from datetime import date, datetime
10
11 from dates import models
12
13
14 # All dates are in DD/MM/YYYY format. North Americans will have to mentally
15 # convert.
16 DATES = (
17 ("01/11/1937", 0), # precise
18 ("01/08/1891", 1), # month
19 ("11/11/1975", 2), # year
20 ("5/4/1940", 3), # decade
21 ("1/1/1000", 4), # century
22 )
23
24 DATE_RANGES = (
25 ("1/1/0101", 4, "1/1/0400", 4),
26 ("2/2/1201", 4, "30/6/1752", 3),
27 ("1/1/1905", 3, "8/9/2010", 0),
28 )
29
30 OPEN_RANGES = (
31 ("2/2/1201", 4),
32 ("1/1/1905", 3),
33 )
34
35 def load_samples():
36 for date_str, prec in DATES:
37 date = datetime.strptime(date_str, "%d/%m/%Y").date()
38 models.Date(date=date, precision=prec).save()
39
40 for date1_str, prec1, date2_str, prec2 in DATE_RANGES:
41 date1 = datetime.strptime(date1_str, "%d/%m/%Y").date()
42 date2 = datetime.strptime(date2_str, "%d/%m/%Y").date()
43 obj1 = models.Date.objects.create(date=date1, precision=prec1)
44 obj2 = models.Date.objects.create(date=date2, precision=prec2)
45 models.DateRange(start=obj1, end=obj2).save()
46
47 for date_str, prec in OPEN_RANGES:
48 date = datetime.strptime(date_str, "%d/%m/%Y").date()
49 obj = models.Date.objects.create(date=date, precision=prec)
50 models.DateRange(start=obj).save()
51
52 if __name__ == "__main__":
53 load_samples()
54
Something went wrong with that request. Please try again.