Consolidate unit handling and use proxy classes.
[cookbook.git] / util / populate_units.py
1 #!/usr/bin/env python
2
3 """Populate a cookbook database with standard units.
4
5 Example usage::
6
7     $ export DJANGO_SETTINGS_MODULE=example.settings
8     $ export PYTHONPATH=.
9     $ python util/populate_units.py
10 """
11
12 from cookbook.models import Unit, SI, US
13
14
15 for abbrev,name,type,system,scale,offset in (
16     (u'ct', u'count', u'count', SI, 1, 0),
17     (u'g', u'gram', u'mass', SI, 1, 0),
18     (u'L', u'liter', u'volume', SI, 1, 0),
19     (u's', u'second', u'time', SI, 1, 0),
20     (u'\u00b0C', u'degree Celsius', u'temperature', SI, 1, 0),
21
22     (u'srv', u'serving', u'count', US, 1, 0),  # not really a US unit
23     (u'doz', u'dozen', u'count', US, 12, 0),
24     (u'gro', u'gross', u'count', US, 144, 0),
25
26     (u'gr', u'grain', u'mass', US, 0.06480, 0),
27     (u'dr', u'dram', u'mass', US, 1.772, 0),
28     (u'oz', u'ounce', u'mass', US, 28.35, 0),
29     (u'lb', u'pound', u'mass', US, 453.6, 0),
30
31     (u'drop', u'drop', u'volume', US, 0.00005, 0),
32     (u'pinch', u'pinch', u'volume', US, 0.0012325, 0),
33     (u'dash', u'dash', u'volume', US, 0.002465, 0),
34     (u't', u'teaspoon', u'volume', US, 0.00493, 0),
35     (u'T', u'tablespoon', u'volume', US, 0.01479, 0),
36     (u'fl oz', u'fluid ounce', u'volume', US, 0.02957, 0),
37     (u'C', u'cup', u'volume', US, 0.23659, 0),
38     (u'pt', u'pint', u'volume', US, 0.47318, 0),
39     (u'qt', u'quart', u'volume', US, 0.94635, 0),
40     (u'gal', u'gallon', u'volume', US, 3.78541, 0),
41
42     # These time units are not really US, but they are certainly not SI.
43     (u'min', u'minute', u'time', US, 60, 0),
44     (u'hr', u'hour', u'time', US, 3600, 0),
45     (u'day', u'day', u'time', US, 86400, 0),        # for "rest overnight"
46     (u'wk', u'', u'time', US, 604800, 0),           # beer
47     (u'month', u'month', u'time', US, 2629800, 0),  # cheese
48     (u'yr', u'year', u'time', US, 31557600, 0),     # wine
49
50     (u'\u00b0F', u'degree Farenheit', u'temperature', US, 1/1.8, 32),
51     ):
52     u = Unit(name=name, abbrev=abbrev, type=type, scale=scale, offset=offset)
53     u.save()