Skip to content
Browse files

Change timestamp requirement to be a score value

  • Loading branch information...
1 parent 2e8c9fa commit ba069b630cfaec509000627447b527acb04fb085 @dcramer dcramer committed
Showing with 9 additions and 3 deletions.
  1. +3 −0 README.rst
  2. +6 −3 timak/timelines.py
View
3 README.rst
@@ -19,6 +19,7 @@ Timelines are unique sets of objects (unique by the ID you provide) ordered by a
>>> tl = Timeline(connection=conn, max_items=3)
+ >>> # t1.add("key", "unique_id", "score")
>>> tl.add("brett:tweets", 1, datetime(2011, 1, 1))
[1]
>>> tl.add("brett:tweets", 2, datetime(2011, 1, 2))
@@ -30,6 +31,8 @@ Timelines are unique sets of objects (unique by the ID you provide) ordered by a
>>> tl.delete("brett:tweets", 2, datetime(2011, 1, 2))
[4, 3]
+If you provide a ``datetime.datetime`` value to score Timak will automatically convert to a sortable score value.
+
As you can see the default order is descending by the date you provide, and the object IDs are returned by default. You can also provide an ``obj_data`` argument (must be JSON serializable) which will be returned instead.
>>> tl.add("brett:tweets", 5, datetime(2011, 1, 5), obj_data={'body': 'Hello world, this is my first tweet'})
View
9 timak/timelines.py
@@ -42,7 +42,7 @@ def _dict_to_list(self, d):
if not d: return []
l = d.values()
reverse = self.order == 'desc'
- l.sort(key=lambda x: x['timestamp'], reverse=reverse)
+ l.sort(key=lambda x: x['score'], reverse=reverse)
return l
def _list_to_data(self, l):
@@ -98,12 +98,15 @@ def get(self, key, raw=False):
def _make_op(action):
assert action in ('add', 'delete')
- def _op(self, key, uniq_ident, obj_datetime, obj_data=None, raw=False):
+ def _op(self, key, uniq_ident, obj_score, obj_data=None, raw=False):
now = self._datetime_to_js(datetime.datetime.utcnow())
obj, data = self._get_obj_and_data(key, write_merged=False)
+ if isinstance(obj_score, datetime.datetime):
+ obj_score = self._datetime_to_js(obj_score)
+
new_item = {'id': uniq_ident,
- 'timestamp': self._datetime_to_js(obj_datetime),
+ 'score': obj_score,
'modified': now}
if obj_data:
new_item['data'] = obj_data

0 comments on commit ba069b6

Please sign in to comment.
Something went wrong with that request. Please try again.