def populate_db(db, num_participants=100, ntips=200, num_teams=5):
 print 'Making Participants'
 make_flag_tester = (num_participants > 1)
 participants = []
 for i in xrange(((num_participants - 1) if make_flag_tester else num_participants)):
  participants.append(fake_participant(db))
 if make_flag_tester:
  flag_tester = fake_participant(db, random_identities=False)
  participants.append(flag_tester)
  nepal = db.one("SELECT id FROM countries WHERE code='NP'")
  flag_tester.store_identity_info(nepal, 'nothing-enforced', {})
  flag_tester.set_identity_verification(nepal, True)
  vatican = db.one("SELECT id FROM countries WHERE code='VA'")
  flag_tester.store_identity_info(vatican, 'nothing-enforced', {})
  flag_tester.set_identity_verification(vatican, True)
 print 'Making Teams'
 teams = []
 teamowners = random.sample(participants, num_teams)
 for teamowner in teamowners:
  teams.append(fake_team(db, teamowner))
 teamowner = random.choice(participants)
 teams.append(fake_team(db, teamowner, 'Gratipay'))
 print 'Making Payment Instructions'
 npayment_instructions = 0
 payment_instructions = []
 for participant in participants:
  for team in teams:
   if (participant.username != team.owner):
    npayment_instructions += 1
    if (npayment_instructions > ntips):
     break
    payment_instructions.append(fake_payment_instruction(db, participant, team))
  if (npayment_instructions > ntips):
   break
 print 'Making Elsewheres'
 for p in participants:
  num_elsewheres = random.randint(1, 3)
  for platform_name in random.sample(PLATFORMS, num_elsewheres):
   fake_elsewhere(db, p, platform_name)
 min_date = min((x['mtime'] for x in payment_instructions))
 max_date = max((x['mtime'] for x in payment_instructions))
 payday_counter = 0
 date = min_date
 paydays_total = (((max_date - min_date).days / 7) + 1)
 while (date < max_date):
  payday_counter += 1
  end_date = (date + datetime.timedelta(days=7))
  week_payment_instructions = filter((lambda x: (x['mtime'] < date)), payment_instructions)
  params = dict(ts_start=date, ts_end=end_date)
  with db.get_cursor() as cursor:
   payday_id = cursor.one('\n                    INSERT INTO paydays\n                                (ts_start, ts_end)\n                         VALUES (%(ts_start)s, %(ts_end)s)\n                      RETURNING id\n                    ', params)
  sys.stdout.write(('\rMaking Paydays (%i/%i)' % (payday_id, paydays_total)))
  sys.stdout.flush()
  week_payments = []
  for payment_instruction in week_payment_instructions:
   participant = Participant.from_id(payment_instruction['participant_id'])
   team = Team.from_id(payment_instruction['team_id'])
   amount = payment_instruction['amount']
   assert (participant.username != team.owner)
   week_payments.append(fake_payment(db=db, participant=participant.username, team=team.slug, timestamp=date, amount=amount, payday=payday_id, direction='to-team'))
   if (amount != 0):
    fee = (amount * D('0.02'))
    fee = abs(fee.quantize(D('.01')))
    fake_exchange(db=db, participant=participant, amount=amount, fee=fee, timestamp=(date + datetime.timedelta(seconds=1)))
  for team in teams:
   week_payments_to_team = filter((lambda x: (x['team'] == team.slug)), week_payments)
   pay_out = sum((t['amount'] for t in week_payments_to_team))
   if pay_out:
    week_payments.append(fake_payment(db=db, participant=team.owner, team=team.slug, timestamp=date, amount=pay_out, payday=payday_id, direction='to-participant'))
  actives = set()
  actives.update((x['participant'] for x in week_payments))
  params = dict(nusers=len(actives), volume=sum((x['amount'] for x in week_payment_instructions)), payday_id=payday_id)
  db.run('\n                UPDATE paydays\n                   SET nusers=%(nusers)s, volume=%(volume)s\n                 WHERE id=%(payday_id)s\n                ', params)
  date = end_date
 print ''

