def recursive_combat(player_1, player_2, level=1, bar: ChargingBar = None): print_wrapper(f"=== Game {level} ===") visited_games = set() round = 0 while player_1 and player_2: round += 1 if (player_1, player_2) in visited_games: return 1, player_1 visited_games.add((player_1, player_2)) print_wrapper(f"Player 1's deck: {player_1}") print_wrapper(f"Player 2's deck: {player_2}") card_1, player_1 = player_1[0], player_1[1:] card_2, player_2 = player_2[0], player_2[1:] print_wrapper(f"Player 1 plays: {card_1}") print_wrapper(f"Player 2 plays: {card_2}") # breakpoint() if len(player_1) >= card_1 and len(player_2) >= card_2: print_wrapper("Playing a sub-game to determine the winner...") winner, _ = recursive_combat( copy_deck(player_1, card_1), copy_deck(player_2, card_2), level=level + 1, ) print_wrapper("...anyway, back to game 1.") if winner == 1: print_wrapper(f"Player 1 wins round {round} of game {level}!") player_1 = (*player_1, card_1, card_2) else: print_wrapper(f"Player 2 wins round {round} of game {level}!") player_2 = (*player_2, card_2, card_1) else: if card_1 > card_2: print_wrapper(f"Player 1 wins round {round} of game {level}!") player_1 = (*player_1, card_1, card_2) else: print_wrapper(f"Player 2 wins round {round} of game {level}!") player_2 = (*player_2, card_2, card_1) if bar and max(len(player_1), len(player_2)) > bar.index: bar.goto(max(len(player_1), len(player_2))) winner = max(player_1, player_2) if winner == player_1: print_wrapper(f"The winner of game {level} is player 1!") else: print_wrapper(f"The winner of game {level} is player 2!") return (1 if winner == player_1 else 2), winner
def generate_gif(input_file): if not os.path.exists(folder): os.makedirs(folder) video_duration = (int( float( subprocess.check_output([ "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", input_file ], universal_newlines=True).strip()))) # creating vars for easy reading current_time = args.begin random_start = args.randstart random_end = args.randend # workaround for videos in source root folder if len(input_file.split("/")) == 1: print("\nCurrent file: " + input_file) else: print("\nCurrent file: " + input_file.split("/")[1]) bar = ChargingBar('Processing', max=video_duration) # initialize bar bar.goto(0) while current_time <= video_duration: current_time = current_time + (random.randrange( random_start, random_end)) if current_time > video_duration: # set progress bar to 100% before breaking bar.goto(video_duration) break # creating vars for easy reading filename_generator = args.destination + '%s-%s.gif' gif_length = args.length # you can increase the fps=12 and scale=w=480 values with a higher number for smoother/bigger gifs, # increases the file size. subprocess.check_output([ 'ffmpeg', '-y', '-ss', str(current_time), '-t', gif_length, '-i', input_file, '-filter_complex', '[0:v] fps=12,scale=w=480:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[' 'b][p] paletteuse=new=1', filename_generator % (input_file, current_time) ], stderr=subprocess.STDOUT, universal_newlines=True).strip() bar.goto(current_time) bar.finish()
def run(self): ''' loader run :return: ''' try: logger.bind_process(self.pid) self.key = f"{self.pid}$%loader$%" logger.bind_busclient(self.bus_client) logger.set_output(self.args.local_logger, self.args.nout, self.args.debug) logger.info(f"{self.key} start loader", __name__) self.case_queue = self.bus_client.get_case() self.case_back_queue = self.bus_client.get_case_back() self.case_count = self.bus_client.get_case_count() self.web_queue = self.bus_client.get_publish_loader() self.params_queue = self.bus_client.get_param() # check the signal of start while True: signal_params_ = self.get_parameter() if isinstance(signal_params_, Signal) and signal_params_.signal == SIGNAL_START: logger.info(f"{self.key} -- get start signal from main", __name__) break complete_case_count = 0 show_count = [] all_count = [] # check the params while True: temp = self.get_parameter() # check the signal of stop, after stop nothing! if isinstance(temp, Signal) and temp.signal == SIGNAL_STOP: if self.args.nout: cb = ChargingBar(max=self.true_case_count) while True: if not self.case_count.empty(): complete_case_count = self.case_count.get() if complete_case_count not in all_count: all_count.append(complete_case_count) else: pass if self.args.nout: cb.goto(complete_case_count) if self.case_back_queue.empty(): pass else: logger.debug("put case here from back queue", __name__) self.put_case("case", None, self.case_back_queue.get()) if self.case_queue.empty() and self.case_back_queue.empty(): with new_locker(self.bus_client, self.key, self.lock): if complete_case_count not in show_count: logger.info( f"complete case count check here {complete_case_count} == {self.true_case_count}", __name__) show_count.append(complete_case_count) if complete_case_count == self.true_case_count: if self.args.nout: cb.finish() logger.debug( f"{self.key} all cases run over [{complete_case_count}/{self.true_case_count}]", __name__) self.end_handler() return # time.sleep(0.001) elif temp is None: # time.sleep(0.001) continue else: file_name = temp.get("file_name") logger.debug(f"file_name = {file_name}", __name__) inputs = plugin_manager.load_from_file(file_name) logger.debug("inputs", __name__) logger.debug(f"{self.key} -- {inputs}", __name__) input = inputs.get("config")[0] bench_name = input.get("name") module_name = input.get("module_name") module_path = input.get("module_path") logger.debug("bench", __name__) bench = HttpApiBench(self.args) if "dbconfig" in inputs.keys(): for input in inputs.get("dbconfig"): db = SQLConfig() db.constructor(input) bench.add_db(db) for input in inputs.get("testcases"): logger.debug(input, __name__) if input.get("id") is None or input.get("subid") is None: continue if input.get("host_port") is None: input["host_port"] = inputs.get("config")[0].get("host_port") if "api_name" in input.keys() or input.get("type") == "api": case = HttpApiCase() elif input.get("type") == "app": case = AppCase() elif input.get("type") == "web": case = WebCase() else: case = PyCase(module_name, module_path) try: case.constructor(input) case.bind_bench(bench_name) case.sqlinfo.bind_config(bench.get_db(case.sqlinfo.config_id))