After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 766580 - treemodel interaction with selective lists
treemodel interaction with selective lists
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2016-05-17 20:21 UTC by Marinus Schraal
Modified: 2016-06-06 13:29 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Gtk: allow sequence treemodel overrides (2.69 KB, patch)
2016-05-20 00:00 UTC, Marinus Schraal
none Details | Review
overrides: allow treemodel sequence shorthands (4.83 KB, patch)
2016-05-21 09:28 UTC, Marinus Schraal
none Details | Review
overrides: allow treemodel sequence shorthands (4.07 KB, patch)
2016-06-04 19:00 UTC, Marinus Schraal
committed Details | Review

Description Marinus Schraal 2016-05-17 20:21:46 UTC
It's possible to do getting/setting of values with treemodel[iter][row] or treemodel[iter][:].

In gnome-music we often want only a specific set of rows set or retrieved, like 

treemodel.set(iter, [0, 1, 4, 5]) = [x, y, z, foo]

This is currently not supported in pygobject shorthand afaics:

treemodel[iter][0, 1, 4, 5] = [x, y, z, foo]

Since we want to shift gnome-music to using the pygi shorthand notation for treemodel interaction, it would be nice to have this as well.
Comment 1 Marinus Schraal 2016-05-20 00:00:53 UTC
Created attachment 328237 [details] [review]
Gtk: allow sequence treemodel overrides

Add get_ and set_ override for sequences of GtkTreeModel indices. This
allows an arbitray list if indeces to be retrieved in one go from a
GtkTreeModel.


First go at this, seems to work fine in some limited testing. Maybe 
needs a testcase as well?
Comment 2 Marinus Schraal 2016-05-21 09:28:26 UTC
Created attachment 328304 [details] [review]
overrides: allow treemodel sequence shorthands

Add get_ and set_ overrides for sequences of GtkTreeModel indices. This
allows an arbitray list of indeces 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]
Comment 3 Marinus Schraal 2016-05-21 09:37:59 UTC
The new patch adds tests.

One thing I'm not 100% sure about is that a 1 element sequence is actually handled by the single element isIsinstance(key, int). This part could be removed in favor of the sequence handling as it is just a special case imo. See also the test line in the patch in comment #2: 1597.

Related to that is the fact that the single element list handling allows negative values, I guess as a service to the user to allow for the pythonic 'start from the end of the sequence' (row in this case). At first I had this covered for sequences as well, but then I removed it as it is completely undocumented and might be confusing. But it would be trivial to add back.
Comment 4 Christoph Reiter (lazka) 2016-06-02 08:42:23 UTC
Review of attachment 328304 [details] [review]:

::: gi/overrides/Gtk.py
@@ +1080,3 @@
                 alist.append(self.model.get_value(self.iter, i))
             return alist
+        elif isinstance(key, tuple):

This could be simplified to

return [self[k] for k in key]

with the bonus that can handle a mix of indices and slices, like [1,2,3:-1]

@@ +1091,2 @@
         else:
+            raise TypeError("indices must be integers, slice or list, not %s" % type(key).__name__)

passing lists doesn't work so I would use "tuple" here to avoid confusion

@@ +1109,3 @@
             for i, v in enumerate(indexList):
                 self.model.set_value(self.iter, v, value[i])
+        elif isinstance(key, tuple):

Similar to above, can be reduced to:

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

@@ +1122,2 @@
         else:
+            raise TypeError("indices must be an integer, slice or list, not %s" % type(key).__name__)

see above

::: tests/test_overrides_gtk.py
@@ +1630,3 @@
+
+        def set_row4():
+            model[0][0, 1, 1] = (0, "zero", "one")

While not very useful, I don't think this should be considered invalid usage.
Comment 5 Marinus Schraal 2016-06-04 19:00:40 UTC
Created attachment 329135 [details] [review]
overrides: allow treemodel sequence shorthands

Add get_ and set_ overrides for sequences of GtkTreeModel indices. This
allows an arbitray list of indeces 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]
Comment 6 Marinus Schraal 2016-06-04 19:06:09 UTC
(In reply to Christoph Reiter (lazka) from comment #4)
Thanks for the review.

Your solution is so much cleaner, shorter and nicer, so use that instead.

> 
> ::: tests/test_overrides_gtk.py
> @@ +1630,3 @@
> +
> +        def set_row4():
> +            model[0][0, 1, 1] = (0, "zero", "one")
> 
> While not very useful, I don't think this should be considered invalid usage.

I dropped it from the patch, I guess this is harder to check for with possibly nested lists of indices.

I was not certain about this either way, but it just seems to me it might prove the source of hard to track down bugs if we allow something that makes little sense in practice and is so position dependant.
Comment 7 Christoph Reiter (lazka) 2016-06-05 17:08:44 UTC
Review of attachment 329135 [details] [review]:

lgtm
Comment 8 Marinus Schraal 2016-06-06 13:29:46 UTC
This problem has been fixed in the unstable development version. The fix will be available in the next major software release. You may need to upgrade your Linux distribution to obtain that newer version.