[hamster-applet] using tuples instead of dicts for fact lists. not as readable but twice as fast
- From: Toms Baugis <tbaugis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hamster-applet] using tuples instead of dicts for fact lists. not as readable but twice as fast
- Date: Tue, 13 Apr 2010 23:06:42 +0000 (UTC)
commit 4bc404bf01f0f6294814d14a61192f15af354045
Author: Toms Bauģis <toms baugis gmail com>
Date: Wed Apr 14 00:01:11 2010 +0100
using tuples instead of dicts for fact lists. not as readable but twice as fast
src/hamster/client.py | 28 +++++++---------
src/hamster/storage.py | 83 +++++++++++++++++++----------------------------
2 files changed, 47 insertions(+), 64 deletions(-)
---
diff --git a/src/hamster/client.py b/src/hamster/client.py
index 23eac78..365efd6 100644
--- a/src/hamster/client.py
+++ b/src/hamster/client.py
@@ -42,22 +42,20 @@ def debus(value):
return value
def from_dbus_fact(fact):
- fact['start_time'] = dt.datetime.utcfromtimestamp(fact['start_time'])
- if fact['end_time']:
- fact['end_time'] = dt.datetime.utcfromtimestamp(fact['end_time'])
- else:
- fact['end_time'] = None
+ """unpack the struct into a proper dict"""
+ return dict(id = fact[0],
+ start_time = dt.datetime.utcfromtimestamp(fact[1]),
+ end_time = dt.datetime.utcfromtimestamp(fact[2]) if fact[2] else None,
+ description = fact[3],
+ name = fact[4],
+ activity_id = fact[5],
+ category = fact[6],
+ tags = fact[7],
+ date = dt.datetime.utcfromtimestamp(fact[8]),
+ delta = dt.timedelta(days = fact[9] // 24 * 60 * 60,
+ seconds = fact[9] % 24 * 60 * 60)
+ )
- fact['tags'] = fact['tags'] or [] # effectively converting 0 to []
-
- if 'date' in fact:
- fact['date'] = dt.datetime.utcfromtimestamp(fact['date']).date()
-
- if 'delta' in fact:
- days, seconds = divmod(fact['delta'], 24 * 60 * 60)
- fact['delta'] = dt.timedelta(days = days, seconds = seconds)
-
- return fact
class Storage(object):
def __init__(self, parent = None):
diff --git a/src/hamster/storage.py b/src/hamster/storage.py
index 5a26b4b..43ef547 100644
--- a/src/hamster/storage.py
+++ b/src/hamster/storage.py
@@ -26,23 +26,17 @@ def to_dbus_fact(fact):
"""Perform the conversion between fact database query and
dbus supported data types
"""
- if not fact:
- return dbus.Dictionary({}, signature='sv')
- fact = dict(fact)
- for key in fact.keys():
- fact[key] = fact[key] or 0
-
- # make sure we return correct type where strings are expected
- if not fact[key] and key in ('name', 'category', 'description'):
- fact[key] = ''
-
- # convert times to gmtime
- if isinstance(fact[key], dt.datetime) or isinstance(fact[key], dt.date):
- fact[key] = timegm(fact[key].timetuple())
- elif isinstance(fact[key], dt.timedelta) :
- fact[key] = fact[key].days * 24 * 60 * 60 + fact[key].seconds
- return fact
+ return (fact['id'],
+ timegm(fact['start_time'].timetuple()),
+ timegm(fact['end_time'].timetuple()) if fact['end_time'] else 0,
+ fact['description'] or '',
+ fact['name'] or '',
+ fact['activity_id'] or 0,
+ fact['category'] or '',
+ dbus.Array(fact['tags'], signature = 's'),
+ timegm(fact['date'].timetuple()),
+ fact['delta'].days * 24 * 60 * 60 + fact['delta'].seconds)
class Storage(dbus.service.Object):
@@ -113,7 +107,7 @@ class Storage(dbus.service.Object):
return result
- @dbus.service.method("org.gnome.Hamster", in_signature='i', out_signature='a{sv}')
+ @dbus.service.method("org.gnome.Hamster", in_signature='i', out_signature='(iiissisasii)')
def GetFact(self, fact_id):
"""Gets the current displaying fact
Parameters:
@@ -127,7 +121,10 @@ class Storage(dbus.service.Object):
u end_time: Seconds since epoch (timestamp)
as tags: List of tags used
"""
- return to_dbus_fact(self.__get_fact(fact_id))
+ fact = dict(self.__get_fact(fact_id))
+ fact['date'] = fact['start_time'].date()
+ fact['delta'] = dt.timedelta()
+ return to_dbus_fact(fact)
@dbus.service.method("org.gnome.Hamster", in_signature='issiiss', out_signature='i')
@@ -174,21 +171,24 @@ class Storage(dbus.service.Object):
self.end_transaction()
- @dbus.service.method("org.gnome.Hamster", in_signature='uus', out_signature='aa{sv}')
+ @dbus.service.method("org.gnome.Hamster", in_signature='uus', out_signature='a(iiissisasii)')
def GetFacts(self, start_date, end_date, search_terms):
"""Gets facts between the day of start_date and the day of end_date.
Parameters:
- u start_date: Seconds since epoch (timestamp). Use 0 for today
- u end_date: Seconds since epoch (timestamp). Use 0 for today
+ i start_date: Seconds since epoch (timestamp). Use 0 for today
+ i end_date: Seconds since epoch (timestamp). Use 0 for today
s search_terms: Bleh
- Returns Array of fact where fact it's Dict of:
- i id: Unique fact identifier
- s name: Activity name
- s category: Category name
- s description: Description of the fact
- u start_time: Seconds since epoch (timestamp)
- u end_time: Seconds since epoch (timestamp)
- as tags: List of tags used
+ Returns Array of fact where fact is struct of:
+ i id
+ i start_time
+ i end_time
+ s description
+ s activity name
+ i activity id
+ i category name
+ as List of fact tags
+ i date
+ i delta
"""
#TODO: Assert start > end ?
if start_date:
@@ -201,29 +201,14 @@ class Storage(dbus.service.Object):
else:
end = dt.date.today()
- facts = self.__get_facts(start, end, search_terms)
- return [to_dbus_fact(fact) for fact in facts]
+ return [to_dbus_fact(fact) for fact in self.__get_facts(start, end, search_terms)]
- @dbus.service.method("org.gnome.Hamster", out_signature='aa{sv}')
+ @dbus.service.method("org.gnome.Hamster", out_signature='a(iiissisasii)')
def GetTodaysFacts(self):
- """Gets facts of today, respecting hamster midnight.
- Returns Array of fact where fact it's Dict of:
- i id: Unique fact identifier
- s name: Activity name
- s category: Category name
- s description: Description of the fact
- u start_time: Seconds since epoch (timestamp)
- u end_time: Seconds since epoch (timestamp)
- as tags: List of tags used
- """
-
- facts = dbus.Array([], signature='a{sv}')
-
- for fact in self.__get_todays_facts():
- facts.append(to_dbus_fact(fact))
-
- return facts
+ """Gets facts of today, respecting hamster midnight. See GetFacts for
+ return info"""
+ return [to_dbus_fact(fact) for fact in self.__get_todays_facts()]
# categories
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]