Skip to content

Commit

Permalink
Change timestamp requirement to be a score value
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Sep 7, 2011
1 parent 2e8c9fa commit ba069b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -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))
Expand All @@ -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'})
Expand Down
9 changes: 6 additions & 3 deletions timak/timelines.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba069b6

Please sign in to comment.