def delete_topic(self): try: user = check( self, users.get_current_user(), lambda: self.redirect(webapp2.uri_for('login'))) topic_url = check( self, self.request.get('topic_url'), 'Unable to delete topic page. No topic specified.') topic = check( self, datastore.Topic.retrieve(topic_url), 'Unable to delete topic page. Topic cannot be found.') correct_user = check( self, topic.creator_id == user.user_id(), 'Unable to delete topic page. You are not the user who created the page.') topic_name = topic.name topic.key.delete() datastore.SearchIndexShard.remove_topic(topic_url, topic_name) #self.redirect(str(self.request.host_url)) self.response.write('The page "' + topic_name + '" has been deleted.') except AssertionError as e: logging.info(str(e.args))
def apply_action(self): tgt = self.target g = Game.getgame() n = min(len([p for p in g.players if not p.dead]), 5) cards = g.deck.getcards(n) assert cards == g.deck.getcards(n) tgt.reveal(cards) rst = tgt.user_input('ran_prophet', cards, timeout=40) if not rst: return False try: check_type([[int, Ellipsis]]*2, rst) upcards = rst[0] downcards = rst[1] check(sorted(upcards+downcards) == range(n)) except CheckFailed as e: try: print 'RAN PROPHET:', upcards, downcards except: pass return act deck = g.deck.cards for i, j in enumerate(downcards): deck[i] = cards[j] deck.rotate(-len(downcards)) for i, j in enumerate(upcards): deck[i] = cards[j] cl = [cards[i] for i in upcards] assert g.deck.getcards(len(upcards)) == cl return True
def parse(self, data): target = self.target categories = self.categories categories = [getattr(target, i) for i in categories] assert all(c.owner is target for c in categories) try: check(sum(len(c) for c in categories)) # no cards at all cid = data g = Game.getgame() check(isinstance(cid, int)) cards = g.deck.lookupcards((cid,)) check(len(cards) == 1) # Invalid id card = cards[0] check(card.resides_in.owner is target) check(card.resides_in in categories) return card except CheckFailed: return None
def get_user_tokeninfo(self): """ Get user token details Returns: -------- JSON message containing access token details e.g., Response 200 OK (application/json) { "header": { "typ": "JWT", "alg": "RS256" }, "payload": { "jti": "7b1430a2-dd61-4a47-919c-495cadb1ea7b", "iss": "http://enableiot.com", "sub": "53fdff4418b547e4241b8358", "exp": "2014-10-02T07:53:25.361Z" } } """ url = "{0}/auth/tokenInfo".format(self.base_url) resp = requests.get(url, headers=get_auth_headers(self.user_token), proxies=self.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() return js
def apply_action(self): g = Game.getgame() target = self.target if target.dead: return False try: while not target.dead: try: g.emit_event('action_stage_action', target) self.in_user_input = True with InputTransaction('ActionStageAction', [target]) as trans: p, rst = ask_for_action( self, [target], ('cards', 'showncards'), g.players, trans ) check(p is target) finally: self.in_user_input = False cards, target_list = rst g.players.reveal(cards) card = cards[0] if not g.process_action(ActionStageLaunchCard(target, target_list, card)): # invalid input log.debug('ActionStage: LaunchCard failed.') check(False) if self.one_shot or self._force_break: break except CheckFailed: pass return True
def importScoresByNames(self, scores, assessmentId = 0, exactGradesourceNames = False): if assessmentId == 0: assessmentId = self.chooseAssessment() GsNameToStudentId, postData = self.parseScoresForm(assessmentId) utils.check("Gradesource name -> studentId: ", GsNameToStudentId) errors = False if not exactGradesourceNames: ExtNameToGsName = self.matchNames(scores.keys(), GsNameToStudentId.keys()) for extName, GsName in ExtNameToGsName.items(): postData[GsNameToStudentId[GsName]] = scores[extName] else: for GsName, score in scores.items(): if GsName in GsNameToStudentId: postData[GsNameToStudentId[GsName]] = score else: cprint('Missing name: ' + GsName, 'white', 'on_red') errors = True if errors: sys.exit() utils.check("Data to post: ", postData) self.postScores(postData) cprint("Go to %s" % (self.assessmentUrl % assessmentId), 'yellow')
def login(self, username, password): """ Submit IoT Analytics user credentials to obtain the access token Args: ---------- username (str): username for IoT Analytics site password (str): password for IoT Analytics site Returns: Sets user_id and user_token attributes for connection instance """ if not username or not password: raise ValueError( "Invalid parameter: username and password required") try: url = "{0}/auth/token".format(self.base_url) headers = {'content-type': 'application/json'} payload = {"username": username, "password": password} data = json.dumps(payload) resp = requests.post( url, data=data, headers=headers, proxies=self.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() self.user_token = js['token'] # get my user_id (uid) within the Intel IoT Analytics Platform js = self.get_user_tokeninfo() self.user_id = js["payload"]["sub"] except Exception, err: raise RuntimeError('Auth ERROR: %s\n' % str(err))
def cond(self, cardlist): from .. import cards try: check(len(cardlist) == 1) check(cardlist[0].is_card(cards.RejectCard)) return True except CheckFailed: return False
def temp(csvPath, col): config = utils.getConfig() reader = csv.reader(open(csvPath, 'rU'), delimiter=',') data = [{'name': "{0[0]}, {0[1]}".format(row), 'score': row[col], 'pid': row[2]} for row in reader if row[col] not in [0, '0', '-', '']] utils.check("Data: ", data) g = Gradesource(config['gradesourceLogin'], config['gradesourcePasswd']) g.importScoresBy(data, 'pid')
def get_attributes(self): url = "{0}/accounts/{1}/devices".format( self.client.base_url, self.account_id) resp = requests.get(url, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() return js
def get_user_info(self, user_id=None): # Get the user's info url = "{0}/users/{1}".format(globals.base_url, user_id) resp = requests.get(url, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() self.id = js["id"] return js
def __init__(self, name=u'<未指定名字的场地>', tl=None): super(self.__class__, self).__init__() self.name = unicode(name) self.comment = u"" if tl is not None: check(isinstance(tl, WeekTimeline), 'tl must be a WeekTimeline!') self.timeavail = tl else: self.timeavail = WeekTimeline()
def __setslice__(self, f, t, v): ''' Set time status ''' check(0 <= f < t <= 1440, 'Timeline: f and t should satisfy 0 <= f < t <= 1440!') v = str(v) check(len(v) > 0, 'Timeline: len(v) == 0!') self.tldata = self.tldata[:f] + (v * (int((t - f)/len(v)) + 1))[:(t - f)] + self.tldata[t:]
def uploadClickerScores(csvPath, col): config = utils.getConfig() reader = csv.reader(open(csvPath, 'rU'), delimiter=',') # Fields: 0:LN, 1:FN, 2:id, >2:scores data = [{'name': '%s, %s' % (row[0], row[1]), 'score': row[col], 'pid': row[2]} for row in reader if row[col] not in [0, '0', '-', '']] utils.check("Clicker data: ", data) g = Gradesource(config['gradesourceLogin'], config['gradesourcePasswd']) g.importScoresBy(data, 'pid')
def get_comp_types(self, full=False): url = "{0}/accounts/{1}/cmpcatalog".format(self.client.base_url, self.account.id) if full == True: url += "?full=true" resp = requests.get(url, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() return js
def parse(self, data): n = self.num try: check(data) check_type([int] * n, data) check(set(data) == set(range(n))) return data except CheckFailed: return range(n)
def __init__(self, tl=None): super(self.__class__, self).__init__() if tl is None: self.tldata = 'X' * 1440 elif isinstance(tl, Timeline): self.tldata = tl.tldata[:] elif isinstance(tl, str): self.tldata = (tl * (int(1440/len(tl)) + 1))[:1440] else: check(False, "tl is nether a Timeline nor a string!")
def parse(self, data): try: cid = data check(isinstance(cid, int)) cards = [c for c in self.cards if c.syncid == cid] check(len(cards)) # Invalid id return cards[0] except CheckFailed: return None
def process(actor, rst): g = Game.getgame() try: check(rst) skills, cards, players = rst [check(not c.detached) for c in cards] if categories: if skills: # check(len(skills) == 1) # why? disabling it. # will reveal in skill_wrap skill = skill_wrap(actor, skills, cards) check(skill and initiator.cond([skill])) else: if not getattr(initiator, 'no_reveal', False): g.players.reveal(cards) check(initiator.cond(cards)) if candidates: players, valid = initiator.choose_player_target(players) check(valid) return skills, cards, players except CheckFailed: return None
def post_process(self, actor, rst): g = Game.getgame() putback, acquire = rst g.players.exclude(actor).reveal(acquire) try: check(self.is_valid(putback, acquire)) except CheckFailed: return [self.cards, []] return rst
def validate(Server, Port): """command validate method returns string""" logger = PLLogger.GetLogger('spirent.vds') msg = '' msg = check(Server, 'Server', msg) msg = check(Port, 'Port', msg) if len(msg) != 0: msg = 'list logs failed validation' + msg logger.LogError(msg) return msg return ''
def get_account_invites(self): if self.account: url = "{0}/accounts/{1}/invites".format( self.client.base_url, self.account.id) resp = requests.get(url, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() return js else: raise ValueError("No account provided.")
def move(self, direction=FORWARD, period=DEF_TIME, speed=DEF_SPEED): logging.debug('Moving motor dir=%s time=%d speed=%d' % (direction, period, speed)) speed = utils.check(speed, Motor.MIN_SPEED, Motor.MAX_SPEED) period = utils.check(period, Motor.MIN_TIME, Motor.MAX_TIME) self.p.ChangeDutyCycle(speed) GPIO.output(self.pin_direct1, direction) GPIO.output(self.pin_direct2, not direction) time.sleep(period) logging.debug('Stop motor') GPIO.output(self.pin_direct1, 0) GPIO.output(self.pin_direct2, 0)
def get_user_invites(self, email): if email: url = "{0}/invites/{1}".format( self.client.base_url, urllib.quote(email)) resp = requests.get(url, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() return js else: raise ValueError("No email provided.")
def update_user(self, user_info): if user_info: # given a user_id, get the user's info url = "{0}/users/{1}".format(globals.base_url, self.id) data = json.dumps(user_info) resp = requests.put(url, data=data, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) else: raise ValueError("No user info given.") return None
def update_device(self, device_info, device_id=None): if not device_id: device_id = self.device_id url = "{0}/accounts/{1}/devices/{2}".format( self.client.base_url, self.account_id, device_id) data = json.dumps(device_info) resp = requests.put(url, data=data, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) js = resp.json() self.info = js return js
def uploadMoodleQuizScores(): config = utils.getConfig() m = Moodle(config['moodleLogin'], config['moodlePasswd']) _, scores = m.getScores(config['moodleCourseId']) # Fields: 0:FN, 1:LN, 2:pid, 3:inst, 4:dpt, 5:email, 6:total, 7:score data = [{'name': '%s, %s' % (row[1], row[0]), 'score': row[7], 'pid': row[2], 'email': row[5]} for row in scores if row[7] not in [0, '0', '-', '']] utils.check("Moodle data: ", data) g = Gradesource(config['gradesourceLogin'], config['gradesourcePasswd']) g.importScoresBy(data, 'pid')
def send_data(self, dataSeries): url = "{0}/data/{1}".format(self.client.base_url, self.device_id) payload = { "on": time.time(), "accountId": self.account_id, "data": dataSeries } data = json.dumps(payload) resp = requests.post(url, data=data, headers=get_auth_headers( self.device_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 201) return resp.text
def add_comp_type(self, component_info=None): if component_info: url = "{0}/accounts/{1}/cmpcatalog".format(self.client.base_url, self.account.id) data = json.dumps(component_info) resp = requests.post(url, data=data, headers=get_auth_headers( self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 201) js = resp.json() return js else: raise ValueError("No component info given.")
def request_password_reset(self, email): if email: # given a user_id, get the user's info url = "{0}/users/forgot_password".format(globals.base_url) payload = {"email": email} data = json.dumps(payload) resp = requests.post(url, data=data, headers=globals.headers, proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) else: raise ValueError("No email given.") return None
async def bridge_of_death(self, ctx): client = self.bot colors = [ "red", "green", "blue", "black", "white", "yellow", "orange", "brown", "pink", "purple", "gray", "grey" ] await ctx.send( "He who wishes to cross the bridge of Death, must answer me these questions three" ) await asyncio.sleep(2) await ctx.send('What is your name?') try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: return await self.failure(ctx) if not msg.content.lower() == ctx.author.name.lower( ) and not msg.content.lower() == ctx.author.nick.lower(): return await self.failure(ctx) name = msg.content await ctx.send(name + '\nWhat is your favorite color?') try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: await self.failure(ctx) return if not msg.content.lower() in colors: await self.failure(ctx) return q3 = random.randint(1, 10) x = random.randint(1, 100) y = random.randint(1, 100) if q3 == 1: question = "What is {}+{}?".format(x, y) answer = str(x + y) elif q3 == 2: question = "What is {}-{}?".format(x, y) answer = str(x - y) elif q3 == 3: question = "What year is it?" answer = str(datetime.now().year) elif q3 == 4: question = "What is the airspeed velocity of an unladen swallow?" answer = "african or european?" elif q3 > 4: url = "https://opentdb.com/api.php?amount=1" try: api_QA = requests.get(url).json() except requests.exceptions.RequestException as e: return await ctx.send("I have a bug. Tell my creator\n" + e) question = "" try: if api_QA["results"][0]["type"] == "boolean": question += "True or False?\n" question += utils.decodeHTMLSymbols( api_QA["results"][0]["question"]) if api_QA["results"][0]["type"] == "multiple": choices = [] choices.append( utils.decodeHTMLSymbols( api_QA["results"][0]["correct_answer"])) for ans in api_QA["results"][0]["incorrect_answers"]: choices.append(utils.decodeHTMLSymbols(ans)) question += "\nChoices:\n" while len(choices) > 1: i = random.randint(0, len(choices) - 1) question += (choices[i] + "\n") del choices[i] question += choices[0] answer = utils.decodeHTMLSymbols( api_QA["results"][0]["correct_answer"]) except KeyError as e: return await ctx.send( "I have a bug in my API usage. Please open an issue with my creator and tell him\n" + e) else: return await ctx.send("You really should not be seeing this...") await ctx.send(name + "\n" + question) try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=15) except asyncio.TimeoutError: return await self.failure(ctx) if q3 == 4 and str(msg.content).lower() == answer.lower(): await ctx.send("Huh? I… I don’t know that.") return await ctx.send( "https://i.makeagif.com/media/8-06-2015/6tzcHH.gif") if not str(msg.content).lower() == answer.lower(): return await self.failure(ctx) await ctx.send("Be on your way") return await ctx.send( "https://tenor.com/view/monty-python-holy-grail-horse-gif-3448553")
def game_start(self): # game started, init state from cards import Card, Deck, CardList self.deck = Deck() ehclasses = list(action_eventhandlers) + self.game_ehs.values() for i, p in enumerate(self.players): p.cards = CardList(p, 'handcard') # Cards in hand p.showncards = CardList( p, 'showncard' ) # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, 'equips') # Equipments p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone p.special = CardList(p, 'special') # used on special purpose p.showncardlists = [p.showncards, p.fatetell] p.tags = defaultdict(int) p.dead = False p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] self.forces = forces = BatchList([PlayerList(), PlayerList()]) for i, p in enumerate(self.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from characters import characters as chars from characters.akari import Akari if Game.SERVER_SIDE: choice = [ CharChoice(cls, cid) for cls, cid in zip(self.random.sample(chars, 16), xrange(16)) ] for c in self.random.sample(choice, 4): c.real_cls = c.char_cls c.char_cls = Akari elif Game.CLIENT_SIDE: choice = [CharChoice(None, i) for i in xrange(16)] # ----------- self.players.reveal(choice) # roll roll = range(len(self.players)) self.random.shuffle(roll) pl = self.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] self.emit_event('game_roll', roll) first = roll[0] self.emit_event('game_roll_result', first) # ---- first_index = self.players.index(first) n = len(self.order_list) order = [self.players[(first_index + i) % n] for i in self.order_list] def mix(p, c): # mix char class with player --> mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] self.emit_event('choose_girl_begin', (self.players, choice)) for i, p in enumerate(order): cid = p.user_input('choose_girl', choice, timeout=(n - i + 1) * 5) try: check(isinstance(cid, int)) check(0 <= cid < len(choice)) c = choice[cid] check(not c.chosen) c.chosen = p except CheckFailed: # first non-chosen char for c in choice: if not c.chosen: c.chosen = p break if issubclass(c.char_cls, Akari): akaris.append((p, c)) else: mix(p, c) self.emit_event('girl_chosen', c) self.emit_event('choose_girl_end', None) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls self.players.reveal([i[1] for i in akaris]) for p, c in akaris: mix(p, c) first_actor = first self.event_handlers = EventHandler.make_list(ehclasses) # ------- log.info(u'>> Game info: ') log.info(u'>> First: %s:%s ', first.char_cls.__name__, Identity.TYPE.rlookup(first.identity.type)) for p in self.players: log.info(u'>> Player: %s:%s %s', p.char_cls.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username) # ------- try: pl = self.players for p in pl: self.process_action(RevealIdentity(p, pl)) self.emit_event('game_begin', self) for p in self.players: self.process_action( DrawCards(p, amount=3 if p is first_actor else 4)) pl = self.players.rotate_to(first_actor) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: self.emit_event('player_turn', p) try: self.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u'>> Winner: %s', Identity.TYPE.rlookup(self.winners[0].identity.type))
def parse(self, data): # data = [ # [skill_index1, ...], # [card_sync_id1, ...], # [player_id1, ...], # {'action_param1': 'AttackCard'}, # ] actor = self.actor g = Game.getgame() categories = self.categories categories = [getattr(actor, i) for i in categories] if categories else None candidates = self.candidates skills = [] cards = [] players = [] params = {} _ = Ellipsis try: check_type([[int, _]] * 3 + [dict], data) sid_list, cid_list, pid_list, params = data if candidates: check(candidates) pl = [g.player_fromid(i) for i in pid_list] check(all([p in candidates for p in pl])) players = pl if categories: cards = g.deck.lookupcards(cid_list) check(len(cards) == len(cid_list)) # Invalid id cs = set(cards) check(len(cs) == len(cid_list)) # repeated ids if sid_list: assert actor.cards in categories or actor.showncards in categories check(all(cat.owner is actor for cat in categories)) check(all(c.resides_in.owner is actor for c in cards)) # Cards belong to actor? for skill_id in sid_list: check(0 <= skill_id < len(actor.skills)) skills = [actor.skills[i] for i in sid_list] else: check(all(c.resides_in in categories for c in cards)) # Cards in desired categories? return [skills, cards, players, params] except CheckFailed: return None
g = d.sum() # k = a + 1 # d = (k @ b).tanh() # e = d @ c.t() # f = e ** a.sum(1) # g = f.norm(dim=0, keepdim=True) # g = g.sum() # k.retain_grad() # c.retain_grad() d.retain_grad() # e.retain_grad() # f.retain_grad() g.retain_grad() g.backward() local = locals() vars = list('abcdg') ret_tuple = tuple(local[x] for x in vars) return ret_tuple, vars if __name__ == '__main__': ret1, vars = check_example() ret2 = check_example(pytorch=True)[0] check(ret1, ret2, vars)
def test(filename='data.bin'): t0 = time.time() import CNN_FW_Softmax_V1 as cnn print '[INFO] loading data : %s' %(filename) with open(filename) as inf: para = cPickle.load(inf) fb = cPickle.load(inf) fx = cPickle.load(inf) fw = cPickle.load(inf) fz = cPickle.load(inf) fmaxz = cPickle.load(inf) fexpz = cPickle.load(inf) fsumexpz = cPickle.load(inf) fsm = cPickle.load(inf) fpred = cPickle.load(inf) fstd = cPickle.load(inf) fb_grad = cPickle.load(inf) fw_grad = cPickle.load(inf) fx_grad = cPickle.load(inf) ni,no,batch_size = para x_offset = 0 x_size = ni*batch_size*type_size sm_offset = x_size sm_size = no*batch_size*type_size print '[INFO] Done: load data' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] running writeLMem' cnn.CNN_FW_Softmax_V1_writeLMem( param_offset = x_offset, param_size = x_size, instream_cpu_to_lmem_at_cpu = fx ) print '[INFO] Done: writeLMem' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] running kernel' res_pred = cnn.CNN_FW_Softmax_V1( param_ni = ni, param_x_offset = x_offset, param_softmax_offset = sm_offset, instream_b = fb, instream_w = fw ) print '[INFO] Done: Conv' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] running readLMem' res_sm = cnn.CNN_FW_Softmax_V1_readLMem( param_offset = sm_offset, param_size = sm_size ) print '[INFO] Done: readLMem' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] checking' ret0 = utils.check('sm',1e-9,res_sm,fsm) ret1 = utils.check('pred',1e-9,res_pred,fpred) print '[INFO] Done: check' return ret0 and ret1
def main(): # Test local versions of libraries utils.test_python_version() utils.test_gmpy2_version() utils.test_pari_version() utils.test_pari_seadata() now = datetime.now() # Parse command line arguments parser = argparse.ArgumentParser(description="Generate an Edwards curve over a given prime field, suited for cryptographic purposes.") parser.add_argument("input_file", help="""JSON file containing the BBS parameters and the prime of the underlying field (typically, the output of 03_generate_prime_field_using_bbs.py. """) parser.add_argument("output_file", help="Output file where this script will write the parameter d of the curve and the current BBS parameters.") parser.add_argument("--start", type=int, help="Number of the candidate to start with (default is 1).", default=1) parser.add_argument("--max_nbr_of_tests", type=int, help="Number of candidates to test before stopping the script (default is to continue until success).") parser.add_argument("--fast", help=""" While computing a the curve cardinality with SAE, early exit when the cardinality will obviously be divisible by a small integer > 4. This reduces the time required to find the final curve, but the cardinalities of previous candidates are not fully computed. """, default=False, action="store_true") args = parser.parse_args() # Check arguments print("Checking inputs...") output_file = args.output_file if os.path.exists(output_file): utils.exit_error("The output file '%s' already exists. Exiting."%(output_file)) input_file = args.input_file with open(input_file, "r") as f: data = json.load(f) # Declare a few important variables bbs_p = int(data["bbs_p"]) bbs_q = int(data["bbs_q"]) bbs_n = bbs_p * bbs_q bbs_s = int(data["bbs_s"]) % bbs_n p = int(data["p"]) start = max(int(args.start),1) max_nbr_of_tests = None if args.max_nbr_of_tests: max_nbr_of_tests = int(args.max_nbr_of_tests) if not subroutines.is_strong_strong_prime(bbs_p): utils.exit_error("bbs_p is not a strong strong prime.") if not subroutines.is_strong_strong_prime(bbs_q): utils.exit_error("bbs_q is not a strong strong prime.") if not (subroutines.deterministic_is_pseudo_prime(p) and p%4 == 3): utils.exit_error("p is not a prime congruent to 3 modulo 4.") # Initialize BBS print("Initializing BBS...") bbs = bbsengine.BBS(bbs_p, bbs_q, bbs_s) # Info about the prime field utils.colprint("Prime of the underlying prime field:", "%d (size: %d)"%(p, gmpy2.bit_length(p))) size = gmpy2.bit_length(p) # total number of bits queried to bbs for each test # Skip the first "start" candidates candidate_nbr = start-1 bbs.skipbits(size * (start-1)) # Start looking for "d" while True: if max_nbr_of_tests and candidate_nbr >= start + max_nbr_of_tests - 1: print("Did not find an adequate parameter, starting at candidate %d (included), limiting to %d candidates."%(start, max_nbr_of_tests)) utils.exit_error("Last candidate checked was number %d."%(candidate_nbr)) candidate_nbr += 1 bits = bbs.genbits(size) d = 0 for bit in bits: d = (d << 1) | bit print("The candidate number %d is d = %d (ellapsed time: %s)"%(candidate_nbr, d, str(datetime.now()-now))) # Test 1 if not utils.check(d != 0 and d < p, "d != 0 and d < p", 1): continue # Test 2 if not utils.check(gmpy2.legendre(d, p) == -1, "d is not a square modulo p", 2): continue # Test 3 if args.fast: cardinality = subroutines.sea_edwards(1, d, p, 4) else: cardinality = subroutines.sea_edwards(1, d, p) assert(cardinality % 4 == 0) q = cardinality>>2 if not utils.check(subroutines.deterministic_is_pseudo_prime(q), "The curve cardinality / 4 is prime", 3): continue # Test 4 trace = p+1-cardinality cardinality_twist = p+1+trace assert(cardinality_twist % 4 == 0) q_twist = cardinality_twist>>2 if not utils.check(subroutines.deterministic_is_pseudo_prime(q_twist), "The twist cardinality / 4 is prime", 4): continue # Test 5 if not utils.check(q != p and q_twist != p, "Curve and twist are safe against additive transfer", 5): continue # Test 6 embedding_degree = subroutines.embedding_degree(p, q) if not utils.check(embedding_degree > (q-1) // 100, "Curve is safe against multiplicative transfer", 6): continue # Test 7 embedding_degree_twist = subroutines.embedding_degree(p, q_twist) if not utils.check(embedding_degree_twist > (q_twist-1) // 100, "Twist is safe against multiplicative transfer", 7): continue # Test 8 D = subroutines.cm_field_discriminant(p, trace) if not utils.check(abs(D) >= 2**100, "Absolute value of the discriminant is larger than 2^100", 8): continue break # Find a base point while True: bits = bbs.genbits(size) y = 0 for bit in bits: y = (y<<1) | bit u = int((1 - y**2) * gmpy2.invert(1 - d*y**2, p)) % p if gmpy2.legendre(u, p) == -1: continue x = gmpy2.powmod(u, (p+1) // 4, p) (x,y) = subroutines.add_on_edwards(x, y, x, y, d, p) (x,y) = subroutines.add_on_edwards(x, y, x, y, d, p) if (x, y) == (0, 1): continue assert((x**2 + y**2) % p == (1 + d*x**2*y**2) % p) break # Print some informations utils.colprint("Number of the successful candidate:", str(candidate_nbr)) utils.colprint("Edwards elliptic curve parameter d is:", str(d)) utils.colprint("Number of points:", str(cardinality)) utils.colprint("Number of points on the twist:", str(cardinality_twist)) utils.colprint("Embedding degree of the curve:", "%d"%embedding_degree) utils.colprint("Embedding degree of the twist:", "%d"%embedding_degree_twist) utils.colprint("Discriminant:", "%d"%D) utils.colprint("Trace:", "%d"%trace) utils.colprint("Base point coordinates:", "(%d, %d)"%(x, y)) # Save p, d, x, y, etc. to the output_file print("Saving the parameters to %s"%output_file) bbs_s = bbs.s with open(output_file, "w") as f: json.dump({"p": int(p), "bbs_p": int(bbs_p), "bbs_q": int(bbs_q), "bbs_s": int(bbs_s), "candidate_nbr": int(candidate_nbr), "d": int(d), "cardinality": cardinality, "cardinality_twist": cardinality_twist, "embedding_degree": embedding_degree, "embedding_degree_twist": embedding_degree_twist, "discriminant": D, "trace": trace, "base_point_x": x, "base_point_y": y}, f, sort_keys=True)
def search_data(self, time0, time1, devices, components, csv=None): """ Retrieve data for a list of devices and components in a given time period. Args: time0 (int): beginning time time1 (int): ending time (None = current timestamp) devices (list of strings): list of devices IDs to return data for components (list of strings): list of component IDs to return data for csv (bool): return results in JSON or CSV format Returns: JSON or CSV message { "from": 1234567890, "to": 1234567890, "maxPoints": 100, "series": [ { "deviceId": "D1", "deviceName": "D1", "componentId": "e3a48caa-e4c5-46bb-951e-8f9d0a4be516", "componentName": "temp", "componentType": "temperature.v1.0", "points": [ {"ts":9874569871, "value":25}, {"ts":9874569899, "value":24} ] }, { "deviceId": "D2", "deviceName": "D2", "componentId": "76a95112-2159-4ee6-8e91-6a69b9c51edc", "componentName": "Humidity 1", "componentType": "humidity.v1.0", "points": [ {"ts":9874569871,"value":"55"}, {"ts":9874569899,"value":"65"} ] } ] } """ url = "{0}/accounts/{1}/data/search".format(globals.base_url, self.id) if csv: url = url + "?output=csv" payload = { "from": time0, "targetFilter": { "deviceList": devices }, "metrics": [] } if time1: payload["to"] = time1 for c in components: payload["metrics"].append({"id": c, "op": "none"}) payload["targetFilter"]["deviceList"] = devices data = json.dumps(payload) resp = requests.post(url, data=data, headers=get_auth_headers(self.client.user_token), proxies=self.client.proxies, verify=globals.g_verify) check(resp, 200) if csv: return resp.text else: js = resp.json() return js["series"]
def test(filename='data.bin'): t0 = time.time() import CNN_BP_Conv_V0 as cnn print '[INFO] loading data : %s' %(filename) with open(filename) as inf: para = cPickle.load(inf) fb = cPickle.load(inf) fw = cPickle.load(inf) fx = cPickle.load(inf) fz = cPickle.load(inf) fz_grad = cPickle.load(inf) fx_grad = cPickle.load(inf) fw_grad = cPickle.load(inf) ni,no,row,col,k,batch_size = para x_offset = 0 x_size_iter = row*col*batch_size*type_size x_size = ni*row*col*batch_size*type_size w_cnt_iter = no*k*k z_grad_offset = x_size z_grad_size = no*(row-k+1)*(col-k+1)*batch_size*type_size x_grad_old_offset = z_grad_offset+z_grad_size x_grad_old_size_iter = row*col*batch_size*type_size x_grad_old_size = ni*row*col*batch_size*type_size x_grad_old_cnt = ni*row*col*batch_size x_grad_offset = x_grad_old_offset x_grad_size_iter = x_grad_old_size_iter x_grad_size = x_grad_old_size print '[INFO] Done: load data' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] running writeLMem' cnn.CNN_BP_Conv_V0_writeLMem( param_offset = z_grad_offset, param_size = z_grad_size, instream_cpu_to_lmem_at_cpu = fz_grad ) print '[INFO] running writeLMem' cnn.CNN_BP_Conv_V0_writeLMem( param_offset = x_offset, param_size = x_size, instream_cpu_to_lmem_at_cpu = fx ) print '[INFO] running writeLMem' cnn.CNN_BP_Conv_V0_writeLMem( param_offset = x_grad_old_offset, param_size = x_grad_old_size, instream_cpu_to_lmem_at_cpu = [0.0]*x_grad_old_cnt ) print '[INFO] Done: writeLMem' print '[INFO] time used = %f' %(time.time()-t0) res_w_grad = [] for st in xrange(ni): print '[INFO] running Conv ',st tmp = cnn.CNN_BP_Conv_V0( param_no = no, param_x_grad_offset = x_grad_offset+x_grad_size_iter*st, param_x_grad_old_offset = x_grad_old_offset+x_grad_old_size_iter*st, param_x_offset = x_offset+x_size_iter*st, param_z_grad_offset = z_grad_offset, instream_w = fw[st*w_cnt_iter:(st+1)*w_cnt_iter] ) #res_w_grad += [sum(tmp[i*batch_size:(i+1)*batch_size]) for i in xrange(no*k*k)] res_w_grad += tmp print '[INFO] Done: Conv' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] running readLMem' res_x_grad = cnn.CNN_BP_Conv_V0_readLMem( param_offset = x_grad_offset, param_size = x_grad_size ) print '[INFO] Done: readLMem' print '[INFO] time used = %f' %(time.time()-t0) print '[INFO] checking' ret1 = utils.check('x_grad',1e-12,res_x_grad,fx_grad) ret2 = utils.check('w_grad',1e-12,res_w_grad,fw_grad) print '[INFO] Done: check' return ret1 and ret2
def train_model(model, criterion, optimizer, scheduler, num_epochs=25): since = time.time() best_acc = 0.0 print_step = 3 classes2idx = image_datasets['train'].classes2idx label_map = {v: k for k, v in classes2idx.iteritems()} for epoch in range(num_epochs): print('Epoch {}/{}'.format(epoch+1, num_epochs)) print('-' * 20) # Each epoch has a training and validation phase for phase in ['train', 'valid']: if phase == 'train': scheduler.step() model.train() # Set model to training mode else: model.eval() # Set model to evaluate mode running_loss = 0.0 running_corrects = 0 # Iterate over data. for inputs, labels, _ in dataloaders[phase]: if use_gpu: inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda()) else: inputs, labels = Variable(inputs), Variable(labels) # zero the parameter gradients optimizer.zero_grad() # forward # track history if only in train with torch.set_grad_enabled(phase == 'train'): outputs = model(inputs) _, preds = torch.max(outputs, 1) loss = criterion(outputs, labels) # backward + optimize only if in training phase if phase == 'train': loss.backward() optimizer.step() # statistics running_loss += loss.item() * inputs.size(0) running_corrects += torch.sum(preds == labels.data) epoch_loss = running_loss / dataset_sizes[phase] epoch_acc = running_corrects.double() / dataset_sizes[phase] print('{}: Loss: {:.4f} Acc: {:.4f}'.format( phase, epoch_loss, epoch_acc)) if epoch % print_step == 0: check_training(model, dataloaders[phase], label_map, use_gpu) if phase == 'valid' and epoch_acc > best_acc: old_model = './checkpoint/resnet101_exp_{:.3f}.t7'.format(best_acc) if os.path.exists(old_model): os.remove(old_model) best_acc = epoch_acc print('Saving model...') state = { 'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'acc': best_acc, } torch.save(state, './checkpoint/resnet101_exp_{:.3f}.t7'.format(best_acc)) print('Saving model completed..') torch.cuda.empty_cache() time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) print('Best val Acc: {:4f}'.format(best_acc)) precision, recall, error_cls, _ = check(model, dataloaders['train'], label_map, use_gpu) print_precision_recall(label_map, precision, recall, 'train') move_error('error', label_map, precision, recall, error_cls, 'train') precision, recall, error_cls, _ = check(model, dataloaders['valid'], label_map, use_gpu) print_precision_recall(label_map, precision, recall, 'valid') move_error('error', label_map, precision, recall, error_cls, 'valid')
def _input_check(self): """ Check all parameter values for correctness """ ######################## # wtseq input checks # ######################## # check if wtseq is of type string check(isinstance(self.wtseq, str), 'type(wtseq) = %s; must be a string ' % type(self.wtseq)) # check if empty wtseq is passed check(len(self.wtseq) > 0, "wtseq length cannot be 0") # Check to ensure the wtseq uses the correct bases according to dicttype # unique characters in the wtseq parameter as a list unique_base_list = list(set(self.wtseq)) # if more than 4 unique bases detected and dicttype is not protein if (len(unique_base_list) > 4 and self.dicttype != 'protein'): print( ' Warning, more than 4 unique bases detected for dicttype %s did you mean to enter protein for dicttype? ' % self.dicttype) # if 'U' base detected and dicttype is not 'rna' if ('U' in unique_base_list and self.dicttype != 'rna'): print( ' Warning, U bases detected for dicttype %s did you mean to enter rna for dicttype? ' % self.dicttype) lin_seq_dict, lin_inv_dict = utils.choose_dict(self.dicttype, modeltype='MAT') check( set(self.wtseq).issubset(lin_seq_dict), 'wtseq can only contain bases in ' + str(lin_seq_dict.keys())) ########################## # mutrate input checks # ########################## # check if mutrate is of type float check(isinstance(self.mutrate, float), 'type(mutrate) = %s; must be a float ' % type(self.mutrate)) # ensure mutrate is in the correct range check( self.mutrate > 0 and self.mutrate <= 1, 'mutrate = %d; must be %d <= mutrate <= %d.' % (self.mutrate, 0, 1)) ######################### # numseq input checks # ######################### # check if numseq is valid check(isinstance(self.numseq, int), 'type(numseq) = %s; must be a int ' % type(self.numseq)) # check if numseq is positive check(self.numseq > 0, 'numseq = %d must be a positive int ' % self.numseq) ########################### # dicttype input checks # ########################### # check if dicttype is of type string check(isinstance(self.dicttype, str), 'type(dicttype) = %s; must be a string ' % type(self.dicttype)) # check if len(dicttype) > 0 check( len(self.dicttype) > 0, " length of dicttype must be greater than 0, length(dicttype): %d" % len(self.dicttype)) ########################### # probarr input checks # ########################### # check if probarr is an ndarray if self.probarr is not None: check( isinstance(self.probarr, np.ndarray), 'type(probarr) = %s; must be an np.ndarray ' % type(self.probarr)) ####################### # tags input checks # ####################### # *** NOTE ***: an additional check is made on tags in the constructor if tags = True # check if tags is of type bool. check(isinstance(self.tags, bool), 'type(tags) = %s; must be an boolean ' % type(self.tags)) ############################# # tag_length input checks # ############################# # check if tag_length is of type int check(isinstance(self.tag_length, int), 'type(tag_length) = %s; must be an int ' % type(self.tag_length)) # check if tag_length is of positive check(self.tag_length > 0, 'tag_length = %d must be a positive int ' % self.tag_length) # /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/
async def game_show(self, ctx, replay=None): client = self.bot if not replay: rounds = 3 difficulties = ["easy", "median", "hard"] await ctx.send("So who is playing?") try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: return await ctx.send("No one? Got it.") players = msg.content.split(" ") confirmed_players = [] confirmed_players.append(self.GameShow(ctx.author)) for player in players: try: member = await msg.guild.fetch_member( int( player.replace("<", "").replace(">", "").replace( "@", "").replace("!", ""))) except discord.errors.NotFound: continue except discord.errors.HTTPException: continue except ValueError: continue # for cplayer in confirmed_players: # if member == cplayer.member: # yield cplayer result = [ cplayer for cplayer in confirmed_players if member == cplayer.member ] if result: continue await ctx.send("Do you wish to play {} ?".format( member.mention)) try: msg = await client.wait_for('message', check=utils.check(member), timeout=60) except asyncio.TimeoutError: await ctx.send("No response? Fine then.") continue if msg.content.lower() == "yes": await ctx.send("Welcome aboard") confirmed_players.append(self.GameShow(member)) else: await ctx.send("Someone is chicken") await ctx.send( "What difficulty do you want to pick?\nEasy\nMedium\nHard\nAuto\nRandom" ) try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: await ctx.send("No response? Then we will do random") msg = None if msg: if msg.content.lower() in difficulties: difficulty = msg.content.lower() elif msg.content.lower() == "auto": await ctx.send( "auto is not supported yet. Good luck on random difficulty" ) difficulty = "" else: difficulty = "" else: difficulty = "" await ctx.send("How many rounds?") try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: await ctx.send("No response? Then we will do " + str(rounds) + " rounds") if utils.is_int(msg.content): rounds = int(msg.content) if (rounds * len(confirmed_players)) > 50: await ctx.send( "Too many rounds given a max of 50 questions will be given." ) rounds = 50 // len(confirmed_players) else: rounds = replay[0] difficulty = replay[1] confirmed_players = replay[2] await ctx.send("That appears to be everyone but lets list them here") names = "" for player in confirmed_players: names += player.member.mention names += "\n" await ctx.send( names + "\nMay the odds be ever in your favor\nto quit say the following\n**I wish to quit**" ) q_number = rounds * len(confirmed_players) url = "https://opentdb.com/api.php?amount=" + \ str(q_number) + "&difficulty=" + difficulty try: api_QA = requests.get(url).json() except requests.exceptions.RequestException as e: return await ctx.send("I have a bug. Tell my creator\n", e) j = 0 quitting = False while j < q_number and not quitting: for player in confirmed_players: question = "" if player.quitting: j += 1 continue try: if api_QA["results"][j]["type"] == "boolean": question += "True or False?\n" question += utils.decodeHTMLSymbols( api_QA["results"][j]["question"]) if api_QA["results"][j]["type"] == "multiple": choices = [] choices.append( utils.decodeHTMLSymbols( api_QA["results"][j]["correct_answer"])) for ans in api_QA["results"][j]["incorrect_answers"]: choices.append(utils.decodeHTMLSymbols(ans)) question += "\nChoices:\n" while len(choices) > 1: i = random.randint(0, len(choices) - 1) question += (choices[i] + "\n") del choices[i] question += choices[0] answer = utils.decodeHTMLSymbols( api_QA["results"][j]["correct_answer"]) j += 1 except KeyError as e: return await ctx.send( "I have a bug in my API usage. Please open an issue with my creator and tell him\n", e) await ctx.send(player.member.mention + "\n" + question) try: msg = await client.wait_for('message', check=utils.check( player.member), timeout=30) except asyncio.TimeoutError: msg = "No reply" if msg == "No reply": await ctx.send("The correct answer was " + answer) elif msg.content.lower() == answer.lower(): await msg.add_reaction('✅') await ctx.send("correct!") player.score += 1 elif msg.content.lower() == "i wish to quit": await ctx.send("Do you wish to quit " + (player.member.nick if player.member. nick != None else player.member.name) + "?") try: msg = await client.wait_for('message', check=utils.check( player.member), timeout=30) except asyncio.TimeoutError: pass if msg.content.lower() == "yes": player.quitting = True await ctx.send("Alright quitter") else: await msg.add_reaction('❎') await ctx.send("Wrong. The correct answer was " + answer) if quitting: break await asyncio.sleep(2) await ctx.send("Thanks for playing here are the final results") final_results = "" for player in confirmed_players: if player.quitting: final_results += "~~" + \ (player.member.nick if player.member.nick != None else player.member.name) + "'s~~ Quitter Score: " + \ str(player.score) + "\n" else: final_results += (player.member.nick if player.member.nick != None else player.member.name) + \ "'s Score: " + str(player.score) + "\n" await ctx.send(final_results) await ctx.send("Wanna play again?") try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: return await ctx.send("No? Got it.") if msg.content.lower() == "yes" or msg.content.lower() == "y": for player in confirmed_players: player.score = 0 await self.game_show(ctx, (rounds, difficulty, confirmed_players))
def runit(self, payload, file_pointer): test_data = json.load(file_pointer, object_hook=utils.byteify) config_dict = configobj.ConfigObj(test_data['config']) testruns = test_data['testruns'] cdict = config_dict['MQTTSubscribeService'] if not 'message_callback' in config_dict['MQTTSubscribeService']: config_dict['MQTTSubscribeService']['message_callback'] = {} config_dict['MQTTSubscribeService']['message_callback'][ 'type'] = payload driver = MQTTSubscribeDriver(**cdict) host = 'localhost' port = 1883 keepalive = 60 userdata = {'topics': [], 'connected_flag': False} client = mqtt.Client(userdata=userdata) client.on_connect = utils.on_connect client.connect(host, port, keepalive) client.loop_start() max_connect_wait = 1 # ToDo - configure i = 1 while not userdata['connected_flag']: if i > max_connect_wait: self.fail("Timed out waiting for connections.") time.sleep(1) i += 1 userdata2 = { 'topics': cdict['topics'].sections, 'connected_flag': False, 'msg': False, 'max_msg_wait': 1 # ToDo - configure } client2 = mqtt.Client(userdata=userdata2) client2.on_connect = utils.on_connect client2.on_message = utils.on_message client2.connect(host, port, keepalive) client2.loop_start() max_connect2_wait = 1 # ToDo - configure i = 1 while not userdata2['connected_flag']: #print("waiting to connect") if i > max_connect2_wait: self.fail("Timed out waiting for connection 2.") time.sleep(1) i += 1 max_waits = 10 for testrun in testruns: for topics in testrun['messages']: for topic in topics: topic_info = topics[topic] msg_count = utils.send_msg(utils.send_mqtt_msg, payload, client.publish, topic, topic_info, userdata2, self) wait_count = utils.wait_on_queue(driver, msg_count, max_waits, 1) # If queue not filled, fail now # otherwise will end up in 'infinite' loop in genLoopPackets if wait_count >= max_waits: self.fail("Could not fill queue.") records = [] gen = driver.genLoopPackets() results = testrun['results'] result = {} found = False for result in results: if 'driver' in result['test']: if payload in result['payloads']: found = True break self.assertTrue(found, "No results for %s" % payload) i = 0 # ToDo not great, but no way to know if more records # could possibly check the queues.. # Todo this needs to be fixed for new debug code while i < len(result['records']): data = next(gen, None) records.append(data) i += 1 utils.check(self, payload, records, result['records']) driver.closePort() client.disconnect() client2.disconnect()
def process(actor, rst): g = Game.getgame() usage = getattr(initiator, 'card_usage', 'none') try: check(rst) skills, rawcards, players, params = rst [check(not c.detached) for c in rawcards] [check(actor.has_skill(s)) for s in skills] # has_skill may be hooked if skills: cards = [skill_wrap(actor, skills, rawcards, params)] usage = cards[0].usage if usage == 'launch' else usage else: cards = rawcards usage = 'launch' if categories: if len(cards) == 1 and cards[0].is_card(VirtualCard): def walk(c): if not c.is_card(VirtualCard): return if getattr(c, 'no_reveal', False): return g.players.reveal(c.associated_cards) for c1 in c.associated_cards: walk(c1) walk(cards[0]) check(skill_check(cards[0])) else: if not getattr(initiator, 'no_reveal', False): g.players.reveal(cards) check(initiator.cond(cards)) assert not (usage == 'none' and rawcards ) # should not pass check else: cards = [] if candidates: players, valid = initiator.choose_player_target(players) check(valid) ask_for_action_verify = getattr(initiator, 'ask_for_action_verify', None) if ask_for_action_verify: check(ask_for_action_verify(actor, cards, players)) return cards, players, params except CheckFailed: return None
def user_choose_cards_logic(input, act, target, categories=None): from utils import check, CheckFailed g = Game.getgame() try: check_type([[int, Ellipsis]] * 3, input) sid_list, cid_list, pid_list = input cards = g.deck.lookupcards(cid_list) check(len(cards) == len(cid_list)) # Invalid id cs = set(cards) check(len(cs) == len(cid_list)) # repeated ids if not categories: categories = [target.cards, target.showncards] if sid_list: check(all(cat.owner is target for cat in categories)) check(all(c.resides_in.owner is target for c in cards)) # Cards belong to target? # associated_cards will be revealed here c = skill_wrap(target, sid_list, cards) check(c) cards = [c] else: check(all(c.resides_in in categories for c in cards)) # Cards in desired categories? if not getattr(act, 'no_reveal', False): g.players.exclude(target).reveal(cards) check(act.cond(cards)) log.debug('user_choose_cards: %s %s %s', repr(act), target.__class__.__name__, repr(cards)) return cards except CheckFailed as e: log.debug('user_choose_cards FAILED: %s %s', repr(act), target.__class__.__name__) return None
def _input_checks(self): """ check input parameters for correctness """ # dataset if self.df is None: raise ControlledError( " The Learn Model class requires pandas dataframe as input dataframe. Entered df was 'None'." ) elif self.df is not None: check(isinstance(self.df, pd.DataFrame), 'type(df) = %s; must be a pandas dataframe ' % type(self.df)) # validate dataset check( pd.DataFrame.equals(self.df, qc.validate_dataset(self.df)), " Input dataframe failed quality control, \ please ensure input dataset has the correct format of an mpathic dataframe " ) # check lm is of type string check(isinstance(self.lm, str), "type(lm) = %s must be a string " % type(self.lm)) # check lm value is valid valid_lm_values = ['ER', 'LS', 'IM', 'PR'] check(self.lm in valid_lm_values, 'lm = %s; must be in %s' % (self.lm, valid_lm_values)) # check that model type is of type string check(isinstance(self.modeltype, str), "type(modeltype) = %s must be a string " % type(self.modeltype)) # check that modeltype value is valid valid_modeltype_values = ['MAT', 'NBR'] check( self.modeltype in valid_modeltype_values, 'modeltype = %s; must be in %s' % (self.modeltype, valid_modeltype_values)) # validate LS_mean_std LS_means_std_valid_col_order = ['bin', 'mean', 'std'] if self.LS_means_std is not None: check( pd.DataFrame.equals(self.LS_means_std, qc.validate_meanstd(self.LS_means_std)), " LS_means_std failed quality control, \ please ensure input dataset has the correct format for LS_means_std: %s" % LS_means_std_valid_col_order) if self.db is not None: # check that db is a string check(isinstance(self.db, str), "type(db) = %s must be a string " % type(self.db)) # check that iteration is an integer check( isinstance(self.iteration, int), 'type(iteration) = %s; must be of type int ' % type(self.iteration)) # check that burnin is an integer check(isinstance(self.burnin, int), 'type(burnin) = %s; must be of type int ' % type(self.burnin)) # check that thin is an integer check(isinstance(self.thin, int), 'type(thin) = %s; must be of type int ' % type(self.thin)) # check that runnum is an integer check(isinstance(self.runnum, int), 'type(runnum) = %s; must be of type int ' % type(self.runnum)) # check that initialize is a string and it's value is valid check( isinstance(self.initialize, str), "type(initialize) = %s must be a string " % type(self.initialize)) valid_initialize_values = ['rand', 'LS', 'PR'] check( self.initialize in valid_initialize_values, 'initialize = %s; must be in %s' % (self.initialize, valid_initialize_values)) # check that start is an integer check(isinstance(self.start, int), 'type(start) = %s; must be of type int ' % type(self.start)) check(self.start >= 0, "start = %d must be a positive integer " % self.start) if self.end is not None: check(isinstance(self.end, int), 'type(end) = %s; must be of type int ' % type(self.end)) # check that foreground is an integer check( isinstance(self.foreground, int), 'type(foreground) = %s; must be of type int ' % type(self.foreground)) # check that background is an integer check( isinstance(self.background, int), 'type(background) = %s; must be of type int ' % type(self.background)) # check that alpha is a float check( isinstance(self.alpha, float), 'type(alpha) = %s; must be of type float ' % type(self.background)) # check that pseudocounts is an integer check( isinstance(self.pseudocounts, int), 'type(pseudocounts) = %s; must be of type int ' % type(self.pseudocounts)) # check that verbose is a boolean check(isinstance(self.verbose, bool), 'type(verbose) = %s; must be of type bool ' % type(self.verbose)) if self.tm is not None: # check that tm is an integer check(isinstance(self.tm, int), 'type(tm) = %s; must be of type int ' % type(self.tm))
def __init__(self, wtseq="ACGACGA", mutrate=0.10, numseq=10000, dicttype='dna', probarr=None, tags=False, tag_length=10): # setting attributes to parameters. This could be modified. self.wtseq = wtseq self.mutrate = mutrate self.numseq = numseq self.dicttype = dicttype self.probarr = probarr self.tags = tags self.tag_length = tag_length # attribute that gets populated after running the constructor self.output_df = None # Validate inputs: self._input_check() # generate sequence dictionary seq_dict, inv_dict = utils.choose_dict(dicttype) if isinstance(probarr, np.ndarray): L = probarr.shape[1] #Generate bases according to provided probability matrix letarr = np.zeros([numseq, L]) for z in range(L): letarr[:, z] = np.random.choice(range(len(seq_dict)), numseq, p=probarr[:, z]) else: parr = [] wtseq = wtseq.upper() L = len(wtseq) letarr = np.zeros([numseq, L]) #find wtseq array wtarr = self.seq2arr(wtseq, seq_dict) mrate = mutrate / (len(seq_dict) - 1) # prob of non wildtype # Generate sequences by mutating away from wildtype '''probabilities away from wildtype (0 = stays the same, a 3 for example means a C becomes an A, a 1 means C-> G)''' parr = np.array([1 - (len(seq_dict) - 1) * mrate] + [mrate for i in range(len(seq_dict) - 1)]) # Generate random movements from wtseq letarr = np.random.choice(range(len(seq_dict)), [numseq, len(wtseq)], p=parr) #Find sequences letarr = np.mod(letarr + wtarr, len(seq_dict)) seqs = [] # Convert Back to letters for i in range(numseq): seqs.append(self.arr2seq(letarr[i, :], inv_dict)) seq_col = qc.seqtype_to_seqcolname_dict[dicttype] seqs_df = pd.DataFrame(seqs, columns=[seq_col]) # If simulating tags, each generated seq gets a unique tag if tags: tag_seq_dict, tag_inv_dict = utils.choose_dict('dna') tag_alphabet_list = tag_seq_dict.keys() check( len(tag_alphabet_list)**tag_length > 2 * numseq, 'tag_length=%d is too short for num_tags_needed=%d' % (tag_length, numseq)) # Generate a unique tag for each unique sequence tag_set = set([]) while len(tag_set) < numseq: num_tags_left = numseq - len(tag_set) new_tags = [''.join(choice(tag_alphabet_list,size=tag_length)) \ for i in range(num_tags_left)] tag_set = tag_set.union(new_tags) df = seqs_df.copy() df.loc[:, 'ct'] = 1 df.loc[:, 'tag'] = list(tag_set) # If not simulating tags, list only unique seqs w/ corresponding counts else: seqs_counts = seqs_df[seq_col].value_counts() df = seqs_counts.reset_index() df.columns = [seq_col, 'ct'] # Convert into valid dataset dataframe and return self.output_df = qc.validate_dataset(df, fix=True)
def runit(self, payload, file_pointer): test_data = json.load(file_pointer, object_hook=utils.byteify) config_dict = configobj.ConfigObj(test_data['config']) testruns = test_data['testruns'] cdict = config_dict['MQTTSubscribeService'] if not 'message_callback' in config_dict['MQTTSubscribeService']: config_dict['MQTTSubscribeService']['message_callback'] = {} config_dict['MQTTSubscribeService']['message_callback'][ 'type'] = payload min_config_dict = { 'Station': { 'altitude': [0, 'foot'], 'latitude': 0, 'station_type': 'Simulator', 'longitude': 0 }, 'Simulator': { 'driver': 'weewx.drivers.simulator', }, 'Engine': { 'Services': {} } } engine = StdEngine(min_config_dict) service = MQTTSubscribeService(engine, config_dict) host = 'localhost' port = 1883 keepalive = 60 userdata = { 'topics': [], 'connected_flag': False, } client = mqtt.Client(userdata=userdata) client.on_connect = utils.on_connect client.connect(host, port, keepalive) client.loop_start() max_connect_wait = 1 # ToDo - configure i = 1 while not userdata['connected_flag']: if i > max_connect_wait: self.fail("Timed out waiting for connections.") time.sleep(1) i += 1 userdata2 = { 'topics': cdict['topics'].sections, 'connected_flag': False, 'msg': False, 'max_msg_wait': 1 #ToDo - configure } client2 = mqtt.Client(userdata=userdata2) client2.on_connect = utils.on_connect client2.on_message = utils.on_message client2.connect(host, port, keepalive) client2.loop_start() max_connect2_wait = 1 # ToDo - configure i = 1 while not userdata2['connected_flag']: if i > max_connect2_wait: self.fail("Timed out waiting for connection 2.") time.sleep(1) i += 1 max_waits = 10 for testrun in testruns: for topics in testrun['messages']: for topic in topics: topic_info = topics[topic] msg_count = utils.send_msg(utils.send_mqtt_msg, payload, client.publish, topic, topic_info, userdata2, self) utils.wait_on_queue(service, msg_count, max_waits, 1) results = testrun['results'] result = {} found = False for result in results: if 'service' in result['test']: if payload in result['payloads']: found = True break self.assertTrue(found, "No results for %s" % payload) record = {} units = result['units'] interval = 300 current_time = int(time.time() + 0.5) end_period_ts = (int(current_time / interval) + 1) * interval record['dateTime'] = end_period_ts record['usUnits'] = units new_loop_packet_event = weewx.Event(weewx.NEW_LOOP_PACKET, packet=record) service.new_loop_packet(new_loop_packet_event) records = [record] utils.check(self, payload, records, result['records']) service.shutDown() client.disconnect() client2.disconnect()
from utils import control, mirror_control, manager_stat, check def menu(): try: while 1: mode = input("1.新增镜像\n2.镜像管理\n3.Manager状态\n") if mode: mode = int(mode) else: break if mode == 1: name = input("输入mirror名称(不能重复):") mirror_control(name).add() elif mode == 2: control() elif mode == 3: manager_stat() else: return 0 except KeyboardInterrupt: return 0 if __name__ == "__main__": if check(): menu()
def _message(self, type, msg): not_registered_text = ( u'文文不认识你,不会帮你发新闻的哦。\n' u'回复“文文求交朋友 <uid> <密码>”,不要带引号,文文就会认识你啦。\n' u'uid可以在登陆论坛或者游戏后知道,密码就是论坛的密码。\n' u'比如这样:\n' u'文文求交朋友 23333 wodemima23333\n' u'\n' u'文文帮你发新闻是要收费的,一发5节操。不过连续发的话收费会翻倍哦。' ) registered_text = ( u'哎呀呀呀,原来你是%s啊,文文认识你了。\n' u'只要你的节操还在,文文就会帮你发新闻~\n' ) help_text = ( u'所有在QQ群里以“`”或者“\'”开头的聊天都会被当做发新闻的请求~\n' u'比如这样:\n' u'`文文最是清正廉直!\n' u'\n' u'文文帮你发新闻是要收费的,一发5节操。不过连续发的话收费会翻倍哦。' ) fail_text = ( u'文文调查了一下你,发现你的密码填的不对!\n' u'快说你到底是谁!' ) qq = self.uin2qq(msg['from_uin']) if type == 'buddy': send = lambda t: self.send_buddy_message(msg['from_uin'], t) elif type == 'sess': send = lambda t: self.send_sess_message(msg['id'], msg['from_uin'], t) uid = dao.get_binding(qq) if not uid: content = self._plaintext(msg['content']).strip() req = content.split(None, 2) req = [i.strip() for i in req] try: check(len(req) == 3) check(req[0] == u'文文求交朋友') check(req[1].isdigit()) except CheckFailed: send(not_registered_text) return uid = int(req[1]) pwd = req[2] with member_client_pool() as cli: member = cli.validate_by_uid(uid, pwd) if not member: send(fail_text) return dao.set_binding(qq, uid) send(registered_text % member['username'] + help_text) return send(help_text)
async def tic_tac_toe(self, ctx): client = self.bot await ctx.send("This is an beta test") await ctx.send("Who do you want to play against?") difficulty = None try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: await ctx.send("Allow me to play the game I select myself") msg = None if msg: member = msg.content.split(" ")[0] try: player = await msg.guild.fetch_member( int( member.replace("<", "").replace(">", "").replace( "@", "").replace("!", ""))) if player.id != client.user.id: difficulty = "player" except discord.errors.NotFound: await ctx.send("player not found. Fine I will do it myself") player = client.user except discord.errors.HTTPException: await ctx.send("player not found. Fine I will do it myself") player = client.user except ValueError: await ctx.send("player not found. Fine I will do it myself") player = client.user if not difficulty: await ctx.send("What difficulty?\nEasy\nHard") difficulty = "easy" try: msg = await client.wait_for('message', check=utils.check(ctx.author), timeout=60) except asyncio.TimeoutError: await ctx.send("You had one job.") return await ctx.send( "https://tenor.com/view/south-park-fractured-difficulty-gif-10182175" ) if msg.content.lower() == "hard" or msg.content.lower() == "h": difficulty = "hard" message = await ctx.send("Loading...") await message.add_reaction("1️⃣") await message.add_reaction("2️⃣") await message.add_reaction("3️⃣") await message.add_reaction("4️⃣") await message.add_reaction("5️⃣") await message.add_reaction("6️⃣") await message.add_reaction("7️⃣") await message.add_reaction("8️⃣") await message.add_reaction("9️⃣") board = "1️⃣2️⃣3️⃣\n4️⃣5️⃣6️⃣\n7️⃣8️⃣9️⃣" vboard = ["c", "c", "c", "c", "c", "c", "c", "c", "c"] current = [ '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣' ] await message.edit(content=board) ticker = await ctx.send(ctx.author.mention + "'s move") def check_reaction(user): def inner_check(reaction, author): return user == author and str(reaction.emoji) in current return inner_check def check_winner(member): if vboard[0] != "c" and vboard[0] == vboard[1] and vboard[ 1] == vboard[2]: return member.mention + " Wins!" elif vboard[3] != "c" and vboard[3] == vboard[4] and vboard[ 4] == vboard[5]: return member.mention + " Wins!" elif vboard[6] != "c" and vboard[6] == vboard[7] and vboard[ 7] == vboard[8]: return member.mention + " Wins!" elif vboard[0] != "c" and vboard[0] == vboard[3] and vboard[ 3] == vboard[6]: return member.mention + " Wins!" elif vboard[1] != "c" and vboard[1] == vboard[4] and vboard[ 4] == vboard[7]: return member.mention + " Wins!" elif vboard[2] != "c" and vboard[2] == vboard[5] and vboard[ 5] == vboard[8]: return member.mention + " Wins!" elif vboard[0] != "c" and vboard[0] == vboard[4] and vboard[ 4] == vboard[8]: return member.mention + " Wins!" elif vboard[2] != "c" and vboard[2] == vboard[4] and vboard[ 4] == vboard[6]: return member.mention + " Wins!" else: return None def check_bot_win(): if vboard[0] != "c" and vboard[0] == vboard[1] and vboard[ 1] == vboard[2]: return "I win!" elif vboard[3] != "c" and vboard[3] == vboard[4] and vboard[ 4] == vboard[5]: return "I win!" elif vboard[6] != "c" and vboard[6] == vboard[7] and vboard[ 7] == vboard[8]: return "I win!" elif vboard[0] != "c" and vboard[0] == vboard[3] and vboard[ 3] == vboard[6]: return "I win!" elif vboard[1] != "c" and vboard[1] == vboard[4] and vboard[ 4] == vboard[7]: return "I win!" elif vboard[2] != "c" and vboard[2] == vboard[5] and vboard[ 5] == vboard[8]: return "I win!" elif vboard[0] != "c" and vboard[0] == vboard[4] and vboard[ 4] == vboard[8]: return "I win!" elif vboard[2] != "c" and vboard[2] == vboard[4] and vboard[ 4] == vboard[6]: return "I win!" else: return None while len(current) != 0: await ticker.edit(content=ctx.author.mention + "'s move") try: # returns a tuple with a reaction and member object reaction, member = await client.wait_for('reaction_add', timeout=30, check=check_reaction( ctx.author)) except asyncio.TimeoutError: return await ticker.edit(content="The game timed out") await ticker.edit(content="Nice choice!") if not str(reaction.emoji) in current: return await ctx.send("wait that was illegal") current.remove(reaction.emoji) board = board.replace(reaction.emoji, "❌") num = int(reaction.emoji[0]) vboard[num - 1] = "x" await message.edit(content=board) win = check_winner(member) if win: return await ticker.edit(content=win) if len(current) == 0: return await ticker.edit(content="This game is over") if difficulty == "player": await ticker.edit(content=player.mention + "'s move") else: await ticker.edit(content="My move") if difficulty == "hard": if vboard[4] == "c": choice = current.index("5️⃣") elif vboard[0] == "x" and vboard[0] == vboard[1] and vboard[ 2] == "c": choice = current.index("3️⃣") elif vboard[1] == "x" and vboard[1] == vboard[2] and vboard[ 0] == "c": choice = current.index("1️⃣") elif vboard[2] == "x" and vboard[2] == vboard[0] and vboard[ 1] == "c": choice = current.index("2️⃣") elif vboard[3] == "x" and vboard[3] == vboard[4] and vboard[ 5] == "c": choice = current.index("6️⃣") elif vboard[4] == "x" and vboard[4] == vboard[5] and vboard[ 3] == "c": choice = current.index("4️⃣") elif vboard[5] == "x" and vboard[5] == vboard[3] and vboard[ 4] == "c": choice = current.index("5️⃣") elif vboard[6] == "x" and vboard[6] == vboard[7] and vboard[ 8] == "c": choice = current.index("9️⃣") elif vboard[7] == "x" and vboard[7] == vboard[8] and vboard[ 6] == "c": choice = current.index("7️⃣") elif vboard[8] == "x" and vboard[8] == vboard[6] and vboard[ 7] == "c": choice = current.index("8️⃣") elif vboard[0] == "x" and vboard[0] == vboard[6] and vboard[ 3] == "c": choice = current.index("4️⃣") elif vboard[0] == "x" and vboard[0] == vboard[3] and vboard[ 6] == "c": choice = current.index("7️⃣") elif vboard[3] == "x" and vboard[3] == vboard[6] and vboard[ 0] == "c": choice = current.index("1️⃣") elif vboard[1] == "x" and vboard[1] == vboard[7] and vboard[ 4] == "c": choice = current.index("5️⃣") elif vboard[1] == "x" and vboard[1] == vboard[4] and vboard[ 7] == "c": choice = current.index("8️⃣") elif vboard[4] == "x" and vboard[4] == vboard[7] and vboard[ 1] == "c": choice = current.index("2️⃣") elif vboard[2] == "x" and vboard[2] == vboard[8] and vboard[ 5] == "c": choice = current.index("6️⃣") elif vboard[2] == "x" and vboard[2] == vboard[5] and vboard[ 8] == "c": choice = current.index("9️⃣") elif vboard[5] == "x" and vboard[5] == vboard[8] and vboard[ 2] == "c": choice = current.index("3️⃣") elif vboard[4] == "x" and vboard[4] == vboard[0] and vboard[ 8] == "c": choice = current.index("9️⃣") elif vboard[4] == "x" and vboard[4] == vboard[2] and vboard[ 6] == "c": choice = current.index("7️⃣") elif vboard[4] == "x" and vboard[4] == vboard[8] and vboard[ 0] == "c": choice = current.index("1️⃣") elif vboard[4] == "x" and vboard[4] == vboard[6] and vboard[ 2] == "c": choice = current.index("3️⃣") else: choice = random.randint(0, len(current) - 1) elif difficulty == "easy": choice = random.randint(0, len(current) - 1) if difficulty != "player": board = board.replace(current[choice], "⭕") num = int(current[choice][0]) vboard[num - 1] = "o" current.remove(current[choice]) win = check_bot_win() else: try: reaction, member = await client.wait_for( 'reaction_add', timeout=30, check=check_reaction(player)) except asyncio.TimeoutError: return await ticker.edit(content="The game timed out") await ticker.edit(content="Nice choice!") if not str(reaction.emoji) in current: return await ctx.send("wait that was illegal") current.remove(reaction.emoji) board = board.replace(reaction.emoji, "⭕") num = int(reaction.emoji[0]) vboard[num - 1] = "o" win = check_winner(player) await message.edit(content=board) if win: return await ticker.edit(content=win)
def apply_action(self): g = Game.getgame() target = self.target if target.dead: return False shuffle_here() try: while not target.dead: g.emit_event('action_stage_action', target) input = target.user_input('action_stage_usecard') check_type([[int, Ellipsis]] * 3, input) skill_ids, card_ids, target_list = input if card_ids: cards = g.deck.lookupcards(card_ids) check(cards) check(all(c.resides_in.owner is target for c in cards)) else: cards = [] target_list = [g.player_fromid(i) for i in target_list] from game import AbstractPlayer check(all(isinstance(p, AbstractPlayer) for p in target_list)) # skill selected if skill_ids: card = skill_wrap(target, skill_ids, cards) check(card) else: check(len(cards) == 1) g.players.exclude(target).reveal(cards) card = cards[0] from .cards import HiddenCard assert not card.is_card(HiddenCard) check(card.resides_in in (target.cards, target.showncards)) if not g.process_action(ActionStageLaunchCard(target, target_list, card)): # invalid input log.debug('ActionStage: LaunchCard failed.') break shuffle_here() except CheckFailed as e: pass return True
import sys, os from shutil import move, copyfile from hashlib import md5 from utils import check bin_name = sys.argv[1] files = os.listdir('bin/') version = len(files) + 1 filemd5 = md5(open(bin_name).read()).hexdigest() name = str(version) + '_' + filemd5 copyfile(bin_name, 'bin/' + name) dirname = 'json/' + str(version) os.mkdir(dirname) if version > 1: dirname2 = 'json/' + str(version - 1) os.system('cp ' + dirname2 + '/ok/ ' + dirname + '/ok -r') os.mkdir(dirname + '/exception') os.mkdir(dirname + '/touch_flag') check(name, dirname2 + '/exception', dirname) check(name, dirname2 + '/touch_flag', dirname) else: os.mkdir(dirname + '/ok') os.mkdir(dirname + '/exception') os.mkdir(dirname + '/touch_flag')
from utils import check, Params, Timer, ensure_dir_exists FIELD_NAMES = ['step', 'generators_loss', 'discr_a_loss', 'discr_b_loss', 'total_loss', 'duration'] @click.command() @click.option('--dataset_a', type=click.Path(exists=True, dir_okay=True), help='Path to dataset A') @click.option('--dataset_b', type=click.Path(exists=True, dir_okay=True), help='Path to dataset B') @click.option('--use_cuda/--no_cuda', default=False, show_default=True) @click.option('--checkpoint_path', default='checkpoint', show_default=True, help='Checkpoint path') @click.option('--save_step', default=100, show_default=True, type=click.IntRange(min=1), help='Save every `--save_step` step') @click.option('--test_step', default=100, show_default=True, type=click.IntRange(min=1), help='Test every `--test_step` step') @click.option('--batch_size', default=1, show_default=True, type=click.IntRange(min=1), help='Batch size') @click.option('--image_pool_size', default=50, show_default=True, type=click.IntRange(min=1), help='Image pool size') @click.option('--adam_beta1', default=0.5, show_default=True, type=float, help="Adam optimizer's beta1 param", callback=check(lambda x: 0 <= x <= 1)) @click.option('--adam_beta2', default=0.999, show_default=True, type=float, help="Adam optimizer's beta2 param", callback=check(lambda x: 0 <= x <= 1)) @click.option('--gen_learning_rate', default=2e-4, show_default=True, type=float, help="Learning rate for generators") @click.option('--discr_learning_rate', default=1e-4, show_default=True, type=float, help="Learning rate for discriminators") @click.option('--log_filename', default='log.csv', show_default=True, help='Log filename') @click.option('--debug_path', default='debug', show_default=True, help='Folder to save debug images') def train(**kwargs): params = Params(kwargs) print('Params:') params.pretty_print() print() use_cuda = params.use_cuda if use_cuda: assert torch.cuda.is_available()
#%% ''' 93. tang: debt capacity/firm tangibility Cash holdings + 0.715 × receivables +0.547 × inventory + 0.535 × PPE/total assets ''' Quarter_data[ 'Factor93_tang'] = Quarter_data['money_cap'] + 0.715 * Quarter_data[ 'accounts_receiv'] + 0.547 * Quarter_data[ 'inventories'] + 0.535 * Quarter_data['fix_assets'] / Quarter_data[ 'total_assets'] #%% ''' 94. tb :Tax income to book income -Annual Tax income, calculated from current tax expense divided by maximum federal tax rate, divided by income before extraordinary items Tax income = tax_expense/(maximum_tax_rate*ebit) (Tax income > 1: Too much tax, bad Tax income < 1: Avoid some tax) We replace it with tax_return(by government) sign. I cannot find maximum_tax_rate in wind for different company ''' Quarter_data['Factor94_tb'] = Quarter_data['taxes_payable'] / ( 0.25 * Quarter_data['ebit_x']) # %% Out_df = utils.extrct_fctr(Quarter_data) utils.check(Out_df) Out_df.to_csv(Path('_saved_factors', 'QrtFactor.csv'), index=False) #%%
def control_and_monitor(pre_train_barrier, post_train_barrier, experiments): multiprocessing.log_to_stderr() ctrlC_to_exit = False try: signature_setups = sorted(experiments, key=lambda run: run['0run'])[0] signature_info = utils.canonical_exp_name(signature_setups) all_ips_in_experiments = set() for setups in experiments: for ip_port in setups['addresses']['ps'] + setups['addresses'][ 'worker']: all_ips_in_experiments.add(ip_port.split(':')[0]) if gv.COLLECT_RESULTS_ONLY: write_monitor_logs_to_db(signature_setups, all_ips_in_experiments) return # For simplicity, all concurrent runs must agree to monitor in order to # launch the monitor task. monitor_tasks_under_user = [] has_monitor_logs = False if all([ 'should_vmstat' in setups and setups['should_vmstat'] for setups in experiments ]): monitor_tasks_under_user += [ monitor('vmstat', ip, signature_info) for ip in all_ips_in_experiments ] print '[{n}] controller: {fg}vmstat monitor enabled{fe}'.format( n=utils.now(), fg=gv.Format.GREEN, fe=gv.Format.END) if all([ 'should_ifstat' in setups and setups['should_ifstat'] for setups in experiments ]): monitor_tasks_under_user += [ monitor('ifstat', ip, signature_info, interface=gv.DATA_PLANE_INF) for ip in all_ips_in_experiments ] print '[{n}] controller: {fg}ifstat monitor enabled{fe}'.format( n=utils.now(), fg=gv.Format.GREEN, fe=gv.Format.END) monitor_tasks_under_root = [] if all([ 'should_tcpdump' in setups and setups['should_tcpdump'] for setups in experiments ]): # Start logging 4 minutes after stage begins and lasts for 20 seconds. # Under debug model, tcpdump starts 1 minutes after stage begins. start_sec = int(gv.NEW_STAGE_BEGIN_USEC / 1e6) start_sec += 60 * (1 if gv.DEBUG_LEVEL > 0 else 4) monitor_tasks_under_root += [ monitor('tcpdump', ip, signature_info, interface=gv.DATA_PLANE_INF, use_root=True, time_to_start_tcpdump_epoch_sec=start_sec, duration_tcpdump_sec=40) for ip in all_ips_in_experiments ] print '[{n}] controller: {fg}tcpdump monitor enabled{fe}'.format( n=utils.now(), fg=gv.Format.GREEN, fe=gv.Format.END) tc_leaders_configs = [] for key, group in itertools.groupby( experiments, key=lambda run: [ipp.split(':')[0] for ipp in run['addresses']['ps']]): runs_one_group = list(group) leader_setups = sorted(runs_one_group, key=lambda run: run['0run'])[0] leader_setups_copy = copy.deepcopy(leader_setups) leader_setups_copy['concurrent_ps_configs'] = [{ 'ps_port': run_['addresses']['ps'][0].split(':')[1], 'wk_port': run_['addresses']['worker'][0].split(':')[1], 'model': run_['args']['model'], } for run_ in runs_one_group] tc_leaders_configs.append(leader_setups_copy) print( '[{}] controller: Reset and setup tc command ' 'on {} PS leaders'.format(utils.now(), len(tc_leaders_configs))) # clean up any pre-existing rules for traffic control [remote_tc.reset_dev_remote(ip) for ip in all_ips_in_experiments] tc_tasks_under_root = [ remote_tc.setup_remote_tc(tc_configs) for tc_configs in tc_leaders_configs ] pre_train_barrier.wait() post_train_barrier.wait() except KeyboardInterrupt: ctrlC_to_exit = True print('{}controller: Caught Ctrl-C. Will exit after cleanup {}{}' ).format(gv.Format.RED, info, gv.Format.END) except Exception as e: multiprocessing.get_logger().error(traceback.format_exc()) finally: # remove monitor tasks [utils.kill_by_tasks(monitor_tasks_under_user) for i in range(0, 3)] [ utils.check('dead', n, ip, verbose=gv.DEBUG_LEVEL > 0) for (n, ip) in monitor_tasks_under_user ] # remove remote_tc tasks, which run under root [ utils.kill_by_tasks(tc_tasks_under_root + monitor_tasks_under_root, use_root=True) for i in range(0, 3) ] [ utils.check('dead', n, ip, use_root=True, verbose=gv.DEBUG_LEVEL > 0) for (n, ip) in tc_tasks_under_root + monitor_tasks_under_root ] # reset tc on all hosts print( '{}[{}] controller: after finishing one stage of experiment, ' 'now reset tc on {} {}').format(gv.Format.YELLOW, utils.now(), all_ips_in_experiments, gv.Format.END) [remote_tc.reset_dev_remote(ip) for ip in all_ips_in_experiments] # write monitor logs to db if not ctrlC_to_exit: write_monitor_logs_to_db(signature_setups, all_ips_in_experiments) print('[{}] Exiting controller. Bye!'.format(utils.now()))
def get_user(opts): endpoint = 'users/self/' res = requests.get(base_url + version + endpoint, opts).json() check(res) pprint(res['data'])