From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: aliguori@linux.vnet.ibm.com, Jes.Sorensen@redhat.com,
agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com,
lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH v5 10/18] qapi: add ordereddict.py helper library
Date: Tue, 5 Jul 2011 08:02:37 -0500 [thread overview]
Message-ID: <1309870965-27066-11-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1309870965-27066-1-git-send-email-mdroth@linux.vnet.ibm.com>
We need this to parse dictionaries with schema ordering intact so that C
prototypes can be generated deterministically.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
scripts/ordereddict.py | 128 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 128 insertions(+), 0 deletions(-)
create mode 100644 scripts/ordereddict.py
diff --git a/scripts/ordereddict.py b/scripts/ordereddict.py
new file mode 100644
index 0000000..e17269f
--- /dev/null
+++ b/scripts/ordereddict.py
@@ -0,0 +1,128 @@
+# Copyright (c) 2009 Raymond Hettinger
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation files
+# (the "Software"), to deal in the Software without restriction,
+# including without limitation the rights to use, copy, modify, merge,
+# publish, distribute, sublicense, and/or sell copies of the Software,
+# and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+from UserDict import DictMixin
+
+class OrderedDict(dict, DictMixin):
+
+ def __init__(self, *args, **kwds):
+ if len(args) > 1:
+ raise TypeError('expected at most 1 arguments, got %d' % len(args))
+ try:
+ self.__end
+ except AttributeError:
+ self.clear()
+ self.update(*args, **kwds)
+
+ def clear(self):
+ self.__end = end = []
+ end += [None, end, end] # sentinel node for doubly linked list
+ self.__map = {} # key --> [key, prev, next]
+ dict.clear(self)
+
+ def __setitem__(self, key, value):
+ if key not in self:
+ end = self.__end
+ curr = end[1]
+ curr[2] = end[1] = self.__map[key] = [key, curr, end]
+ dict.__setitem__(self, key, value)
+
+ def __delitem__(self, key):
+ dict.__delitem__(self, key)
+ key, prev, next = self.__map.pop(key)
+ prev[2] = next
+ next[1] = prev
+
+ def __iter__(self):
+ end = self.__end
+ curr = end[2]
+ while curr is not end:
+ yield curr[0]
+ curr = curr[2]
+
+ def __reversed__(self):
+ end = self.__end
+ curr = end[1]
+ while curr is not end:
+ yield curr[0]
+ curr = curr[1]
+
+ def popitem(self, last=True):
+ if not self:
+ raise KeyError('dictionary is empty')
+ if last:
+ key = reversed(self).next()
+ else:
+ key = iter(self).next()
+ value = self.pop(key)
+ return key, value
+
+ def __reduce__(self):
+ items = [[k, self[k]] for k in self]
+ tmp = self.__map, self.__end
+ del self.__map, self.__end
+ inst_dict = vars(self).copy()
+ self.__map, self.__end = tmp
+ if inst_dict:
+ return (self.__class__, (items,), inst_dict)
+ return self.__class__, (items,)
+
+ def keys(self):
+ return list(self)
+
+ setdefault = DictMixin.setdefault
+ update = DictMixin.update
+ pop = DictMixin.pop
+ values = DictMixin.values
+ items = DictMixin.items
+ iterkeys = DictMixin.iterkeys
+ itervalues = DictMixin.itervalues
+ iteritems = DictMixin.iteritems
+
+ def __repr__(self):
+ if not self:
+ return '%s()' % (self.__class__.__name__,)
+ return '%s(%r)' % (self.__class__.__name__, self.items())
+
+ def copy(self):
+ return self.__class__(self)
+
+ @classmethod
+ def fromkeys(cls, iterable, value=None):
+ d = cls()
+ for key in iterable:
+ d[key] = value
+ return d
+
+ def __eq__(self, other):
+ if isinstance(other, OrderedDict):
+ if len(self) != len(other):
+ return False
+ for p, q in zip(self.items(), other.items()):
+ if p != q:
+ return False
+ return True
+ return dict.__eq__(self, other)
+
+ def __ne__(self, other):
+ return not self == other
+
--
1.7.0.4
next prev parent reply other threads:[~2011-07-05 13:04 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-05 13:02 [Qemu-devel] [QAPI+QGA 2/3] QAPI code generation infrastructure v5 Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 01/18] Add hard build dependency on glib Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 02/18] qlist: add qlist_first()/qlist_next() Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 03/18] qapi: add module init types for qapi Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 04/18] qapi: add QAPI visitor core Michael Roth
2011-07-07 14:32 ` Luiz Capitulino
2011-07-07 14:40 ` Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 05/18] qapi: add QMP input visitor Michael Roth
2011-07-07 14:32 ` Luiz Capitulino
2011-07-07 14:45 ` Michael Roth
2011-07-12 0:05 ` Michael Roth
2011-07-12 13:16 ` Luiz Capitulino
2011-07-12 13:46 ` Michael Roth
2011-07-12 13:53 ` Luiz Capitulino
2011-07-12 14:14 ` Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 06/18] qapi: add QMP output visitor Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 07/18] qapi: add QAPI dealloc visitor Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 08/18] qapi: add QMP command registration/lookup functions Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 09/18] qapi: add QMP dispatch functions Michael Roth
2011-07-05 13:02 ` Michael Roth [this message]
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 11/18] qapi: add qapi.py helper libraries Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 12/18] qapi: add qapi-types.py code generator Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 13/18] qapi: add qapi-visit.py " Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 14/18] qapi: add qapi-commands.py " Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 15/18] qapi: test schema used for unit tests Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 16/18] qapi: add test-visitor, tests for gen. visitor code Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 17/18] qapi: add test-qmp-commands, tests for gen. marshalling/dispatch code Michael Roth
2011-07-05 13:02 ` [Qemu-devel] [PATCH v5 18/18] qapi: add QAPI code generation documentation Michael Roth
2011-07-07 14:37 ` [Qemu-devel] [QAPI+QGA 2/3] QAPI code generation infrastructure v5 Luiz Capitulino
2011-07-07 15:02 ` Michael Roth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1309870965-27066-11-git-send-email-mdroth@linux.vnet.ibm.com \
--to=mdroth@linux.vnet.ibm.com \
--cc=Jes.Sorensen@redhat.com \
--cc=agl@linux.vnet.ibm.com \
--cc=aliguori@linux.vnet.ibm.com \
--cc=lcapitulino@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).