From: jblum Date: Sun, 1 Feb 2009 03:37:13 +0000 (+0000) Subject: insert for odict X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=80fb5db88469ed5c80246846a2086338af7caea4;p=debian%2Fgnuradio insert for odict git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@10368 221aa14e-8319-0410-a670-987f0aec2ac5 --- diff --git a/grc/src/utils/__init__.py b/grc/src/utils/__init__.py index 21af4a8b..73a2ed56 100644 --- a/grc/src/utils/__init__.py +++ b/grc/src/utils/__init__.py @@ -46,6 +46,32 @@ class odict(DictMixin): copy_dict._keys = list(self._keys) return copy_dict + def insert_after(self, pos_key, key, val): + """ + Insert the new key, value entry after the entry given by the position key. + If the positional key is None, insert at the end. + @param pos_key the positional key + @param key the key for the new entry + @param val the value for the new entry + """ + index = (pos_key is None) and len(self._keys) or self._keys.index(pos_key) + assert key not in self._keys + self._keys.insert(index+1, key) + self._data[key] = val + + def insert_before(self, pos_key, key, val): + """ + Insert the new key, value entry before the entry given by the position key. + If the positional key is None, insert at the begining. + @param pos_key the positional key + @param key the key for the new entry + @param val the value for the new entry + """ + index = (pos_key is not None) and self._keys.index(pos_key) or 0 + assert key not in self._keys + self._keys.insert(index, key) + self._data[key] = val + def exists_or_else(d, key, alt): if d.has_key(key): return d[key] else: return alt