def test_lazycache_provide_after_failed_lookup(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() linecache.getlines(NONEXISTENT_FILENAME) linecache.lazycache(NONEXISTENT_FILENAME, globals()) self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME))
def test_lazycache_smoke(self): lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() self.assertEqual(True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) # Note here that we're looking up a non existant filename with no # globals: this would error if the lazy value wasn't resolved. self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME))
def test_lazycache_smoke(self): lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() self.assertEqual( True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) # Note here that we're looking up a non existant filename with no # globals: this would error if the lazy value wasn't resolved. self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME))
def test_getline(self): getline = linecache.getline # Bad values for line number should return an empty string self.assertEqual(getline(FILENAME, 2**15), EMPTY) self.assertEqual(getline(FILENAME, -1), EMPTY) # Float values currently raise TypeError, should it? self.assertRaises(TypeError, getline, FILENAME, 1.1) # Bad filenames should return an empty string self.assertEqual(getline(EMPTY, 1), EMPTY) self.assertEqual(getline(INVALID_NAME, 1), EMPTY) # Check whether lines correspond to those from file iteration for entry in TESTS: filename = os.path.join(TEST_PATH, entry) + '.py' with open(filename) as file: for index, line in enumerate(file): self.assertEqual(line, getline(filename, index + 1)) # Check module loading for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' with open(filename) as file: for index, line in enumerate(file): self.assertEqual(line, getline(filename, index + 1)) # Check that bogus data isn't returned (issue #1309567) empty = linecache.getlines('a/b/c/__init__.py') self.assertEqual(empty, [])
def test_lazycache_already_cached(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) self.assertEqual( False, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))
def parse(self): print('Reading txt document ...') lines = linecache2.getlines(os.path.join('../download/', self.zip_name_id + '.txt')) line_num_arr = [] chapter_arr = [] for i in range(0, len(lines)): reg = re.search(r'(^\d+(\.)*( )*(第[零一二三四五六七八九十百千万0-9]+章))|(^第[零一二三四五六七八九十百千万0-9]+章)|(^[零一二三四五六七八九十百千万]+章)|(^\d+(\.)*[^零一二三四五六七八九十百千万0-9]+章)\S+\n', lines[i], re.M | re.I) if reg: num_raw = lines[i][int(reg.span()[0]):int(reg.span()[1])].replace('第', '').replace('章', '') if re.search(r'\d+', num_raw, re.I|re.M): num_true = num_raw else: num_true = cn2dig(num_raw) chapter_arr.append({ 'num': num_true, 'name': lines[i][int(reg.span()[1]):(len(lines[i]) - 1)].strip(), 'content': None }) line_num_arr.append(i) # 数组去重 for k in range(0, len(chapter_arr)-1): chapter_arr[k]['content'] = ''.join(lines[(line_num_arr[k]+1): line_num_arr[k+1]]) # print(chapter_arr) print('Read and parse txt success!') self.save_db(chapter_arr)
def test_no_ending_newline(self): temp_file = tempfile.NamedTemporaryFile( suffix='.py', mode='w', delete=False) self.addCleanup(os.unlink, temp_file.name) with open(temp_file.name, "w") as fp: fp.write(SOURCE_3) lines = linecache.getlines(temp_file.name) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
def test_no_ending_newline(self): temp_file = tempfile.NamedTemporaryFile(suffix='.py', mode='w', delete=False) self.addCleanup(os.unlink, temp_file.name) with open(temp_file.name, "w") as fp: fp.write(SOURCE_3) lines = linecache.getlines(temp_file.name) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
def test_lazycache_already_cached(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) self.assertEqual(False, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))
def test_lazycache_no_globals(self): lines = linecache.getlines(FILENAME) linecache.clearcache() self.assertEqual(False, linecache.lazycache(FILENAME, None)) self.assertEqual(lines, linecache.getlines(FILENAME))
def filedirectorycatchdata( filedirectory): #获取log csv文件数据 输出格式为[[all屏体数据][all按键数据]]数组 global L, usefuldatafile listfile = os.listdir(filedirectory) L = [ filename for filename in listfile if filename[-4:] == '.csv' and not filename.find('summary') != -1 ] print(' ' + '-' * 19 + '导入文件' + '-' * 20) alldata = [] allsampledata = [] allsamplekeydata = [] allsamplecptestdata = [] allsamplecpshortdata = [] nodatafile = [] usefuldatafile = [] for fileadr in L: try: #解决文件存在非法编码导致无法linecache问题 linecache.updatecache(filedirectory + fileadr) linefile = linecache.getlines(filedirectory + fileadr) except Exception: print(str(fileadr) + '该文件数据存在非法字符') newfile = codecs.open(filedirectory + fileadr, 'r', 'gbk', 'ignore') text = newfile.read() newfile.close() with codecs.open(filedirectory + fileadr, 'w') as newfile2: newfile2.write(text) linecache.updatecache(filedirectory + fileadr) linefile = linecache.getlines(filedirectory + fileadr) '''print(filedirectory+fileadr)''' linenumber = 0 starline = 0 endline = 0 sampledata = [] keyarray = [] cpteststartline = 0 cptestendline = 0 cpshortstartline = 0 cpshortendline = 0 sampledata = [] keyarray = [] samplecpselfdata = [] samplecpshortdata = [] for line in linefile: linenumber += 1 if line.find('CMDelta Test Start') != -1: starline = linenumber if line.find('CMDelta Test End') != -1: endline = linenumber if line.find('Self Cp Test Start') != -1: #加入Self CP test cpteststartline = linenumber if line.find('Self Cp Test End') != -1: cptestendline = linenumber if line.find('CP_SHORT Test Start') != -1: #加入CP Short test cpshortstartline = linenumber #print(cpshortstartline) if line.find('CP_SHORT Test End') != -1: cpshortendline = linenumber datanumber = 0 if starline != 0 and endline != 0: dataline = linefile[starline:endline] for data in dataline: if data.find('[Row00]') != -1: datastar = datanumber if data.find('CM Delta Key') != -1: dataend = datanumber datanumber += 1 keydata = dataline[dataend:endline] del keydata[0] del keydata[-1] keyarray = [] for k in keydata: if k == '\n': pass else: keyread = k.split(',') keyrealdata = keyread[:-1] for i in keyrealdata: if i == '' or i == '\n': pass else: newkey = (((((i.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') keyarray.append(int(newkey)) data = dataline[datastar:dataend - 1] for datare in data: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist.append(int(news)) if len(slist) != 0: sampledata.append(slist) usefuldatafile.append(str(fileadr)) else: nodatafile.append(str(fileadr)) if (len(sampledata) != 0): allsampledata.append(sampledata) if (len(keyarray) != 0): allsamplekeydata.append(keyarray) if cpteststartline != 0 and cptestendline != 0: #提取 Self CP 测试数据 #print('try to catch self cp data') selfcpdatanumber = 0 selfcpdataline = linefile[cpteststartline:cptestendline] for selfcpdata in selfcpdataline: if selfcpdata.find('Row00') != -1: selfdatastart = selfcpdatanumber if selfcpdata.find(' Self Cp Test End') != -1: selfdataend = selfcpdatanumber selfcpdatanumber += 1 selfcpdatafile = selfcpdataline[selfdatastart:selfdataend] for datare in selfcpdatafile: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist2 = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist2.append(int(news)) if len(slist2) != 0: samplecpselfdata.append(slist2) if (len(samplecpselfdata) != 0): #print(samplecpselfdata) allsamplecptestdata.append(samplecpselfdata) if cpshortstartline != 0 and cpshortendline != 0: #提取 CP SHORT 测试数据 #print('try to catch SHORT data') selfshortnumber = 0 cpshortline = linefile[cpshortstartline:cpshortendline] #print(cpshortline) for cpshortdata in cpshortline: if cpshortdata.find('Row00') != -1: cpshortstart = selfshortnumber if cpshortdata.find(' CP_SHORT Test End') != -1: cpshortend = selfshortnumber selfshortnumber += 1 cpshortfile = cpshortline[cpshortstart:cpshortend] #print(cpshortfile) for datare in cpshortfile: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist3 = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist3.append(int(news)) if len(slist3) != 0: samplecpshortdata.append(slist3) if (len(samplecpshortdata) != 0): #print(samplecpshortdata) allsamplecpshortdata.append(samplecpshortdata) print('*' * 19 + '数据不存在样品' + '*' * 19) print(nodatafile) print('\n') '''print('-'*19+'有效文件'+'-'*19) print(usefuldatafile)''' alldata.append(allsampledata) if (len(allsamplekeydata) != 0): alldata.append(allsamplekeydata) return alldata
patch = fixtures.MonkeyPatch('sys.%s' % streamname, stream) with patch: yield stream FNAME = __file__ if FNAME.endswith('.pyc'): FNAME = FNAME[:-1] class FakeLoader: def __init__(self, lines): self._lines = lines def get_source(self, name): return self._lines fake_module = dict( __name__='fred', __loader__=FakeLoader(''.join(linecache.getlines(FNAME))) ) test_code = namedtuple('code', ['co_filename', 'co_name']) test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals']) test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next']) class SyntaxTracebackCases(testtools.TestCase): # For now, a very minimal set of tests. I want to be sure that # formatting of SyntaxErrors works based on changes for 2.1. def get_exception_format(self, func, exc): try: func()
FNAME = __file__ if FNAME.endswith('.pyc'): FNAME = FNAME[:-1] class FakeLoader: def __init__(self, lines): self._lines = lines def get_source(self, name): return self._lines fake_module = dict(__name__='fred', __loader__=FakeLoader(''.join(linecache.getlines(FNAME)))) test_code = namedtuple('code', ['co_filename', 'co_name']) test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals']) test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next']) class SyntaxTracebackCases(testtools.TestCase): # For now, a very minimal set of tests. I want to be sure that # formatting of SyntaxErrors works based on changes for 2.1. def get_exception_format(self, func, exc): try: func() except exc as value: return traceback.format_exception_only(exc, value)
workbook = xlsxwriter.Workbook('speaker_test_music.xlsx') worksheet = workbook.add_worksheet('asr-nlu-test') #using examples #worksheet.write('A1', 'trigger times') # write somethong into A1 #count = linecache.getline(filename,linenum) #extract something of a certain line #str = linecache.getlines(filename) #get the log information by a list style,str is a list #linenumbers=len(filename) #probably to judge how many lines the log file has #loops to read information from the log file into excel str_asr_result = linecache.getlines('asr_result.log') #calculating that how many lines the log has worksheet.write('A1','ASR_Result') for i in range(len(str_asr_result)+1): worksheet.write('A%s'%(i+1),'%s'%linecache.getline('asr_result.log',i)) #Write the lines into the corresponding cell of a certain sheet in sequence linecache.clearcache() #other operation about using linecache, somethong like clearing cache str_songName = linecache.getlines('songName.log') #calculating that how many lines the log has worksheet.write('B1','songName') for i in range(len(str_songName)+1): worksheet.write('B%s'%(i+1),'%s'%linecache.getline('songName.log',i)) #Write the lines into the corresponding cell of a certain sheet in sequence linecache.clearcache()
workbook = xlsxwriter.Workbook('speaker_test.xlsx') worksheet = workbook.add_worksheet('asr-nlu-test') #using examples #worksheet.write('A1', 'trigger times') # write somethong into A1 #count = linecache.getline(filename,linenum) #extract something of a certain line #str = linecache.getlines(filename) #get the log information by a list style,str is a list #linenumbers=len(filename) #probably to judge how many lines the log file has #loops to read information from the log file into excel str_asr_result = linecache.getlines('asr_result.log') #calculating that how many lines the log has worksheet.write('A1','ASR_Result') for i in range(len(str_asr_result)+1): worksheet.write('A%s'%(i+1),'%s'%linecache.getline('asr_result.log',i)) #Write the lines into the corresponding cell of a certain sheet in sequence linecache.clearcache() #other operation about using linecache, somethong like clearing cache str_nlu_domain = linecache.getlines('nlu_domain.log') #calculating that how many lines the log has worksheet.write('B1','NLU_Domain') for i in range(len(str_nlu_domain)+1): worksheet.write('B%s'%(i+1),'%s'%linecache.getline('nlu_domain.log',i)) #Write the lines into the corresponding cell of a certain sheet in sequence linecache.clearcache()