def safely_reserve_a_username(cursor, gen_usernames=gen_random_usernames, reserve=insert_into_participants):
 cursor.execute('SAVEPOINT safely_reserve_a_username')
 seatbelt = 0
 for username in gen_usernames():
  seatbelt += 1
  if (seatbelt > 100):
   raise FailedToReserveUsername
  try:
   check = reserve(cursor, username)
  except IntegrityError:
   cursor.execute('ROLLBACK TO safely_reserve_a_username')
   continue
  else:
   assert (check == username)
   break
 else:
  raise RanOutOfUsernameAttempts
 cursor.execute('RELEASE safely_reserve_a_username')
 return username

def safely_reserve_a_username ( cursor , gen_usernames = gen_random_usernames , reserve = insert_into_participants ) : cursor . execute ( 'SAVEPOINT safely_reserve_a_username' ) seatbelt = 0 for username in gen_usernames ( ) : seatbelt += 1 if ( seatbelt > 100 ) : raise FailedToReserveUsername try : check = reserve ( cursor , username ) except IntegrityError : cursor . execute ( 'ROLLBACK TO safely_reserve_a_username' ) continue else : assert ( check == username ) break else : raise RanOutOfUsernameAttempts cursor . execute ( 'RELEASE safely_reserve_a_username' ) return username
safely reserve a username .