[gom] python: Add a working example
- From: Mathieu Bridon <mbridon src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gom] python: Add a working example
- Date: Sat, 8 Aug 2015 12:14:14 +0000 (UTC)
commit 975b7a7da85765f23fa95366d6913763c4a03a55
Author: Mathieu Bridon <bochecha daitauha fr>
Date: Wed Jul 8 23:17:37 2015 +0200
python: Add a working example
The example demonstrates how to use Gom from Python, connecting to a
database, creating resource classes, inserting resource instances,
fetching them back with or without filters, with or without ordering,
synchronously or asynchronously, and then closing the connection.
Some parts don't look very Pythonic, but it could be made better with GI
overrides.
https://bugzilla.gnome.org/show_bug.cgi?id=753137
Makefile.am | 1 +
examples/Makefile.include | 1 +
examples/gom.py | 85 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 87 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 613a7e9..e102598 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,6 @@
include Makefile.tests
include data/Makefile.include
+include examples/Makefile.include
include gom/Makefile.include
include tests/Makefile.include
diff --git a/examples/Makefile.include b/examples/Makefile.include
new file mode 100644
index 0000000..7d4a5e4
--- /dev/null
+++ b/examples/Makefile.include
@@ -0,0 +1 @@
+EXTRA_DIST += examples/gom.py
diff --git a/examples/gom.py b/examples/gom.py
new file mode 100755
index 0000000..2417325
--- /dev/null
+++ b/examples/gom.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python3
+
+
+from gi.types import GObjectMeta
+from gi.repository import GLib
+from gi.repository import GObject
+from gi.repository import Gom
+
+
+# Need a metaclass until we get something like _gclass_init_
+# https://bugzilla.gnome.org/show_bug.cgi?id=701843
+class ItemResourceMeta(GObjectMeta):
+ def __init__(cls, name, bases, dct):
+ super(ItemResourceMeta, cls).__init__(name, bases, dct)
+ cls.set_table("items")
+ cls.set_primary_key("id")
+ cls.set_notnull("name")
+
+
+class ItemResource(Gom.Resource, metaclass=ItemResourceMeta):
+ id = GObject.Property(type=int)
+ name = GObject.Property(type=str)
+
+
+if __name__ == '__main__':
+ # Connect to the database
+ adapter = Gom.Adapter()
+ adapter.open_sync(":memory:")
+
+ # Create the table
+ repository = Gom.Repository(adapter=adapter)
+ repository.automatic_migrate_sync(1, [ItemResource])
+
+ # Insert an item
+ item = ItemResource(repository=repository, name="item1")
+ item.save_sync()
+
+ # Fetch the item back
+ item = repository.find_one_sync(ItemResource, None)
+ assert item.id == 1
+ assert item.name == 'item1'
+
+ # Insert a new item
+ item = ItemResource(repository=repository, name="item2")
+ item.save_sync()
+
+ # Fetch them all with a None filter, ordered by name
+ names = ['item2', 'item1']
+ sorting = Gom.Sorting()
+ sorting.add(ItemResource, "name", Gom.SortingMode.DESCENDING)
+ group = repository.find_sorted_sync(ItemResource, None, sorting)
+ count = group.get_count()
+ assert count == 2
+
+ group.fetch_sync(0, count)
+ for i in range(count):
+ item = group.get_index(i)
+ assert item.name == names[i]
+
+ # Fetch only one of them with a filter, asynchronously
+ loop = GLib.MainLoop()
+
+ def fetch_cb(group, result, user_data):
+ group.fetch_finish(result)
+
+ item = group.get_index(0)
+ assert item.name == "item2"
+
+ # Close the database
+ adapter.close_sync()
+
+ loop.quit()
+
+ def find_cb(repository, result, user_data):
+ group = repository.find_finish(result)
+
+ count = group.get_count()
+ assert count == 1
+
+ group.fetch_async(0, count, fetch_cb, None)
+
+ filter = Gom.Filter.new_eq(ItemResource, "name", "item2")
+ group = repository.find_async(ItemResource, filter, find_cb, None)
+
+ loop.run()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]