[Answered ]-Django lambda and django-activity-stream

1👍

Somewhere above where these lambdas are defined, you need to import the names get_people_i_follow and get_my_followers. I’m not familiar with django-activity-stream, but it’s probably something like from activity_stream import get_people_i_follow, get_my_follwers.

Lambda is a keyword for creating anonymous functions on the fly, so the meaning of your code is basically the same as if you had written the following.

def ACTIVITY_GET_PEOPLE_I_FOLLOW(user):
    return get_people_i_follow(user)

def ACTIVITY_GET_MY_FOLLOWERS(user):
    return get_my_followers(user)
👤jcdyer

1👍

You need to make sure that the functions get_people_i_follow and get_my_followers are imported into your settings files.

e.g.:


from activity_stream.models import get_people_i_follow, get_my_followers

Lambda is just a shorthand for defining a function so:


ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user)

Is equivalent to:


def activity_get_people_i_follow(user):
    return get_people_i_follow(user)
ACTIVITY_GET_PEOPLE_I_FOLLOW = activity_get_people_i_follow

Which upon reflection means you don’t gain a lot in this case. However if you needed to avoid importing those function too early in your settings file (i.e. due to circular import) then you could do:


def activity_get_people_i_follow(user):
    from activity_stream.models import get_people_i_follow
    return get_people_i_follow(user)
ACTIVITY_GET_PEOPLE_I_FOLLOW = activity_get_people_i_follow

and just import the activity stream function as you need it.

UPDATE: looks like defining these settings is a red-herring:

https://github.com/philippWassibauer/django-activity-stream/blob/master/activity_stream/models.py#L133

As you can see these settings are only needed if you are not using the default activity streams. So simply remove them from your settings file.

The seg-fault is probably due to an infinite recursion occurring, as get_people_i_follow calls whatever function is defined by ACTIVITY_GET_PEOPLE_I_FOLLOW, which in this case calls get_people_i_follow again…

0👍

If you are integrating with a pre-existing network I don’t believe you’re actually supposed to write verbatim:

ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user)
ACTIVITY_GET_MY_FOLLOWERS = lambda user: get_my_followers(user)

I believe the author was just showing an example that ACTIVITY_GET_PEOPLE_I_FOLLOW and
ACTIVITY_GET_MY_FOLLOWERS need to be set to a lambda or function that accepts one user argument and returns a list of users. You should probably be looking for something like friends_for_user in django-friends, or writing your own functions to implement this functionality.

get_people_i_follow is indeed defined in activity_stream.models but it is just importing what’s defined in settings.py. So if settings.py has ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user) you’re going to get a wild and crazy circular import / infinite recursion.

👤dgel

Leave a comment