Skip to content

Instantly share code, notes, and snippets.

@alexoliveira
Created June 29, 2012 23:30
Show Gist options
  • Save alexoliveira/3021385 to your computer and use it in GitHub Desktop.
Save alexoliveira/3021385 to your computer and use it in GitHub Desktop.
from trees.models import Tree, Post, Member
from datetime import datetime, timedelta
def percentRetained():
'''Iterate over periods of 2 weeks, filter out all users who didn't add a post during that 2-weeks-period.
Start with particular group of two weeks, see who used it for that 2-week period.
For every period of two weeks, filter out people who are not'''
today = datetime.today()
experiment_duration = 7 * 4 * 3
retained_members = None
for d in range(0, experiment_duration, 7 * 2):
date_range = (today, today + timedelta(d))
members_this_period = [p.tree.owner for p in Post.objects.filter(
created__range=date_range).all()]
if retained_members == None:
members_on_starting_period = members_this_period
retained_members = members_this_period
pass
new_retained_members = []
for member in retained_members:
if member in members_this_period:
new_retained_members.append(member)
retained_members = new_retained_members
print 'There were %s members on the starting period' % (len(members_on_starting_period))
print 'There were %s retained members after %s days' % (len(retained_members), experiment_duration)
return float(len(retained_members)) / float(len(members_on_starting_period))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment