[pygobject] overrides: allow treemodel sequence shorthands
- From: Marinus Schraal <mschraal src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] overrides: allow treemodel sequence shorthands
- Date: Mon, 6 Jun 2016 13:28:13 +0000 (UTC)
commit 76e2e30bc586016eba8bcfdd753dc1f4672dced7
Author: Marinus Schraal <mschraal src gnome org>
Date: Fri May 20 01:26:27 2016 +0200
overrides: allow treemodel sequence shorthands
Add get_ and set_ overrides for sequences of GtkTreeModel indices. This
allows an arbitray list of indices to be retrieved or written in one go
from or to a GtkTreeModel row:
model[0][0, 1] = [True, "Hello"]
[foo, bar] = model[0][2, 7]
https://bugzilla.gnome.org/show_bug.cgi?id=766580
gi/overrides/Gtk.py | 15 ++++++++++++-
tests/test_overrides_gtk.py | 46 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 2 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 05ef403..831401c 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -1079,8 +1079,11 @@ class TreeModelRow(object):
for i in range(start, stop, step):
alist.append(self.model.get_value(self.iter, i))
return alist
+ elif isinstance(key, tuple):
+ return [self[k] for k in key]
else:
- raise TypeError("indices must be integers, not %s" % type(key).__name__)
+ raise TypeError("indices must be integers, slice or tuple, not %s"
+ % type(key).__name__)
def __setitem__(self, key, value):
if isinstance(key, int):
@@ -1099,8 +1102,16 @@ class TreeModelRow(object):
for i, v in enumerate(indexList):
self.model.set_value(self.iter, v, value[i])
+ elif isinstance(key, tuple):
+ if len(key) != len(value):
+ raise ValueError(
+ "attempt to assign sequence of size %d to sequence of size %d"
+ % (len(value), len(key)))
+ for k, v in zip(key, value):
+ self[k] = v
else:
- raise TypeError("index must be an integer or slice, not %s" % type(key).__name__)
+ raise TypeError("indices must be an integer, slice or tuple, not %s"
+ % type(key).__name__)
def _convert_negative_index(self, index):
new_index = self.model.get_n_columns() + index
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index f184152..f47c63b 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1587,6 +1587,52 @@ class TestTreeModel(unittest.TestCase):
self.assertRaises(TypeError, set_row3)
+ def test_tree_row_sequence(self):
+ model = Gtk.ListStore(int, str, float)
+ model.append([1, "one", -0.1])
+
+ self.assertEqual([1, "one", -0.1], model[0][0, 1, 2])
+ self.assertEqual([1, "one"], model[0][0, 1])
+ self.assertEqual(["one", -0.1], model[0][1, 2])
+ self.assertEqual("one", model[0][1])
+ self.assertEqual([1, -0.1], model[0][0, 2])
+ self.assertEqual([-0.1, 1], model[0][2, 0])
+
+ model[0][0, 1, 2] = (2, "two", -0.2)
+ self.assertEqual([2, "two", -0.2], model[0][0, 1, 2])
+
+ model[0][0, 1] = (3, "three")
+ self.assertEqual([3, "three"], model[0][0, 1])
+
+ model[0][1, 2] = ("four", -0.4)
+ self.assertEqual(["four", -0.4], model[0][1, 2])
+
+ model[0][0, 2] = (5, -0.5)
+ self.assertEqual([5, -0.5], model[0][0, 2])
+
+ model[0][0, 1, 2] = (6, "six", -0.6)
+ self.assertEqual([-0.6, 6, "six"], model[0][2, 0, 1])
+
+ def set_row1():
+ model[0][4, 5] = ("shouldn't", "work",)
+
+ self.assertRaises(IndexError, set_row1)
+
+ def set_row2():
+ model[0][0, 1] = (0, "zero", 0)
+
+ self.assertRaises(ValueError, set_row2)
+
+ def set_row3():
+ model[0][0, 1] = ("shouldn't", 0)
+
+ self.assertRaises(TypeError, set_row3)
+
+ def set_row4():
+ model[0][0, "two"] = (0, "zero")
+
+ self.assertRaises(TypeError, set_row4)
+
def test_tree_model_set_value_to_none(self):
# Tests allowing the usage of None to set an empty value on a model.
store = Gtk.ListStore(str)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]