[pyclutter/wip/introspection] container: Implement __getitem__ and __setitem__
- From: Bastian Winkler <bwinkler src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pyclutter/wip/introspection] container: Implement __getitem__ and __setitem__
- Date: Tue, 6 Dec 2011 18:24:28 +0000 (UTC)
commit 43dc7118bbbb368641e8d357f3fa202e523923c0
Author: Bastian Winkler <buz netbuz org>
Date: Tue Dec 6 08:55:57 2011 -0600
container: Implement __getitem__ and __setitem__
introspection/Clutter.py | 39 +++++++++++++++++++++++++++++++++++++--
1 files changed, 37 insertions(+), 2 deletions(-)
---
diff --git a/introspection/Clutter.py b/introspection/Clutter.py
index 51839df..6417a76 100644
--- a/introspection/Clutter.py
+++ b/introspection/Clutter.py
@@ -594,8 +594,43 @@ class Container(Clutter.Container):
def __iter__(self):
return iter(self.get_children())
- def __getitem__(self, index):
- return self.get_children()[index]
+ def __getitem__(self, key):
+ children = self.get_children()
+ n_children = len(children)
+ if isinstance(key, int):
+ if key < 0:
+ key += n_children
+ if key < 0 or key >= n_children:
+ raise IndexError("index out of range: %d" % key)
+ return children[key]
+ elif isinstance(key, slice):
+ start, stop, step = key.indices(n_children)
+ ret = []
+ for i in range(start, stop, step):
+ ret.append(children[i])
+ return ret
+ else:
+ raise TypeError("indices must be integer or slice")
+
+ def __setitem__(self, key, value):
+ children = self.get_children()
+ n_children = len(children)
+ if isinstance(key, int):
+ if key < 0:
+ key += n_children
+ if key >= n_children:
+ raise IndexError("index out of range: %d" % key)
+ old = children[key]
+ if key < n_children:
+ silbing = children[key + 1]
+ else:
+ silbing = None
+ self.remove(old)
+ self.add(value)
+ if silbing:
+ self.lower_child(value, silbing)
+ else:
+ raise TypeError("indices must be integer")
def add(self, *actors):
for actor in actors:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]