# Generate random test data to database import random import datetime def generate_groups(sections = 12, pens = 10): groups = [] for section in range(1, sections + 1): for pen in range(1, pens + 1): groupid = 100 * section + pen groups.append((groupid, section, pen)) return groups def generate_animals(groups, pergroup_min = 5, pergroup_max = 13, deadprob = 0.05): animals = [] for groupid, section, pen in groups: count = random.randint(pergroup_min, pergroup_max) baseanimalid = 100 * groupid for i in range(count): alive = random.random() >= deadprob animals.append((baseanimalid + i, groupid, alive, "")) return animals def generate_feeds(animals, feedavg = 0.5, feeddev = 0.3, intervalavg = 5., intervaldev = 1., days = 90): feeds = [] endday = datetime.datetime.now() startday = endday - datetime.timedelta(days = days) feedid = 0 for animalid, groupid, alive, notes in animals: timestamp = startday while timestamp < endday: interval = random.gauss(intervalavg, intervaldev) timestamp += datetime.timedelta(seconds = int(interval * 3600)) amount = random.gauss(feedavg, feeddev) feeds.append((feedid, timestamp, groupid, animalid, amount)) feedid += 1 return feeds if __name__ == "__main__": answer = raw_input("Are you sure you want to insert random data to database? ") if answer.lower() not in ["y", "yes"]: exit() from database import db_conn random.seed() print "Generating data:" print "- groups" groups = generate_groups(sections = 3, pens = 3) print "- animals" animals = generate_animals(groups, pergroup_min = 3, pergroup_max = 8) print "- feeds" feeds = generate_feeds(animals, intervalavg = 10.) print "Updating database.." cursor = db_conn.cursor() cursor.executemany("INSERT INTO groups(groupid, section, pen) VALUES (?,?,?)", groups) cursor.executemany("INSERT INTO animals(animalid, groupid, alive, notes) VALUES (?,?,?,?)", animals) cursor.executemany("INSERT INTO feeds(feedid, timestamp, groupid, animalid, amount) VALUES (?,?,?,?,?)", feeds) db_conn.commit() print "Done!"