Create a SlotDict constructor which can take an optional positional arg that
authorZac Medico <zmedico@gentoo.org>
Mon, 28 Jul 2008 05:34:07 +0000 (05:34 -0000)
committerZac Medico <zmedico@gentoo.org>
Mon, 28 Jul 2008 05:34:07 +0000 (05:34 -0000)
is passed to the update() method (similar to the dict constructor), and also
pass keyword arguments into the update() method if any are given. This makes
it possible to use the constructor similarly to the way that the
_emerge.SlotObject constructor is used.

svn path=/main/trunk/; revision=11230

pym/portage/cache/mappings.py

index d0ca487f07dca8376512b8178c984bd6bff728c4..75f97d6aed8f7468de81a172d1066595cbe92a9b 100644 (file)
@@ -142,6 +142,19 @@ def slot_dict_class(keys, prefix="_val_"):
                        __slots__ = ("__weakref__",) + \
                                tuple(prefix + k for k in allowed_keys)
 
+                       def __init__(self, *args, **kwargs):
+
+                               if len(args) > 1:
+                                       raise TypeError(
+                                               "expected at most 1 positional argument, got " + \
+                                               repr(1 + len(args)))
+
+                               if args:
+                                       self.update(args[0])
+
+                               if kwargs:
+                                       self.update(kwargs)
+
                        def __iter__(self):
                                for k, v in self.iteritems():
                                        yield k