]> code.delx.au - bg-scripts/blobdiff - lib/python24_adapter.py
Initial import
[bg-scripts] / lib / python24_adapter.py
diff --git a/lib/python24_adapter.py b/lib/python24_adapter.py
new file mode 100644 (file)
index 0000000..28951e1
--- /dev/null
@@ -0,0 +1,20 @@
+#! python
+
+import collections
+
+class defaultdict(dict):
+       def __init__(self, default_factory):
+               self.default_factory = default_factory
+       def __missing__(self, key):
+               if self.default_factory is None:
+                       raise KeyError(key)
+               self[key] = value = self.default_factory()
+               return value
+       def __getitem__(self, key):
+               try:
+                       return dict.__getitem__(self, key)
+               except KeyError:
+                       return self.__missing__(key)
+
+if not hasattr(collections, 'defaultdict'):
+       collections.defaultdict = defaultdict