[gom] python: Make ResourceGroup a sequence



commit ed722a3361e89ce3c71c3f6b930470aa93fdbd06
Author: Mathieu Bridon <bochecha daitauha fr>
Date:   Sat Aug 1 16:31:39 2015 +0200

    python: Make ResourceGroup a sequence
    
    This is a simple GI overrides, which allows iterating directly over a
    Gom.ResourceGroup, accessing items as if it were a list, and getting its
    length, all in the natural Python way.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753137

 bindings/python/gi/overrides/Gom.py |   20 ++++++++++++++++++++
 examples/gom.py                     |    9 ++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)
---
diff --git a/bindings/python/gi/overrides/Gom.py b/bindings/python/gi/overrides/Gom.py
index c89a125..fde0746 100644
--- a/bindings/python/gi/overrides/Gom.py
+++ b/bindings/python/gi/overrides/Gom.py
@@ -17,6 +17,26 @@
 
 
 from ..module import get_introspection_module
+from ..overrides import override
 
 
 Gom = get_introspection_module('Gom')
+
+
+__all__ = [
+    'ResourceGroup',
+    ]
+
+
+class ResourceGroupOverride(Gom.ResourceGroup):
+    def __len__(self):
+        return self.get_count()
+
+    def __getitem__(self, index):
+        if index >= self.get_count():
+            raise IndexError()
+
+        return self.get_index(index)
+
+
+ResourceGroup = override(ResourceGroupOverride)
diff --git a/examples/gom.py b/examples/gom.py
index 2417325..f6ea9c5 100755
--- a/examples/gom.py
+++ b/examples/gom.py
@@ -49,12 +49,11 @@ if __name__ == '__main__':
     sorting = Gom.Sorting()
     sorting.add(ItemResource, "name", Gom.SortingMode.DESCENDING)
     group = repository.find_sorted_sync(ItemResource, None, sorting)
-    count = group.get_count()
+    count = len(group)
     assert count == 2
 
     group.fetch_sync(0, count)
-    for i in range(count):
-        item = group.get_index(i)
+    for i, item in enumerate(group):
         assert item.name == names[i]
 
     # Fetch only one of them with a filter, asynchronously
@@ -63,7 +62,7 @@ if __name__ == '__main__':
     def fetch_cb(group, result, user_data):
         group.fetch_finish(result)
 
-        item = group.get_index(0)
+        item = group[0]
         assert item.name == "item2"
 
         # Close the database
@@ -74,7 +73,7 @@ if __name__ == '__main__':
     def find_cb(repository, result, user_data):
         group = repository.find_finish(result)
 
-        count = group.get_count()
+        count = len(group)
         assert count == 1
 
         group.fetch_async(0, count, fetch_cb, None)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]