Consolidate unit handling and use proxy classes.
[cookbook.git] / util / populate_units.py
diff --git a/util/populate_units.py b/util/populate_units.py
new file mode 100644 (file)
index 0000000..af4daa8
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+
+"""Populate a cookbook database with standard units.
+
+Example usage::
+
+    $ export DJANGO_SETTINGS_MODULE=example.settings
+    $ export PYTHONPATH=.
+    $ python util/populate_units.py
+"""
+
+from cookbook.models import Unit, SI, US
+
+
+for abbrev,name,type,system,scale,offset in (
+    (u'ct', u'count', u'count', SI, 1, 0),
+    (u'g', u'gram', u'mass', SI, 1, 0),
+    (u'L', u'liter', u'volume', SI, 1, 0),
+    (u's', u'second', u'time', SI, 1, 0),
+    (u'\u00b0C', u'degree Celsius', u'temperature', SI, 1, 0),
+
+    (u'srv', u'serving', u'count', US, 1, 0),  # not really a US unit
+    (u'doz', u'dozen', u'count', US, 12, 0),
+    (u'gro', u'gross', u'count', US, 144, 0),
+
+    (u'gr', u'grain', u'mass', US, 0.06480, 0),
+    (u'dr', u'dram', u'mass', US, 1.772, 0),
+    (u'oz', u'ounce', u'mass', US, 28.35, 0),
+    (u'lb', u'pound', u'mass', US, 453.6, 0),
+
+    (u'drop', u'drop', u'volume', US, 0.00005, 0),
+    (u'pinch', u'pinch', u'volume', US, 0.0012325, 0),
+    (u'dash', u'dash', u'volume', US, 0.002465, 0),
+    (u't', u'teaspoon', u'volume', US, 0.00493, 0),
+    (u'T', u'tablespoon', u'volume', US, 0.01479, 0),
+    (u'fl oz', u'fluid ounce', u'volume', US, 0.02957, 0),
+    (u'C', u'cup', u'volume', US, 0.23659, 0),
+    (u'pt', u'pint', u'volume', US, 0.47318, 0),
+    (u'qt', u'quart', u'volume', US, 0.94635, 0),
+    (u'gal', u'gallon', u'volume', US, 3.78541, 0),
+
+    # These time units are not really US, but they are certainly not SI.
+    (u'min', u'minute', u'time', US, 60, 0),
+    (u'hr', u'hour', u'time', US, 3600, 0),
+    (u'day', u'day', u'time', US, 86400, 0),        # for "rest overnight"
+    (u'wk', u'', u'time', US, 604800, 0),           # beer
+    (u'month', u'month', u'time', US, 2629800, 0),  # cheese
+    (u'yr', u'year', u'time', US, 31557600, 0),     # wine
+
+    (u'\u00b0F', u'degree Farenheit', u'temperature', US, 1/1.8, 32),
+    ):
+    u = Unit(name=name, abbrev=abbrev, type=type, scale=scale, offset=offset)
+    u.save()