def get_logs(self, jid, nb=10): """ Get the nb last messages from the log history for the given jid. Note that a message may be more than one line in these files, so this function is a little bit more complicated than “read the last nb lines”. """ if config.get_by_tabname('load_log', jid) <= 0: return if not config.get_by_tabname('use_log', jid): return if nb <= 0: return self.check_and_create_log_dir(jid, open_fd=False) try: fd = open(os.path.join(log_dir, jid), 'rb') except: log.error('Unable to open the log file (%s)', os.path.join(log_dir, jid), exc_info=True) return if not fd: return # read the needed data from the file, we just search nb messages by # searching "\nM" nb times from the end of the file. We use mmap to # do that efficiently, instead of seek()s and read()s which are costly. with fd: try: m = mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ) except Exception: # file probably empty log.error('Unable to mmap the log file for (%s)', os.path.join(log_dir, jid), exc_info=True) return pos = m.rfind(b"\nM") # start of messages begin with MI or MR, # after a \n # number of message found so far count = 0 while pos != -1 and count < nb-1: count += 1 pos = m.rfind(b"\nM", 0, pos) if pos == -1: # If we don't have enough lines in the file pos = 1 # 1, because we do -1 just on the next line # to get 0 (start of the file) lines = m[pos-1:].decode(errors='replace').splitlines() messages = [] color = '\x19%s}' % dump_tuple(get_theme().COLOR_LOG_MSG) # now convert that data into actual Message objects idx = 0 while idx < len(lines): if lines[idx].startswith(' '): # should not happen ; skip idx += 1 log.debug('fail?') continue tup = parse_message_line(lines[idx]) idx += 1 if not tup or 7 > len(tup) > 10: # skip log.debug('format? %s', tup) continue time = [int(i) for index, i in enumerate(tup) if index < 6] message = {'lines': [], 'history': True, 'time': common.get_local_time(datetime(*time))} size = int(tup[6]) if len(tup) == 8: #info line message['lines'].append(color+tup[7]) else: # message line message['nickname'] = tup[7] message['lines'].append(color+tup[8]) while size != 0 and idx < len(lines): message['lines'].append(lines[idx][1:]) size -= 1 idx += 1 message['txt'] = '\n'.join(message['lines']) del message['lines'] messages.append(message) return messages
def test_local_time(): delta = timedelta(seconds=-3600) d = datetime.datetime.now() time.timezone = -3600 time.altzone = -3600 assert get_local_time(d) == d - delta
def get_logs(self, jid, nb=10): """ Get the nb last messages from the log history for the given jid. Note that a message may be more than one line in these files, so this function is a little bit more complicated than “read the last nb lines”. """ if config.get_by_tabname('load_log', jid) <= 0: return if not config.get_by_tabname('use_log', jid): return if nb <= 0: return self.check_and_create_log_dir(jid, open_fd=False) try: fd = open(os.path.join(log_dir, jid), 'rb') except FileNotFoundError: log.info('Non-existing log file (%s)', os.path.join(log_dir, jid), exc_info=True) return except OSError: log.error('Unable to open the log file (%s)', os.path.join(log_dir, jid), exc_info=True) return if not fd: return # read the needed data from the file, we just search nb messages by # searching "\nM" nb times from the end of the file. We use mmap to # do that efficiently, instead of seek()s and read()s which are costly. with fd: try: m = mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ) except Exception: # file probably empty log.error('Unable to mmap the log file for (%s)', os.path.join(log_dir, jid), exc_info=True) return pos = m.rfind(b"\nM") # start of messages begin with MI or MR, # after a \n # number of message found so far count = 0 while pos != -1 and count < nb - 1: count += 1 pos = m.rfind(b"\nM", 0, pos) if pos == -1: # If we don't have enough lines in the file pos = 1 # 1, because we do -1 just on the next line # to get 0 (start of the file) lines = m[pos - 1:].decode(errors='replace').splitlines() messages = [] color = '\x19%s}' % dump_tuple(get_theme().COLOR_LOG_MSG) # now convert that data into actual Message objects idx = 0 while idx < len(lines): if lines[idx].startswith(' '): # should not happen ; skip idx += 1 log.debug('fail?') continue tup = parse_message_line(lines[idx]) idx += 1 if not tup or 7 > len(tup) > 10: # skip log.debug('format? %s', tup) continue time = [int(i) for index, i in enumerate(tup) if index < 6] message = { 'lines': [], 'history': True, 'time': common.get_local_time(datetime(*time)) } size = int(tup[6]) if len(tup) == 8: #info line message['lines'].append(color + tup[7]) else: # message line message['nickname'] = tup[7] message['lines'].append(color + tup[8]) while size != 0 and idx < len(lines): message['lines'].append(lines[idx][1:]) size -= 1 idx += 1 message['txt'] = '\n'.join(message['lines']) del message['lines'] messages.append(message) return messages
feature_ = feature_.reshape(-1) return feature_ def pretreatment(pathname_): imglist_ = [ cv2.imread(os.path.join(pathname_, file_)) for file_ in os.listdir(pathname_) ] for img_ in imglist_: row_, col_, chn_ = img_.shape print(row_, col_) if __name__ == "__main__": print(common.get_local_time(), "程序运行开始") winSize = (32, 32) blockSize = (16, 16) blockStride = (8, 8) cellSize = (8, 8) nbins = 9 hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins) x_train, y_train, x_test, y_test = datasets.load_CIFAR10('data/cifar10/') grays = np.array([ cv2.cvtColor(x_train[i], cv2.COLOR_RGB2GRAY) for i in range(len(x_train)) ]) features = np.array([get_feature(gray, hog, (8, 8)) for gray in grays]) print(common.get_local_time(), "HOG特征提取完毕") new_features = features[:500] new_labels = y_train[:500]