1

我正在尝试设置基本提要:

from stream_framework.feeds.redis import RedisFeed

class PinFeed(RedisFeed):
    key_format = 'feed:normal:%(user_id)s'

class UserPinFeed(PinFeed):
    key_format = 'feed:user:%(user_id)s'


feed = UserPinFeed(13)

但是当我feed[:2]在添加一个活动后运行时,我不断收到这个错误:

<ipython-input-25-41b7cd0d0d8c> in <module>()
----> 1 feed[:2]

/Users/home/venv/lib/python2.7/site-packages/stream_framework/feeds/base.pyc in __getitem__(self, k)
    303         try:
    304             results = self.get_activity_slice(
--> 305                 start, bound)
    306         except StopIteration:
    307             # There's nothing left, even though the bound is higher.

/Users/home/venv/lib/python2.7/site-packages/stream_framework/feeds/base.pyc in get_activity_slice(self, start, stop, rehydrate)
    354         print 'line 350'
    355         if self.needs_hydration(activities) and rehydrate:
--> 356             activities = self.hydrate_activities(activities)
    357         return activities
    358

/Users/home/venv/lib/python2.7/site-packages/stream_framework/feeds/base.pyc in hydrate_activities(self, activities)
    332         activity_data = {a.serialization_id: a for a in activity_list}
    333         print activity_data
--> 334         return [activity.get_hydrated(activity_data) for activity in activities]
    335
    336     def needs_hydration(self, activities):

/Users/home/venv/lib/python2.7/site-packages/stream_framework/activity.pyc in get_hydrated(self, activities)
     42
     43         '''
---> 44         activity = activities[int(self.serialization_id)]
     45         activity.dehydrated = False
     46         return activity

KeyError: 14929294667170000000001005L

我可以打印出来[<stream_framework.activity.DehydratedActivity object at 0x10e114e90>]并且[u'14929294667170000000001005'].

任何线索可能导致这种情况?

4

1 回答 1

2

这里的类型不匹配,在KeyError堆栈跟踪中显示的 Key 和打印的表示之间[u'14929294667170000000001005']

u'14929294667170000000001005'是一个 unicode 字符串,14929294667170000000001005L而是一个long整数类型。

键本身或您尝试用作查找键的值都必须更改,因此类型相同。请注意,带或不带 L 后缀(表示long类型)的 int 将用于从 python 检索值dict。一个简单的演示:

>>> D = {14929294667170000000001005 : 'some value'}
>>> D[14929294667170000000001005]
'some value'
>>> D[14929294667170000000001005L]
'some value'
于 2017-04-23T06:57:17.397 回答