def populate_db ( db , num_participants = 100 , ntips = 200 , num_teams = 5 ) : print 'Making Participants' make_flag_tester = ( num_participants > 1 ) participants = [ ] for i in xrange ( ( ( num_participants - 1 ) if make_flag_tester else num_participants ) ) : participants . append ( fake_participant ( db ) ) if make_flag_tester : flag_tester = fake_participant ( db , random_identities = False ) participants . append ( flag_tester ) nepal = db . one ( "SELECT id FROM countries WHERE code='NP'" ) flag_tester . store_identity_info ( nepal , 'nothing-enforced' , { } ) flag_tester . set_identity_verification ( nepal , True ) vatican = db . one ( "SELECT id FROM countries WHERE code='VA'" ) flag_tester . store_identity_info ( vatican , 'nothing-enforced' , { } ) flag_tester . set_identity_verification ( vatican , True ) print 'Making Teams' teams = [ ] teamowners = random . sample ( participants , num_teams ) for teamowner in teamowners : teams . append ( fake_team ( db , teamowner ) ) teamowner = random . choice ( participants ) teams . append ( fake_team ( db , teamowner , 'Gratipay' ) ) print 'Making Payment Instructions' npayment_instructions = 0 payment_instructions = [ ] for participant in participants : for team in teams : if ( participant . username != team . owner ) : npayment_instructions += 1 if ( npayment_instructions > ntips ) : break payment_instructions . append ( fake_payment_instruction ( db , participant , team ) ) if ( npayment_instructions > ntips ) : break print 'Making Elsewheres' for p in participants : num_elsewheres = random . randint ( 1 , 3 ) for platform_name in random . sample ( PLATFORMS , num_elsewheres ) : fake_elsewhere ( db , p , platform_name ) min_date = min ( ( x [ 'mtime' ] for x in payment_instructions ) ) max_date = max ( ( x [ 'mtime' ] for x in payment_instructions ) ) payday_counter = 0 date = min_date paydays_total = ( ( ( max_date - min_date ) . days / 7 ) + 1 ) while ( date < max_date ) : payday_counter += 1 end_date = ( date + datetime . timedelta ( days = 7 ) ) week_payment_instructions = filter ( ( lambda x : ( x [ 'mtime' ] < date ) ) , payment_instructions ) params = dict ( ts_start = date , ts_end = end_date ) with db . get_cursor ( ) as cursor : payday_id = cursor . one ( '\n                    INSERT INTO paydays\n                                (ts_start, ts_end)\n                         VALUES (%(ts_start)s, %(ts_end)s)\n                      RETURNING id\n                    ' , params ) sys . stdout . write ( ( '\rMaking Paydays (%i/%i)' % ( payday_id , paydays_total ) ) ) sys . stdout . flush ( ) week_payments = [ ] for payment_instruction in week_payment_instructions : participant = Participant . from_id ( payment_instruction [ 'participant_id' ] ) team = Team . from_id ( payment_instruction [ 'team_id' ] ) amount = payment_instruction [ 'amount' ] assert ( participant . username != team . owner ) week_payments . append ( fake_payment ( db = db , participant = participant . username , team = team . slug , timestamp = date , amount = amount , payday = payday_id , direction = 'to-team' ) ) if ( amount != 0 ) : fee = ( amount * D ( '0.02' ) ) fee = abs ( fee . quantize ( D ( '.01' ) ) ) fake_exchange ( db = db , participant = participant , amount = amount , fee = fee , timestamp = ( date + datetime . timedelta ( seconds = 1 ) ) ) for team in teams : week_payments_to_team = filter ( ( lambda x : ( x [ 'team' ] == team . slug ) ) , week_payments ) pay_out = sum ( ( t [ 'amount' ] for t in week_payments_to_team ) ) if pay_out : week_payments . append ( fake_payment ( db = db , participant = team . owner , team = team . slug , timestamp = date , amount = pay_out , payday = payday_id , direction = 'to-participant' ) ) actives = set ( ) actives . update ( ( x [ 'participant' ] for x in week_payments ) ) params = dict ( nusers = len ( actives ) , volume = sum ( ( x [ 'amount' ] for x in week_payment_instructions ) ) , payday_id = payday_id ) db . run ( '\n                UPDATE paydays\n                   SET nusers=%(nusers)s, volume=%(volume)s\n                 WHERE id=%(payday_id)s\n                ' , params ) date = end_date print ''
populate database with total_users simulated users and their actions .