def test_2children(self): parent = { 'state': 'state', 'evaluation': (1, 2, 3), 'parent': None, } recParent = Record(**parent) children = [ { 'state': 'state child1', 'evaluation': (2, 3, 4), 'parent': recParent, }, { 'state': 'state child2', 'evaluation': (3, 4, 5), 'parent': recParent, }, ] recParent.opToChild[('x', 0, True)] = Record(**children[0]) recParent.opToChild[('x', 0, False)] = Record(**children[1]) self.assertEqual(len(recParent.opToChild), 2) self.assertEqual(recParent.opToChild['x', 0, True].state, children[0]['state']) self.assertEqual(recParent.opToChild['x', 0, True].nbSteps, 1) self.assertEqual(recParent.opToChild['x', 0, False].nbSteps, 1) self.assertEqual(recParent.opToChild['x', 0, False].parent, recParent)
def collect_data(collect_stats, sql, save_collection_name=None, days_to_pass=None): lcid_bid = meta_indexes.load_lcid_bid() connection = sqlite3.connect(DB_PATH) cursor = connection.cursor() cursor.execute(sql) print('Query executed successfully') index = 0 log_step = 1000 start = datetime.datetime.now() # if save_collection_name != None: # db = connect_to_mongo_database() # record = write_record_to_mongodb(db, save_collection_name, cursor.fetchone()) record = Record(cursor.fetchone(), lcid_bid) # record = Record(cursor.fetchone(), lcid_bid) # first_log_moment = record.read_at while not record.empty: collect_stats(record) index += 1 if index % log_step == 0: print('records processed: {0}'.format(index)) record = Record(cursor.fetchone(), lcid_bid) # record = write_record_to_mongodb(db, save_collection_name, cursor.fetchone()) finish = datetime.datetime.now() delta = finish - start print('Time passed: {0}'.format(delta)) connection.close() return index
def test_parsing_OTHER_ID(record, isGood): if isGood: rec = Record(record) assert (rec.OTHER_ID == "") else: with pytest.raises(BadRecord): rec = Record(record)
def test_parsing_ZIP_CODE(record, isGood, value): if isGood: rec = Record(record) assert (rec.ZIP_CODE == value) else: with pytest.raises(BadRecord): rec = Record(record)
def data(self): msg = Message() for carrier in self.carriers: msg.append(Record('urn:nfc:wkt:ac', data=str(carrier))) if self.error.reason is not None: msg.append(Record("urn:nfc:wkt:err", data=str(self.error))) return str(self.version) + str(msg)
def test_note_filter(self): dt = datetime(year=2021, month=1, day=25, hour=19, minute=32) self._record_list.append(Record(dt, note='ABC')) self._record_list.append(Record(dt, note='BCD')) self._record_list.append(Record(dt, note='CDA')) indexes, str_records = self._record_list.filter_record(fragment='A') self.assertEqual([0, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='B') self.assertEqual([0, 1], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> BCD', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='C') self.assertEqual([0, 1, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> BCD', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='D') self.assertEqual([1, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> BCD', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records)
def main(): """Test script""" import numpy as np from record import Record import timeit n = 1000 channel_count = 25 channels = ["ch" + str(c) for c in range(channel_count)] pid1 = start(channels, 'buffer1.db') pid2 = start(channels, 'buffer2.db') starttime = timeit.default_timer() for i in range(n): d = [np.random.uniform(-1000, 1000) for cc in range(channel_count)] if i % 2 == 0: append(pid1, Record(d, i, None)) else: append(pid2, Record(d, i, None)) endtime = timeit.default_timer() totaltime = endtime - starttime print("Records inserted in buffer 1: {}".format(count(pid1))) print("Records inserted in buffer 2: {}".format(count(pid2))) print("Total insert time: " + str(totaltime)) query_n = 5 data = get_data(pid1, 0, query_n) print("Sample records from buffer 1 (query < {}): {}".format(query_n, data)) stop(pid1) stop(pid2)
def youAreLeader_handler(self, addr, args, recievedMsg): print("\n" + addr) if self.electionStatus is not None and self.electionStatus.decided == False: response = Election.fromString(recievedMsg) if response.view > self.electionStatus.view and ( response.view % self.totalNumber) != self.pid: # not me self.electionStatus.latestValue = Message.fromString( response.latestValue) if self.electionStatus.majorityCheck.addVoteAndCheck(): self.electionStatus.decided = True print("Yeah, I become a leader, pid: ", self.pid) #self.leaderAlive = True hole = self.detectHole() if hole != -1: filling = Record(self.view, hole, self.buffer[0]) self.propose(filling) if self.electionStatus.latestValue is not None: #doubt, why is this here? latestMsg = Message(self.electionStatus.latestValue) fromPrevious = Record(self.view, response.roundNumber, latestMsg) self.propose(fromPrevious) print("Ready to process client requests.")
def getRecord(self, keyword, use_subject='', path='', return_all=False): subject = default_subject; if use_subject != "": subject = use_subject if path == '': path = self.getPath(subject) print 'searching %s'%keyword + " in " + subject record_list = [] for file_name in self.find_file_by_pattern(".*", path): f = open(file_name) for line in f.readlines(): record = Record(line) record.set_path(file_name) if record.get_id().lower().strip() == keyword.lower().strip(): print "found " + record.get_id() + ' ' + record.get_title() + ' ' + record.get_url() + ' in ' + self.shortFileName(file_name) if return_all: record_list.append(record) else: return record if return_all: if len(record_list) > 0: return record_list else: print "no record found in " + subject +" db" return record_list.append(Record('')) else: print "no record found in " + subject +" db" return Record('')
def test_parsing_TRANSACTION_AMT(record, isGood, value): if isGood: rec = Record(record) assert (rec.TRANSACTION_AMT == value) else: with pytest.raises(BadRecord): rec = Record(record)
def test_parsing_TRANSACTION_DT(record, isGood, date): if isGood: rec = Record(record) assert (rec.TRANSACTION_DT == pd.to_datetime(date)) else: with pytest.raises(BadRecord): rec = Record(record)
def test_is_active(self): dt = datetime(year=2021, month=1, day=18, hour=18, minute=40) r = Record(dt, tag_id=1, is_active=True) self.assertEqual( '[18.01.2021 18:40:00] <test tag: start>', str(r)) r = Record(dt, tag_id=1, is_active=False) self.assertEqual( '[18.01.2021 18:40:00] <test tag: end>', str(r))
def get_records(self, gunid): key = Record(gunid).enc_gunid() try: values = self._get_records(key) except KeyError as ex: raise KeyError(gunid) from None for value in values: yield Record(gunid).dec_metadata(value)
def test_parsing_CMTE_ID(record, isGood, value): if isGood: rec = Record(record) print(rec.CMTE_ID) assert (rec.CMTE_ID == value) else: with pytest.raises(BadRecord): rec = Record(record)
def editRecord(self, rID, data, originFileName, library='', resourceType='', divID=''): print 'editRecord->' + data record = Record(' | | | ' + data) newid = self.utils.reflection_call('record', 'WrapRecord', 'get_tag_content', record.line, {'tag': 'id'}) if newid != None: newid = newid.strip() else: newid = rID title = self.utils.reflection_call('record', 'WrapRecord', 'get_tag_content', record.line, { 'tag': 'title', 'library': library }).strip() url = self.utils.reflection_call('record', 'WrapRecord', 'get_tag_content', record.line, { 'tag': 'url', 'library': library }).strip() desc = data.replace('id:' + newid, '').replace('title:' + title, '').replace('url:' + url, '').strip() if url == None: url = '' newRecord = Record(newid + ' | ' + title + ' | ' + url + ' | ' + desc) result = newRecord.editRecord(self.utils, rID, newRecord, originFileName, library, resourceType=resourceType) if result: print divID if divID.find('-history-') != -1: aid = divID[0:divID.find('-history-')].strip() + '-a-' result = 'refresh#history#' + aid elif divID.find('-content-') != -1: aid = divID[0:divID.find('-content-')].strip() + '-a-' result = 'refresh#content#' + aid else: result = 'refresh' else: result = 'error' print 'result--->' + result return result
def getRecords(self): data = pandas.read_csv(DATA) for itm in data.values: if itm[1] == self.phoneNumber: record = Record(itm[0], itm[1], itm[2], itm[3], itm[4]) self.originRecords.append(record) if itm[2] == self.phoneNumber: record = Record(itm[0], itm[1], itm[2], itm[3], itm[4]) self.destRecords.append(record)
def data(self): msg = Message() if self.version >= Version('\x12'): if self.nonce is None: raise EncodeError("collision resolution required since V1.2") crn = struct.pack(">H", self.nonce) msg.append(Record("urn:nfc:wkt:cr", data=crn)) for carrier in self.carriers: msg.append(Record('urn:nfc:wkt:ac', data=str(carrier))) return str(self.version) + str(msg)
def test_str(self): dt = datetime(year=2021, month=1, day=16, hour=19, minute=4) self._record_list.append(Record(dt, note='first note')) dt = datetime(year=2021, month=1, day=16, hour=19, minute=20) self._record_list.append(Record(dt, note='second note')) self.assertEqual( '[16.01.2021 19:04:00] <no tag> first note\n' '[16.01.2021 19:20:00] <no tag> second note', str(self._record_list), )
def test_get_tag_id_stat(self): self.assertEqual({}, self._record_list.get_tag_id_stat()) self._record_list.append(Record()) self.assertEqual({0: 1}, self._record_list.get_tag_id_stat()) self._record_list.append(Record(tag_id=1)) self.assertEqual({0: 1, 1: 1}, self._record_list.get_tag_id_stat()) self._record_list.append(Record(tag_id=1)) self.assertEqual({0: 1, 1: 2}, self._record_list.get_tag_id_stat())
def test_sort_dt(self): dt = datetime(year=2021, month=1, day=23, hour=17, minute=37) self._record_list.append(Record(dt)) dt = datetime(year=2021, month=1, day=23, hour=17, minute=36) self._record_list.append(Record(dt)) self._record_list.sort() self.assertEqual( '[23.01.2021 17:36:00] <no tag>\n' '[23.01.2021 17:37:00] <no tag>', str(self._record_list), )
def data(self): # encode smart poster payload as ndef message message = Message(UriRecord(self._uri)) for lang, text in self.title.iteritems(): message.append(TextRecord(text=text, language=lang)) for image_type, image_data in self.icons.iteritems(): message.append(Record("image/"+image_type, data=image_data)) if self._action >= 0: message.append(Record("urn:nfc:wkt:act", data=chr(self._action))) if self._res_size: size = struct.pack('>L', self._res_size) message.append(Record("urn:nfc:wkt:s", data=size)) return str(message)
def test_sort_is_active_none(self): dt = datetime(year=2021, month=1, day=23, hour=18, minute=6) self._record_list.append(Record(dt, is_active=True)) # start dt = datetime( year=2021, month=1, day=23, hour=18, minute=6, microsecond=1) self._record_list.append(Record(dt, tag_id=1)) self._record_list.sort() self.assertEqual( '[23.01.2021 18:06:00] <test tag>\n' '[23.01.2021 18:06:00] <no tag: start>', str(self._record_list), )
def test_insert(self): sp = SlottedPage(66) sp[1] = Record(0, cname1, phone1) sp.insert(test_record) self.assertEqual(sp[0], test_record) sp = SlottedPage(66) sp[0] = Record(0, cname1, phone1) sp.insert(test_record) self.assertEqual(sp[1], test_record) self.assertEqual(type(sp[1]), Record)
def makeArrayOfRecord(strRecordArray, recordArray): for x in range (1,len(strRecordArray)): pieces = re.split(":", strRecordArray[x]) # print pieces[0] # print pieces[1] if(len(pieces)== 2): record = Record(pieces[0],pieces[1],True) recordArray.append(record) else: record = Record(pieces[0],pieces[1],False) recordArray.append(record) printRecord(pieces) print "\n" print "\n"
def commit(self, chain): if self.kind == "regular": self.input = chain.get_unspent_records(self.frm) unspent_amount = chain.get_balance(self.frm) if self.amount > unspent_amount: return "\nInsufficient balance for this transaction. Current balance: " + str( unspent_amount) remainder_amount = unspent_amount - self.amount self.output.append(Record(self.to, self.amount)) self.output.append(Record(self.frm, remainder_amount)) return True elif self.kind == "buy": self.output.append(Record(self.to, self.amount)) return True
def translate(userid, content, in_chat_mode=False, user_content=None): try: # if type(content) == unicode: # content = content.encode('utf-8') return_audio = False if content.startswith("#") or content.startswith(u"#"): content = content[1:] return_audio = True result = get_cache_translation(content) ##save tranlation record of user if record_services: if not in_chat_mode: reply = Record("translator", result.pretty(), Record.TRANSLATION_RESULT) record = Record(userid, content, Record.TRANSLATION_REQUEST, reply) else: reply = Record("tuling_robot", content + "\n----\n" + result.pretty(), Record.CHAT_RESPONSE) record = Record(userid, user_content, Record.CHAT_REQUEST, reply) try: record_services.add_record(record) except Exception, e: logger.exception(e) logger.info("get translation:%s" % result.words) if client: client.send_text_message(userid, result.words) mediaid = get_mediaid(result.pronounce_list) if mediaid: client.send_voice_message(userid, mediaid) return result.pretty() elif return_audio: return get_music_msg(result) else: if redis_client: key = userid + "_last_content" redis_client.set(key, content) redis_client.expire(key, cfg.translation_expire_seconds) resp_text = result.pretty() + u"\n--回复#获得语音--" if in_chat_mode: resp_text = content + "\n----\n" + resp_text return resp_text else: return result.pretty()
def simulate(self, ts, board, player): from record import Record from value_network import NoActionException records = Record() while True: try: bd = board.copy() board_str = util.board_str(board) valid_action = rule.valid_actions(board, player) while True: (from_, act), q = self.epsilon_greedy(board, player, valid_action, ts) if (board_str, from_, act) not in self.predicts or len( ts.root.sub_edge) == 1: break ts.root.sub_edge = [ e for e in ts.root.sub_edge if e.a != (from_, act) ] valid_action.remove((from_, act)) assert board[from_] == player ts.move_down(board, player, action=(from_, act)) if self.episode % 10 == 0: logger.info('action:%s,%s', from_, act) logger.info('q is %s', q) to_ = tuple(np.add(from_, rule.actions_move[act])) command, eat = rule.move(board, from_, to_) records.add3(bd, from_, act, len(eat), win=command == rule.WIN) except NoActionException: # 随机初始化局面后一方无路可走 return Record(), 0 except Exception as ex: logging.warning('board is:\n%s', board) logging.warning('player is: %s', player) valid = rule.valid_actions(board, player) logging.warning('valid is:\n%s', valid) logging.warning('from_:%s, act:%s', from_, act) ts.show_info() records.save('records/train/1st_') raise ex if command == rule.WIN: logging.info('%s WIN, step use: %s, epsilon:%s', str(player), records.length(), self.epsilon) return records, player if records.length() > 10000: logging.info('走子数过多: %s', records.length()) return Record(), 0 player = -player board = rule.flip_board(board)
def new(self, default=True, obj_id=None): record = Record(self.model_name, obj_id, group=self) if default: record.default_get() record.signal_connect(self, 'record-changed', self._record_changed) record.signal_connect(self, 'record-modified', self._record_modified) return record
def build_lines(list_all): id_title_lines = copy.deepcopy(list_all) describe_lines = [] for i in range(0, custom_cell_row): describe_lines.append(copy.deepcopy(list_all)) for i in range(0, len(list_all)): update_cell_len(i) if len(id_title_lines[i]) == 0: record = Record("") id_title_lines[i].append(align_id_title(record)) for l in range(0, len(describe_lines)): describe_lines[l][i].append(align_describe("")) for j in range(0, len(list_all[i])): id_title_lines[i][j] = align_id_title(list_all[i][j]) describe = utils.str_block_width(list_all[i][j].get_describe()) start = 0 end = 0 if output_with_describe == True: for l in range(0, len(describe_lines)): if end >= describe: describe_lines[l][i][j] = align_describe("") continue end = next_pos(list_all[i][j].get_describe(), start) describe_lines[l][i][j] = align_describe( list_all[i][j].get_describe()[start:end]) start = end return id_title_lines, describe_lines
def main(): try: src, start, end = validate_args(sys.argv[1:]) except ValueError: print( "Error: Invalid Arguments: please run this program like " + f"'python {__file__} accounts/account.csv -s 2020-08-24 -e 2020-09-01'" ) return if not src.is_file(): print(f"Error: '{src}' does not exist or is a directory.") return if src.suffix != ".csv": print("Error: Invalid Extension: Only CSV file is available.") return records = RecordList([]) with open(src, "r") as f: for line in f: records.append(Record(*line.strip().split(","))) category_total = records.category_total(start, end) c_length = max_len(category_total.keys()) e_length = max_len(category_total.values()) print(f"{'total':{c_length}s} | {category_total['total']:>{e_length}d}") for category, expense in category_total.items(): if category != "total": print(f"{category:{c_length}s} | {expense:>{e_length}d}")