def compile_while_statement(self, function_id, statement): current_label_index = self.while_label_index self.while_label_index += 1 logger.info('==========into while==========') # 方式while。start标签,以便重复执行 while_start = 'WHILE_EXP{}'.format(current_label_index) self.vm_code.append('label {}'.format(while_start)) # 计算while表达式的值,放入堆栈中 expression = statement.find('expression', recursive=False) expression_compiler = ExpressionCompiler(function_id, self.st) expression_compiler.compile_expression(expression) self.vm_code.extend(expression_compiler.get_vm_code()) # 构造while判断体 # not 表达式为真则退出 while_end = 'WHILE_END{}'.format(current_label_index) self.vm_code.append('not') self.vm_code.append('if-goto {}'.format(while_end)) # 判断这个值大于0则进入程序块WHILE_START.while_label_index while_true = 'WHILE_TRUE{}'.format(current_label_index) # while程序体 expression_bodys = statement.find_all('statements', recursive=False) self.compile_statements(function_id, expression_bodys) # 程序体跳转回去继续判断 self.vm_code.append('goto {}'.format(while_start)) self.vm_code.append('label {}'.format(while_end))
def _answerCIPSTARTHandler(self, answer): if answer.ok: # Serial port self.__serialPort.rawMode = True try: localPort = int(self.localport_text.GetValue()) downPort = int(self.local_down_port_text.GetValue()) type = self.conntype_combo.GetValue() # Check correct values if self.__localSocket.start(localPort, downPort, type): # Serial and local socket are both connected # now just enable redirection txt = '\nWaiting for local socket connection' logger.info('Waiting for local socket connection') else: txt = '\nError opening local ports' logger.error('Error opening local ports') txt += '\nRemote connection open' logger.info('Remote connection open') except Exception, v: txt = '\nWrong port number' logger.error('Wrong port number(s) ' + v.strerror)
def localSocketConnectionOpen(self): self.__socketRedirection = True logger.info("Local socket, connection open") wx.CallAfter(pub.sendMessage, "AppendLogText", text='Local socket connected\nBridge enabled')
def today_to_do_list(self): """ 生成今天的任务 :return: """ # 取遗忘曲线未完成任务 sql = "select id,name,content,update_time,ebbinghausid from items where status = 1" self.db_con.execute(sql) raw_rows = self.db_con.fetchall() ebbinghaus_rows = [] for row in raw_rows: # 计算差值 interval_time = cal_time_interval(row[3], datetime.now()) # 查询ebbinghaus信息 sql = "select time from ebbinghaus where id = %d" % row[4] self.db_con.execute(sql) ebbinghaus_raw_rows = self.db_con.fetchall() # 大于遗忘曲线指定时间的都要取出 logger.info( "interval_time[%f]ebbinghaus_time[%f]" % (float(interval_time), float(ebbinghaus_raw_rows[0][0]))) if float(interval_time) >= float(ebbinghaus_raw_rows[0][0]): content = self.content_type._make(list(row)) ebbinghaus_rows.append(content) list_rows = ebbinghaus_rows return list_rows
def get_day_by_rank_id(self, rank): sql = "select time from ebbinghaus,rank where rank.ebbinghausId = ebbinghaus.id and rank = %d" % rank logger.info(sql) self.db_con.execute(sql) raw_row = self.db_con.fetchall() return int(raw_row[0][0])
def compile_function_start(self, function_id): local_vars_amount = self.st.get_local_vars_amount(function_id) logger.debug('function_id:{}, local_vars_amount:{}'.format( function_id, local_vars_amount)) code = 'function {} {}'.format(function_id, local_vars_amount) self.vm_code.append(code) logger.info(code)
def start(self, throughput, duration): result = True if self.running: result = False if result: #try: self.running = True nrPacketsPerSec = throughput / 80 if nrPacketsPerSec >= 1000: self.__nrOfPacketsPerIteration = nrPacketsPerSec / 1000 self.__delay = 0.001 else: self.__nrOfPacketsPerIteration = 1 self.__delay = 1.0 / nrPacketsPerSec self.__repetitions = duration / self.__delay self.__data = self.__tx_decoder.encode(self.__data) logger.info("Start serial write, repetitions %s, %s" % (self.__repetitions, self.__nrOfPacketsPerIteration)) self.__serialThread = threading.Thread( target=self.__serialWriterLoop, args=()) self.__serialThread.daemon = True self.__serialThread.start() #except: #logger.error("Error starting serial loop") #result = False return result
def update_today_task(self, item_id, rank, remark): """ 更新任务难度 :param item_id: 任务ID :param rank: 1-right[EbbinghausId:8] 2-easy[EbbinghausId:5] 3-general[EbbinghausId:4] 4-difficultly[EbbinghausId:3] 5-difficultly[EbbinghausId:2] :param remark: 备注 :return: """ ebbinghaus_id = 0 if rank == 1: ebbinghaus_id = 8 elif rank == 2: ebbinghaus_id = 5 elif rank == 3: ebbinghaus_id = 4 elif rank == 4: ebbinghaus_id = 3 elif rank == 5: ebbinghaus_id = 2 sql = "select times,ebbinghausid,update_time from items where id = %d" % item_id logger.info(sql) self.db_con.execute(sql) raw_rows = self.db_con.fetchall() times = int(raw_rows[0][0]) + 1 # 次数加一 # rank = int(raw_rows[0][1]) # 只有等级调小才重新更新日期,计算时间间隔 # if ebbinghaus_id < int(raw_rows[0][1]): # update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 当期时间 # else: # update_time = raw_rows[0][2] # 原来时间 # # # 未按期完成的,重新开始计算 rank_days = self.get_day_by_rank_id(rank) # interval_time = cal_time_interval(update_time, datetime.now()) # 任务完成时间间隔 # logger.info("update_time[%s],now[%s],interval_time[%f]ebbbinghaus_interval[%f]" # % (update_time, datetime.now(), interval_time, rank_days)) # if interval_time > rank_days: # 任务完成时间间隔大于ebbinghuas时间间隔 # update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 更新时间都取最新时间 update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 当期时间 now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) now_time = datetime.strptime(now, '%Y-%m-%d %H:%M:%S') delta = timedelta(days=rank_days) review_time = (now_time + delta).strftime('%Y-%m-%d %H:%M:%S') sql = "update items set times = %d, ebbinghausid = %d, remark = '%s', update_time = '%s', review_time= '%s' " \ "where id = %d" % (times, ebbinghaus_id, remark.strip(), update_time, review_time, item_id) logger.info(sql) self.db_con.execute(sql) self.db_con.commit()
def tree_click(event, tree, winframe): sub_frame = Tk() sub_frame.title("Modify Item") # 设置窗口名字 screenwidth = sub_frame.winfo_screenwidth() screenheight = sub_frame.winfo_screenheight() size = '%dx%d+%d+%d' % (500, 220, (screenwidth - 500) / 2, (screenheight - 220) / 3) sub_frame.geometry(size) for item in tree.selection(): item_text = tree.item(item, "values") print(item_text[2]) # 输出所选行的第一列的值 box_frame = Frame(sub_frame) box_frame.pack(side=TOP, fill=BOTH, expand=NO) # Name标签 text_label_name = Label(box_frame, text='Task Name:') text_label_name.grid(row=0, column=0) # 输入框 logger.info(item_text[1]) input_box = Entry(box_frame, show=None, font=('Arial', 9), width=50) input_box.insert(END, item_text[1]) input_box.grid(row=0, column=1) # Content标签 text_label_content = Label(box_frame, text='Task Content:') text_label_content.grid(row=1, column=0) # 文本框 text_box = Text(box_frame, width=50, height=5, font=('Arial', 9)) text_box.insert(INSERT, item_text[3]) text_box.grid(row=1, column=1) # Content标签 remark_label_content = Label(box_frame, text='Remark:') remark_label_content.grid(row=2, column=0) remark_box = Text(box_frame, width=50, height=5, font=('Arial', 9)) remark_box.insert(INSERT, item_text[4]) remark_box.grid(row=2, column=1) # 新建button_frame button_frame = Frame(sub_frame) button_frame.pack(side=TOP, expand=NO) # 按钮 button_box_r = Button( button_frame, text='Save', width=5, height=1, command=lambda: update_item(winframe, sub_frame, item_text[0], input_box.get(), text_box.get(1.0, END), remark_box.get(1.0, END))) button_box_r.grid(row=0, column=1) sub_frame.mainloop()
def get_item_name(self): sql = "SELECT DISTINCT name FROM items" logger.info(sql) self.db_con.execute(sql) raw_row = self.db_con.fetchall() ret_row = [] for ele in raw_row: ret_row.append(ele[0]) return tuple(ret_row)
def connect_remote_buttonOnButtonClick(self, event): if self.__serialPort.status: if not self.__serialPort.rawMode: params = [ self.conntype_combo.GetValue(), self.remoteaddress_text.GetValue(), self.remoteport_text.GetValue() ] cmd = ATcommand(atcmds.CIPSTART_CMD_ID, True, params) self.__serialPort.sendCommand(cmd) else: logger.info("Connection already open")
def compile_function_define(self, subroutine_dec): function_id = self.get_class_name() + '.' + subroutine_dec.find_all( )[2].text # 进行子程序头部定义。格式为 function func_name local_var_amount local_vars_amount = self.st.get_local_vars_amount(function_id) logger.debug('function_id:{}, local_vars_amount:{}'.format( function_id, local_vars_amount)) code = 'function {} {}'.format(function_id, local_vars_amount) self.vm_code.append(code) logger.info(code) # 处理方法体部分 statements = subroutine_dec.find('subroutineBody').find_all( 'statements', recursive=False) self.compile_statements(function_id, statements)
def export_all_task(self): book = xlwt.Workbook() sheet = book.add_sheet('sheet') alignment = xlwt.Alignment() # 创建居中 alignment.horz = xlwt.Alignment.HORZ_CENTER alignment.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() # 创建样式 style.alignment = alignment # 给样式添加文字居中属性 style.font.height = 200 # 设置字体大小 col_name = [('ID', 'Name', 'Content', 'Time', 'EbbinghausID')] for row, line in enumerate(col_name): logger.info(line) for col, value in enumerate(line): logger.info(value) sheet.write(row, col, value, style) for row, line in enumerate(self.items): row = row + 1 logger.info(line) for col, value in enumerate(line): logger.info(value) sheet.write(row, col, value) date_time = datetime.now().strftime('%Y-%m-%d') xml_file = filedialog.asksaveasfilename() book.save('%s_%s.xls' % (xml_file, date_time))
def compile_return_statement(self, function_id, statement): # 如果return后面有表达式 if statement.find('expression'): # 计算返回值表达式 expression_compiler = ExpressionCompiler(function_id, self.st) expression_compiler.compile_expression( statement.find('expression')) self.vm_code.extend(expression_compiler.get_vm_code()) else: self.vm_code.append('push constant 0') self.vm_code.append('return') logger.info('push constant 0') logger.info('return') return
def list_all_by_query(self, query_string): sql = "SELECT * FROM items WHERE Name = '%s' ORDER BY ID DESC" % query_string logger.info(sql) self.db_con.execute(sql) raw_rows = self.db_con.fetchall() item_type = namedtuple('item', [ 'id', 'name', 'time', 'content', 'remark', 'times', 'ebbinghuasid', 'status', 'update_time', 'review_time' ]) ret_rows = [] for row in raw_rows: item = item_type._make(list(row)) ret_rows.append(item) return ret_rows
def start(self, port, downPort, socketType): result = True if self.status and socketType not in ['TCP', 'UDP']: result = False else: try: if socketType == 'TCP': self.__listenSocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port self.__remoteAddress = ( "", port) # to connect from outside the machine self.__listenSocket.bind(self.__remoteAddress) logger.info('Starting request port on port ' + str(port)) target = self._listen_thread self.status = True elif socketType == 'UDP': # Uplink socket (read) self.__connSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) uplinkAddr = ("127.0.0.1", port) self.__connSocket.bind(uplinkAddr) self.__connSocket.setblocking(0) self.__connSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Downlink socket (write) self.__remoteAddress = ("127.0.0.1", downPort) self.__udpDownSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM) self.__udpDownSocket.setblocking(0) logger.info('Open socket on port ' + str(port)) target = self._udp_listen_thread self.status = True self.connOpenCallback() except socket.error, v: logger.error('Error starting local socket Port ' + v.strerror) result = False
def compile(self): logger.debug('start to compile') subroutine_decs = self.soup.find_all('subroutineDec') # print('subroutines', subroutines) index = 0 for subroutine_dec in subroutine_decs: index += 1 logger.info('处理第{}个方法'.format(index)) if subroutine_dec.find().text == 'function': self.compile_function_define(subroutine_dec) elif subroutine_dec.find().text == 'constructor': self.compile_constructor_define(subroutine_dec) elif subroutine_dec.find().text == 'method': self.compile_method_define(subroutine_dec) else: raise Exception('未知的结构:{}'.format(subroutine_dec)) logger.info('第{}个方法处理完成'.format(index)) return self.vm_code
def compile_constructor_define(self, subroutine_dec): function_id = self.get_class_name() + '.' + subroutine_dec.find_all( )[2].text # 进行子程序头部定义。格式为 function func_name local_var_amount local_vars_amount = self.st.get_local_vars_amount(function_id) logger.debug('function_id:{}, local_vars_amount:{}'.format( function_id, local_vars_amount)) code = 'function {} {}'.format(function_id, local_vars_amount) self.vm_code.append(code) logger.info(code) # 分配内存空间 class_vars_amount = self.st.get_class_vars_amount( self.get_class_name()) self.vm_code.append('push constant {}'.format(class_vars_amount)) self.vm_code.append('call Memory.alloc 1') # 设置当前对象的this地址 self.vm_code.append('pop pointer 0') # 处理方法体部分 statements = subroutine_dec.find('subroutineBody').find_all( 'statements', recursive=False) self.compile_statements(function_id, statements)
def compile_term(self, term): # logger.debug(term) name = term.find().name text = term.find().text length = len(term.find_all(recursive=False)) if length > 1: next_text = term.find_all(recursive=False)[1].text # 如果是前缀操作符号 if name == 'symbol' and length > 1 and text in ['~', '-']: self.handle_symbol(term, text) # 如果是数字常量 elif name == 'integerConstant': self.vm_code.append('push constant {}'.format(text)) # 如果是字符串常量 elif name == 'stringConstant': logger.info('=======stringConstant========{}'.format(text)) self.handle_string(text) # 如果是复合表达式 elif text == '(': expression = term.find('expression', recursive=False) self.compile_expression(expression) # 如果是方法调用 elif name == 'identifier' and length > 1 and (next_text == '(' or next_text == '.'): self.handle_subroutine_call(next_text, term) # 如果是变量 elif name == 'identifier' and length == 1: self.deal_single_var(text) # todo 如果是数组变量 elif name == 'identifier' and length > 1 and next_text == '[': self.handle_array(term) elif name == 'keyword' and text in ['true', 'false', 'null', 'this']: self.headle_keyword(text) else: logger.error('无法识别此表达式{}'.format(term)) raise Exception('无法识别此表达式{}'.format(term))
def parse(self): file = [] self.parse_class_vars() self.parse_local_vars() self.parse_argument_vars() self.all_table.extend(self.class_table) self.all_table.extend(self.local_table) self.all_table.extend(self.arg_table) logger.info('类变量 {} 个'.format(len(self.class_table))) for item in self.class_table: logger.info(item) logger.info('私有变量 {} 个'.format(len(self.local_table))) for item in self.local_table: logger.info(item) logger.info('参数变量 {} 个'.format(len(self.arg_table))) for item in self.arg_table: logger.info(item)
def update_item(self, item_id, name, content, remark): sql = "update items set name = '%s', content = '%s', remark = '%s' where id = %d" % \ (name, content.strip(), remark.strip(), int(item_id)) logger.info(sql) self.db_con.execute(sql) self.db_con.commit()
def localSocketConnectionClosed(self): print "localSocketConnectionClosed(self):" logger.info("Local socket, connection open") self.__socketRedirection = False
def print_source_code(self, expression): head_nodes = expression.find_all()[:3] code = self.get_code(head_nodes) code = code.replace('\n', '').replace('\r', '') logger.info('开始解析:{}'.format(code))
def run(self): """Execute events until the queue is empty. When there is a positive delay until the first event, the delay function is called and the event is left in the queue; otherwise, the event is removed from the queue and executed (its action function is called, passing it the argument). If the delay function returns prematurely, it is simply restarted. It is legal for both the delay function and the action function to modify the queue or to raise an exception; exceptions are not caught but the scheduler's state remains well-defined so run() may be called again. A questionable hack is added to allow other threads to run: just after an event is executed, a delay of 0 is executed, to avoid monopolizing the CPU when other threads are also runnable. """ # localize variable access to minimize overhead # and to improve thread safety q = scheduler._queue delayfunc = self.delayfunc timefunc = self.timefunc pop = heapq.heappop global logger def print_empty(): print 'empty' while q: scheduler.print_queue_id() if len(q) == 0: print 'Empty queue\n' self.enter(1, 10, print_empty, (), 99999, 'kkk') checked_event = q[0] time, priority, action, argument, uid, description = checked_event now = timefunc() if time > now: Wakeup_Lock().CloseLock() while time > now and Wakeup_Lock().IsLocked(): now = timefunc() delayfunc(1) #print 'event %s handle %s description %s Sleeping\n' %(uid,action,description) else: if Wakeup_Lock().IsLocked(): Wakeup_Lock().OpenLock() event = pop(q) # Verify that the event was not removed or altered # by another thread after we last looked at q[0]. if event is not checked_event and event[5] == 'URG': msg = 'Urgent event %s arrived' logger.debug(msg, event[4]) logger.info("event %s %s called with args: %s", event[4], event[2], event[3]) event[2](*event[3]) delayfunc(0) # Let other threads run elif event is checked_event: logger.info("event %s %s called with args: %s", uid, action, argument) action(*argument) delayfunc(0) # Let other threads run else: heapq.heappush(q, event) print 'Empty Queue'
def _listen_thread(self): if not self.status: return try: # Listen for incoming connections self.__listenSocket.listen(1) except socket.error, v: logger.error('Socket error listening, ' + v.strerror) while self.status: try: logger.info('Waiting for new connection') # Wait for a connection self.__connSocket, client_address = self.__listenSocket.accept( ) logger.debug('New Connection from ' + client_address[0]) self.connOpen = True self.connOpenCallback() if not self._receive_data(): logger.info('Connection closed') self.connClosedCallback() except socket.error: logger.error('Exception accepting new connection') self.stop()
def evaluate_estimator(X_train, X_test, y_train, y_test, cl, parameters): # scores = ['accuracy', 'adjusted_rand_score', 'average_precision', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', # 'f1_weighted', 'log_loss', 'mean_absolute_error', 'mean_squared_error', 'median_absolute_error', # 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted', 'r2', # 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'roc_auc'] scores = [ 'accuracy', 'f1', 'f1_macro', 'f1_micro', 'f1_weighted', 'precision', 'precision_macro', 'precision_micro', 'precision_weighted', 'recall', 'recall_macro', 'recall_micro', 'recall_weighted', 'roc_auc' ] for score in scores: starttime = datetime.now() logger.info("# Tuning hyper-parameters for %s" % score) logger.info('START:********************%s********************' % score) try: clf = GridSearchCV(cl, parameters, cv=10, scoring=score, n_jobs=-1) clf.fit(X_train, y_train) logger.info("Best parameters set found on development set:") logger.info(clf.best_params_) # logger.info("Grid scores on development set:") # means = clf.cv_results_['mean_test_score'] # stds = clf.cv_results_['std_test_score'] # for mean, std, params in zip(means, stds, clf.cv_results_['params']): # logger.info("%0.3f (+/-%0.03f) for %r" # % (mean, std * 2, params)) logger.info("Detailed classification report:") logger.info("The model is trained on the full development set.") logger.info("The scores are computed on the full evaluation set.") y_true, y_pred = y_test, clf.predict(X_test) logger.info(classification_report(y_true, y_pred)) except Exception as e: logger.info(e) endtime = datetime.now() logger.info('耗时:********************%s********************\n' % str( (endtime - starttime).seconds)) logger.info('END:********************%s********************\n' % score)