def main(options, logger=None): if logger == None: logger = Logger("download.log", "download.py", True, True) else: logger.context = "download.py" try: query = utils.getQuery() logger.context = "download.py" if __name__ == "__main__": utils.log_header(logger, DESCRIPTION, options.get("verbose", False)) logger.debug("Options:", options.get("verbose", False)) for i in options: logger.debug(i + ": " + str(options[i]), options.get("verbose", False)) download_query(query, options, logger) # move all downloaded videos in videos directory repoDir = utils.repoPath() fs = os.listdir(repoDir) for f in fs: if utils.is_video(f): os.rename(f, "videos/" + f) logger.success("All files downloaded.", not options.get("silent", False)) except Exception as e: logger.handle_exception(e) raise e
def main(module, config, logger=None, verbose=False): if logger == None: logger = Logger("runner_main.log", "runner.py > main", autosave=True) else: logger.context = "runner.py > main()" classes = utils.get_classes(module) errors = utils.parse_config(config, classes) for i in errors: if type(i) == ConfigError: logger.error("ConfigError: " + str(i), True) elif type(i) == PropertyError: logger.warning("PropertyError: " + str(i), True) w = World(initial_food=config.get("World").get("initial_food", 100), food_function=config.get("World").get("food_function", functions.base)) for ccllss in classes: w.creatures[ccllss.__name__] = [] n_of_class = config.get("World", {}).get("initial", {}).get("n_" + ccllss.__name__, 1) for i in range(n_of_class): c = ccllss(name=ccllss.__name__, species=ccllss.__name__, reproduction_chance=config.get("Creatures", {}).get( ccllss.__name__, {}).get("reproduction_chance", 0.1), death_chance=config.get("Creatures", {}).get( ccllss.__name__, {}).get("death_chance", 0.1), speed=config.get("Creatures", {}).get(ccllss.__name__, {}).get("speed", 1), death_age=config.get("Creatures", {}).get(ccllss.__name__, {}).get("death_age", 100), age_increments=config.get("Creatures", {}).get( ccllss.__name__, {}).get("age_increments", 0.1), energy=config.get("Creatures", {}).get(ccllss.__name__, {}).get("energy", 100)) w.creatures[ccllss.__name__].append(c) logger.debug( "Added " + str(n_of_class) + " instances of " + ccllss.__name__ + " to the world", verbose) data = do_iterations(w, config.get("iterations"), logger, config) logger.save() display_data(data)
def exec_sql(sql_command="SELECT * FROM tasks", verbose=True, logger: Logger = None): """Execute sql_command on database and return None or whatever is returned from the database. If sql_command is not specified, all tasks will get returned""" result = None if logger != None and type(logger) == Logger: logger.context = "exec_sql" else: logger = Logger("exec_sql.log", "exec_sql", True, True) try: connection = pymysql.connect(host='localhost', user='******', password='******', db='Helix', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) except OperationalError as e: logger.handle_exception(e) raise AuthenticationError("Access denied to database") if verbose and logger != None: logger.success("connected to database", False) if verbose and logger != None: logger.debug("executing SQL-Command: " + sql_command) with connection.cursor() as cursor: cursor.execute(sql_command) if "SELECT" in sql_command: result = cursor.fetchall() else: connection.commit() if verbose and logger != None: logger.debug("SQL query returned: " + str(result)) try: connection.close() if verbose and logger != None: logger.success("Shut down connection to database", False) except Exception as e: if logger != None and type(logger) == Logger: logger.handle_exception(e) else: print(e) finally: return result
def do_iterations(world, iterations, logger=None, config={}): if logger == None: logger = Logger("runner_do_iterations", "iterations", autosave=True) else: logger.context = "iterations" raw_data = {} for species in world.creatures: raw_data[species] = [] raw_data["food"] = [] raw_data["population"] = [] csv_lines = [] try: species_count = { "iteration": 0, "food_count": world.food_count, "food_count_before": world.food_count_before, "population": 0 } mystr = str("iteration, food count, food count before, population") for species in world.creatures: species_count[species] = len(species) mystr += ", " + species species_vs = fileloghelper.VarSet(species_count) print(mystr) for iteration in range(iterations): world.act(iteration, config) # array logging for matplotlib population = 0 for species in world.creatures: n_of_animals_in_species = len(world.creatures[species]) raw_data[species].append(n_of_animals_in_species) population += n_of_animals_in_species raw_data["food"].append(world.food_count_before) raw_data["population"].append(population) species_vs.set("iteration", iteration) for species in world.creatures: species_vs.set(species, len(species)) species_vs.set("food_count", world.food_count) species_vs.set("food_count_before", world.food_count_before) species_vs.set("population", population) species_vs.print_variables() # reduced (string) logging to csv file (i.e. for later analysis) #csv_lines.append(string + "\n") except KeyboardInterrupt: pass except KeyError: pass except Exception as e: raise e finally: logger.save() f = open("raw_data.csv", "w") header = "timestamp, " for i in raw_data: header += i + ", " header = header[:-2] f.write(header + "\n") f.writelines(csv_lines) f.close() return raw_data
arguments = parser.parse_args() args = vars(arguments) VERBOSE = args.get("verbose", False) log = Logger("workflow.log", "workflow.py", True, True) utils.log_header(log, DESCRIPTION, VERBOSE) try: log.debug("Debug mode activated.", VERBOSE) log.debug("Args: ", VERBOSE) for i in args: log.debug(i + ": " + str(args[i]), VERBOSE) if not args.get("test", False): download.main(args, logger=log) if args.get("facerec", None) != None and not args.get("test", False): if "all" in args.get("facerec", []): # pass all videos from utils.getVideos() to facerec facerec.main({"files": utils.getVideos()}) else: # pass all videos from args to facerec facerec.main({"files": args.get("facerec", [])}) if not args.get("test", False): convert.main(args, log) if not args.get("test", False): transfer.main(args, log) log.success("Workflow routine finished!", not args.get("silent", False)) except KeyboardInterrupt: log.context = "workflow.py" log.warning("exiting...", True) exit(0)