def check_opts(choice): """ Parse up the user input and run whatever commands are needed. """ choice_opts = choice.split(' ') if len(choice_opts) == 1: if type(choice) is int: return choice elif 'help' in choice: help() choice = -1 elif 'gops' in choice: config.dump() choice = -1 elif 'quit' in choice or 'exit' in choice: # hard quit os._exit(1) elif 'bg' in choice: background() else: if 'set' in choice_opts[0]: opts = choice.split(' ') if opts[1] is None or opts[2] is None: return print '[!] Setting ' + color.YELLOW + '%s' % opts[1] + color.END + \ '-> ' + color.GREEN + '%s..' % opts[2] + color.END config.set(opts[1], opts[2]) choice = -1 return choice
def check_opts(choice): """ Parse up the user input and run whatever commands are needed. """ if type(choice) is int: return choice elif 'info' in choice: Error('\'info\' not implemented yet.') choice = -1 elif 'help' in choice: help() choice = -1 elif 'set' in choice: opts = choice.split(' ') if opts[1] is None or opts[2] is None: return print '[!] Setting ' + color.YELLOW + '%s'%opts[1] + color.END + \ '-> ' + color.GREEN + '%s..'%opts[2] + color.END config.set(opts[1], opts[2]) choice = -1 elif 'opts' in choice: config.dump() choice = -1 elif 'quit' in choice or 'exit' in choice: # hard quit os._exit(1) elif 'bg' in choice: background() return choice
def setLevelScore(self): if not hasattr(self, 'levelData'): return self.score = self.lives previousScore = 0 if "score" in self.levelData: previousScore = self.levelData["score"] self.bestScore = previousScore if self.score > previousScore: self.levelData["score"] = self.score self.saveLevel() if self.score - previousScore > 0: scoreDifference = self.score - previousScore else: scoreDifference = 0 # Use this in the menu animation previousKeys = config["player"]["keys"] config["player"]["keys"] += scoreDifference dump(config) return previousKeys, scoreDifference, previousScore
def get_artifact(message): if not check_text(message, get_artifact): return code = message.text.upper() try: player = Player.get(Player.tg_id == message.chat.id) except DoesNotExist: bot.send_message(message.chat.id, 'Не могу найти вас в списках игроков. Напишите @yury_zh') logger.error("Can't find user - {} in players database!".format(message.from_user.username)) return if config.secondaryArtifacts.count(code): # TODO: Delete artifact config.secondaryArtifacts.remove(code) # Deleting artifact config.dump() bot.send_message(player.tg_id, config.artifactSecondary) player.energy += config.secondaryEnergyAmount player.save() logger.info("Secondary artifact was registered by {}".format(player.name)) return exists = False artifact_race = 0 artifact_pos = 0 while artifact_race < 10: if config.artifacts[artifact_race].count(code) > 0: exists = True artifact_pos = config.artifacts[artifact_race].index(code) break artifact_race += 1 if not exists: bot.send_message(message.chat.id, config.artifactWrongCode) return artifact_race += 1 # 0-numeration -> 1-numeration if player.race != artifact_race: bot.send_message(message.chat.id, config.artifactWrongRace) logger.info("Team {} (race: {}) has found artifact of race {}".format(player.name, player.race, artifact_race)) return if player.currentPurpose < artifact_pos: bot.send_message(player.tg_id, config.artifactTooEarly) logger.info("Team {} (Current purpose: {}) has fond purpose {}".format( player.name, player.currentPurpose, artifact_pos )) elif player.currentPurpose > artifact_pos: bot.send_message(player.tg_id, config.artifactUsed) else: player.currentPurpose += 1 if player.currentPurpose >= len(config.purposes[player.race - 1]): player.finish = True if currentRound >= 10: player.time += timer.get_time() bot.send_message(player.tg_id, config.endings[player.race - 1]) else: logger.warning("Team {} has done all purposes before 10th round!!!".format(player.name)) bot.send_message(player.tg_id, 'Вы завершили все свои цели до окончания квеста!\n' 'Напишите @{}'.format(config.creatorUsername)) player.save() bot.send_message(player.tg_id, config.purposes[player.race - 1][player.currentPurpose - 1]) logger.info("Team {} has reached purpose {}".format(player.name, player.currentPurpose - 1))
def deleteLevel(self): path = self.game.mapLoader.getMap(self.levelData["mapName"]) if os.path.exists(path): # Delete the level os.remove(path) self.game.mapLoader.removeMap(self.levelData["mapName"]) self.game.mapLoader.removeCustomMap(self.levelData["mapName"]) self.game.mainMenu.updateCustomMaps() del config["maps"]["custom"][self.levelData["mapName"]] dump(config)
def print_menu(arr): global buffered """Main menu printer @param arr is the menu array to print. Fetches input, parses and built-in command keywords, and returns the selected idx. """ if not buffered is None: # buffered input, return if len(buffered) > 0: return buffered.pop(0) else: buffered = None tmp = Cmd() arr = ['\t[%d] %s' % (x + 1, arr[x]) for x in xrange(len(arr))] tmp.columnize(arr, 35) print '\n0) Back' try: choice = raw_input('> ') if 'info' in choice: Error('\'info\' not implemented yet.') choice = -1 elif 'set' in choice: opts = choice.split(' ') if opts[1] is None or opts[2] is None: return print '[!] Setting \033[33m%s\033[0m -> \033[32m%s\033[0m..' % ( opts[1], opts[2]) config.set(opts[1], opts[2]) choice = -1 elif 'opts' in choice: config.dump() choice = -1 elif 'quit' in choice or 'exit' in choice: # hard quit os._exit(1) elif 'bg' in choice: background() else: # buffered input choice = choice.split(' ') if len(choice) > 1: buffered = [] for entry in choice[1:]: buffered.append(int(entry)) choice = int(choice[0]) except Exception: os.system('clear') choice = -1 return choice
def print_menu(arr): global buffered """Main menu printer @param arr is the menu array to print. Fetches input, parses and built-in command keywords, and returns the selected idx. """ if not buffered is None: # buffered input, return if len(buffered) > 0: return buffered.pop(0) else: buffered = None tmp = Cmd() arr = ['\t[%d] %s'%(x+1,arr[x]) for x in xrange(len(arr))] tmp.columnize(arr,35) print '\n0) Back' try: choice = raw_input('> ') if 'info' in choice: Error('\'info\' not implemented yet.') choice = -1 elif 'set' in choice: opts = choice.split(' ') if opts[1] is None or opts[2] is None: return print '[!] Setting \033[33m%s\033[0m -> \033[32m%s\033[0m..'%(opts[1], opts[2]) config.set(opts[1], opts[2]) choice = -1 elif 'opts' in choice: config.dump() choice = -1 elif 'quit' in choice or 'exit' in choice: # hard quit os._exit(1) elif 'bg' in choice: background() else: # buffered input choice = choice.split(' ') if len(choice) > 1: buffered = [] for entry in choice[1:]: buffered.append(int(entry)) choice = int(choice[0]) except Exception: os.system('clear') choice = -1 return choice
def run_stages(self): config.log_to_file(config.log_file) logger.info('BEGIN') while config.stage < len(self.__stages): stage = self.__stages[config.stage] try: logger.info('running {.__name__}'.format(stage)) stage() except: logger.error('error---saving state') with open(config.failure_file, 'w') as failure_f: print >>failure_f, config.stage #logger.error(traceback.format_exc()) config.dump(config.config_file) raise config.dynamic('stage', config.stage + 1)
def run_stages(self): config.log_to_file(config.log_file) logger.info('BEGIN') while config.stage < len(self.__stages): stage = self.__stages[config.stage] try: logger.info('running {.__name__}'.format(stage)) stage() except: logger.error('error---saving state') with open(config.failure_file, 'w') as failure_f: print >> failure_f, config.stage #logger.error(traceback.format_exc()) config.dump(config.config_file) raise config.dynamic('stage', config.stage + 1)
def begin_cmd(message): # TODO: Beginning of the quest config.dump() autosave() global currentRound currentRound = 0 timer.set_duration(default_duration) for player in Player.select(): player.currentRound = 0 player.save() bot.send_message(player.tg_id, config.nextKpName.format( config.kp_rus.get(config.kp[player.race - 1][0], default=config.kp[player.race - 1][0]))) for kp in User.select().where(User.role == Role.KP): kp.currentTeamName = set_next_team(kp, 0) kp.save() bot.send_message(kp.tg_id, 'Текущая группа - {}'.format(kp.currentTeamName)) timer.start(next_round) everyone('Приключение началось! Удачи!') logger.info('Quest has been started by @{}'.format(message.from_user.username))
def saveLevelAs(self): # Name of the map self.levelData["mapName"] = self.game.textHandler.getString() self.levelData["deletable"] = True self.levelData["saved"] = True # saveName = "map" + str( # len(self.game.mapLoader.getMaps()) + 1) + '.json' saveName = ("map_" + self.game.textHandler.getString().replace(" ", "_") + '.json') path = os.path.join(MAPSFOLDER, saveName) with open(path, "w") as f: json.dump(self.levelData, f) config["maps"]["custom"][self.game.textHandler.getString()] = saveName dump(config) self.game.mapLoader.addMap(self.game.textHandler.getString(), path, self.game.mapLoader.getCustomMaps()) self.game.mainMenu.updateCustomMaps()
def sns_handler(event, context): """ Parameters ---------- event: dict, required context: object, required Lambda Context runtime methods and attributes Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html Returns ------ """ global ctx ctx["now"] = misc.utc_now() log.log(log.NOTICE, "Handler start.") ctx["FunctionName"] = "SNS" log.info("Processing start (version=%s)" % (ctx.get("CloneSquadVersion"))) init() misc.load_prerequisites(ctx, ["o_state", "o_notify", "o_targetgroup"]) Cfg.dump() sns_mgr = sns.SNSMgr(ctx, ctx["o_ec2"]) r = sns_mgr.handler(event, context) # Persist all aggregated data KVTable.persist_aggregates() # Call me back if needed call_me_back_send() log.log(log.NOTICE, "Normal end.") return r
def print_menu(arr): i = 0 while i < len(arr): # if there are more than 6 items in the list, add another column if len(arr) > 6 and i < len(arr)-1: print '\t[%d] %s \t [%d] %s'%(i+1,arr[i],i+2,arr[i+1]) i += 2 else: print '\t[%d] %s'%(i+1,arr[i]) i += 1 print '\n0) Back' try: choice = raw_input('> ') if 'info' in choice: Error('\'info\' not implemented yet.') #stream.view_info(choice.split(' ')[1]) choice = -1 elif 'set' in choice: opts = choice.split(' ') if opts[1] is None or opts[2] is None: return print '[!] Setting \033[33m%s\033[0m -> \033[32m%s\033[0m..'%(opts[1], opts[2]) config.set(opts[1], opts[2]) choice = -1 elif 'opts' in choice: config.dump() choice = -1 elif 'quit' in choice: # hard quit os._exit(1) else: choice = int(choice) except Exception: os.system('clear') choice = -1 return choice
def ar_inner(): with open(config.failure_file, 'w') as failure_f: config.dump(config.config_file) print >>failure_f, config.stage
def evaluate(self, train=None, test=None, verbose=0, eval_num=0, params=None): #np.arange(0.0, 2.0, 0.1)): coef, weights, error, iterations = None, None, None, None if params != None: coef, weights, error, iterations = self.grid_search_train( train, params=params) else: coef = self.l2_coeff weights, error, iterations, messages = self.SGD( D=train, l2_coeff=self.l2_coeff, verbose=verbose) # Optionally view weights: if verbose == 2: for key, val in sorted(weights.items(), key=itemgetter(1, 0), reverse=True): if val != 0.0: print key, val config.dump(weights, 'params.%s.json' % eval_num) # Evaluation: predictions = [] for i, d in enumerate(test): (_, x, _, distractors) = d[:4] if verbose >= 2: print 'Evaluating %d of %d' % (i, len(test)) predictions.append( self.predict(x=x, w=weights, messages=messages, distractors=distractors)) gold = [d[2] for d in test] # id, x, y, ... with config.open('predictions.%s.jsons' % eval_num, 'w') as outfile: for d, prediction in zip(test, predictions): (id, x, y) = d[:3] report = { 'id': str(id), 'input': str(x), 'gold': str(y), 'prediction': str(prediction) } json.dump(report, outfile) if verbose >= 2 and y != prediction: print 'Wrong: %s' % str(report) outfile.write('\n') results = { 'iterations': iterations, 'error': error, 'evaluations': {}, 'l2_coeff': coef } for metric in self.metrics: evaluation = metric(gold, predictions) results['evaluations'][metric.__name__] = evaluation if verbose: print "%s: %0.03f" % (metric.__name__, evaluation) return results
def ar_inner(): with open(config.failure_file, 'w') as failure_f: config.dump(config.config_file) print >> failure_f, config.stage
def main_handler_entrypoint(event, context): """ Parameters ---------- event: dict, required context: object, required Lambda Context runtime methods and attributes Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html Returns ------ """ #print(Dbg.pprint(event)) ctx["now"] = misc.utc_now() ctx["FunctionName"] = "Main" init() if Cfg.get_int("app.disable") != 0 and not misc.is_sam_local(): log.warning("Application disabled due to 'app.disable' key") return no_is_called_too_early = False # Manage Spot interruption as fast as we can if sqs.process_sqs_records(event, function=ec2_schedule.manage_spot_notification, function_arg=ctx): log.info("Managed Spot Interruption SQS record!") # Force to run now disregarding `app.run_period` as we have at least one Spot instance to # remove from target groups immediatly no_is_called_too_early = True # Check that we are not called too early # Note: We peform a direct read to the KVTable to spare initialization time when the # Lambda is called too early ctx["main.last_call_date"] = ctx["o_ec2"].get_state("main.last_call_date", direct=True) if ctx["main.last_call_date"] is None or ctx["main.last_call_date"] == "": ctx["main.last_call_date"] = str(misc.epoch()) if not no_is_called_too_early and is_called_too_early(): log.log(log.NOTICE, "Called too early by: %s" % event) notify.do_not_notify = True sqs.process_sqs_records(event) sqs.call_me_back_send() return log.debug("Load prerequisites.") load_prerequisites(["o_state", "o_notify", "o_ec2", "o_cloudwatch", "o_targetgroup", "o_ec2_schedule", "o_scheduler", "o_rds"]) # Remember 'now' as the last execution date ctx["o_ec2"].set_state("main.last_call_date", value=ctx["now"], TTL=Cfg.get_duration_secs("app.default_ttl")) Cfg.dump() # Perform actions: log.debug("Main processing.") ctx["o_targetgroup"].manage_targetgroup() ctx["o_ec2_schedule"].schedule_instances() ctx["o_ec2_schedule"].stop_drained_instances() ctx["o_cloudwatch"].configure_alarms() ctx["o_rds"].manage_subfleet_rds() ctx["o_ec2_schedule"].prepare_metrics() ctx["o_cloudwatch"].send_metrics() ctx["o_cloudwatch"].configure_dashboard() # If we got woke up by SNS, acknowledge the message(s) now sqs.process_sqs_records(event) ctx["o_notify"].notify_user_arn_resources() # Call me back if needed sqs.call_me_back_send()
def main(argv=None): if not argv: argv = sys.argv usage = 'usage: muttlearn [options] [-o file] mbox1 [mbox2...]\n'\ ' muttlearn [options]\n'\ ' muttlearn -D\n'\ ' muttlearn --output-only' version = u'muttlearn %s\nCopyright (C) 2010 Johannes Weißl\n'\ 'License GPLv3+: GNU GPL version 3 or later '\ '<http://gnu.org/licenses/gpl.html>.\n'\ 'This is free software: you are free to change and redistribute it.\n'\ 'There is NO WARRANTY, to the extent permitted by law.' % __version__ desc = 'muttlearn is an alternative to manually maintaining profiles in mutt. '\ 'It scans your mailboxes, learns how you want to write mails (e.g. '\ 'with which from address, what signature, what greeting, etc.) and '\ 'outputs send-hooks per recipient or group of recipients.' locale.setlocale(locale.LC_ALL, '') parser = optparse.OptionParser(usage=usage, version=version, description=desc, prog='muttlearn') parser.add_option('-o', dest='output', default=None, metavar='FILE', help='output file, "-" for stdout') parser.add_option('-D', dest='dump_vars', action='store_true', default=False, help='print the value of all configuration options to stdout.') parser.add_option('-F', dest='muttlearnrc', default=None, metavar='FILE', help='use alternative config file instead of ~/.muttlearnrc') parser.add_option('-v', '--verbose', dest='verbosity', action='count', default=0, help='be verbose (multiple times: more verbose)') parser.add_option('-p', '--progress', action='store_true', default=False, help='show progress') parser.add_option('--muttrc', default=None, metavar='FILE', help='use this file instead of ~/.muttrc to get mutt defaults') parser.add_option('-n', '--no-muttrc', dest='read_muttrc', action='store_false', default=True, help='do not read ~/.muttrc') parser.add_option('-C', '--rebuild-cache', action='store_true', default=False, help='rebuild message cache (slow)') parser.add_option('-c', '--clean-cache', action='store_true', default=False, help='remove unused messages from the cache') parser.add_option('--output-only', action='store_true', default=False, help='do not scan messages, just output (very fast)') options, args = parser.parse_args(argv[1:]) log.verbosity = options.verbosity if not options.read_muttrc and options.muttrc: parser.error('-n and --muttrc cannot both be specified') config.init(conf_path=options.muttlearnrc, mutt_conf_path=options.muttrc, read_muttrc=options.read_muttrc) if options.dump_vars: config.dump() return if not options.output: options.output = config.get('output_file').encode(locale.getpreferredencoding()) if not options.output: parser.error('no output file specified, use $output_file or -o') mailbox_paths = args if args else config.get_mailboxes() if not mailbox_paths: parser.error('no mailbox specified for learning!') acquire_lock() if options.rebuild_cache: use_cache = False else: use_cache = not config.db_needs_rebuilding() if not use_cache: options.clean_cache = True log.debug('rebuilding message cache (slow!)') config.save_to_cache() else: log.debug('using message cache (faster)') if options.output_only: #recipients = load_recipients() recipients = gen_recipients_from_cache(config.options(), progress=options.progress) else: scan.init(config.options()) mailboxes = [scan.Mailbox(path, 'auto') for path in mailbox_paths] recipients = gen_recipients(mailboxes, config.options(), use_cache=use_cache, clean_cache=options.clean_cache, progress=options.progress) #save_recipients(recipients) if options.output == '-': outfile = sys.stdout else: try: outfile = open(options.output, 'wb') except IOError, e: log.error('error opening output file: %s', e)