feeds Package

base Module

class stream_framework.feeds.base.BaseFeed(user_id)[source]

Bases: object

The feed class allows you to add and remove activities from a feed. Please find below a quick usage example.

Usage Example:

feed = BaseFeed(user_id)
# start by adding some existing activities to a feed
feed.add_many([activities])
# querying results
results = feed[:10]
# removing activities
feed.remove_many([activities])
# counting the number of items in the feed
count = feed.count()
feed.delete()

The feed is easy to subclass. Commonly you’ll want to change the max_length and the key_format.

Subclassing:

class MyFeed(BaseFeed):
    key_format = 'user_feed:%(user_id)s'
    max_length = 1000

Filtering and Pagination:

feed.filter(activity_id__gte=1)[:10]
feed.filter(activity_id__lte=1)[:10]
feed.filter(activity_id__gt=1)[:10]
feed.filter(activity_id__lt=1)[:10]

Activity storage and Timeline storage

To keep reduce timelines memory utilization the BaseFeed supports normalization of activity data.

The full activity data is stored only in the activity_storage while the timeline only keeps a activity references (refered as activity_id in the code)

For this reason when an activity is created it must be stored in the activity_storage before other timelines can refer to it

eg.

feed = BaseFeed(user_id)
feed.insert_activity(activity)
follower_feed = BaseFeed(follower_user_id)
feed.add(activity)

It is also possible to store the full data in the timeline storage

The strategy used by the BaseFeed depends on the serializer utilized by the timeline_storage

When activities are stored as dehydrated (just references) the BaseFeed will query the activity_storage to return full activities

eg.

feed = BaseFeed(user_id)
feed[:10]

gets the first 10 activities from the timeline_storage, if the results are not complete activities then the BaseFeed will hydrate them via the activity_storage

activity_class

alias of Activity

activity_serializer

alias of BaseSerializer

activity_storage_class

alias of BaseActivityStorage

add(activity, *args, **kwargs)[source]
add_many(activities, batch_interface=None, trim=True, *args, **kwargs)[source]

Add many activities

Parameters:
  • activities – a list of activities
  • batch_interface – the batch interface
count()[source]

Count the number of items in the feed

delete()[source]

Delete the entire feed

filter(**kwargs)[source]

Filter based on the kwargs given, uses django orm like syntax

Example ::
# filter between 100 and 200 feed = feed.filter(activity_id__gte=100) feed = feed.filter(activity_id__lte=200) # the same statement but in one step feed = feed.filter(activity_id__gte=100, activity_id__lte=200)
filtering_supported = False
classmethod flush()[source]
get_activity_slice(start=None, stop=None, rehydrate=True)[source]

Gets activity_ids from timeline_storage and then loads the actual data querying the activity_storage

classmethod get_activity_storage()[source]

Returns an instance of the activity storage

classmethod get_timeline_batch_interface()[source]
classmethod get_timeline_storage()[source]

Returns an instance of the timeline storage

classmethod get_timeline_storage_options()[source]

Returns the options for the timeline storage

hydrate_activities(activities)[source]

hydrates the activities using the activity_storage

index_of(activity_id)[source]

Returns the index of the activity id

Parameters:activity_id – the activity id
classmethod insert_activities(activities, **kwargs)[source]

Inserts an activity to the activity storage

Parameters:activity – the activity class
classmethod insert_activity(activity, **kwargs)[source]

Inserts an activity to the activity storage

Parameters:activity – the activity class
key_format = 'feed_%(user_id)s'
max_length = 100
needs_hydration(activities)[source]

checks if the activities are dehydrated

on_update_feed(new, deleted)[source]

A hook called when activities area created or removed from the feed

order_by(*ordering_args)[source]

Change default ordering

ordering_supported = False
remove(activity_id, *args, **kwargs)[source]
classmethod remove_activity(activity, **kwargs)[source]

Removes an activity from the activity storage

Parameters:activity – the activity class or an activity id
remove_many(activity_ids, batch_interface=None, trim=True, *args, **kwargs)[source]

Remove many activities

Parameters:activity_ids – a list of activities or activity ids
timeline_serializer

alias of SimpleTimelineSerializer

timeline_storage_class

alias of BaseTimelineStorage

trim(length=None)[source]

Trims the feed to the length specified

Parameters:length – the length to which to trim the feed, defaults to self.max_length
trim_chance = 0.01
class stream_framework.feeds.base.UserBaseFeed(user_id)[source]

Bases: stream_framework.feeds.base.BaseFeed

Implementation of the base feed with a different Key format and a really large max_length

key_format = 'user_feed:%(user_id)s'
max_length = 1000000

cassandra Module

memory Module

class stream_framework.feeds.memory.Feed(user_id)[source]

Bases: stream_framework.feeds.base.BaseFeed

activity_storage_class

alias of InMemoryActivityStorage

timeline_storage_class

alias of InMemoryTimelineStorage

redis Module

class stream_framework.feeds.redis.RedisFeed(user_id)[source]

Bases: stream_framework.feeds.base.BaseFeed

activity_serializer

alias of ActivitySerializer

activity_storage_class

alias of RedisActivityStorage

filtering_supported = True
classmethod get_timeline_storage_options()[source]

Returns the options for the timeline storage

ordering_supported = True
redis_server = 'default'
timeline_storage_class

alias of RedisTimelineStorage