def test_clearcache(self): cached = [] for entry in TESTS: filename = os.path.join(TEST_PATH, entry) + '.py' cached.append(filename) linecache.getline(filename, 1) # Are all files cached? cached_empty = [fn for fn in cached if fn not in linecache.cache] self.assertEqual(cached_empty, []) # Can we clear the cache? linecache.clearcache() cached_empty = [fn for fn in cached if fn in linecache.cache] self.assertEqual(cached_empty, [])
def format_exception_only(self): """Format the exception part of the traceback. The return value is a generator of strings, each ending in a newline. Normally, the generator emits a single string; however, for SyntaxError exceptions, it emites several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the output. """ if self.exc_type is None: yield _format_final_exc_line(None, self._str) return stype = getattr(self.exc_type, '__qualname__', self.exc_type.__name__) smod = u(self.exc_type.__module__) if smod not in ("__main__", "builtins", "exceptions"): stype = smod + u('.') + stype if not issubclass(self.exc_type, SyntaxError): yield _format_final_exc_line(stype, self._str) return # It was a syntax error; show exactly where the problem was found. filename = _some_fs_str(self.filename) or u("<string>") lineno = str(self.lineno) or u('?') yield u(' File "{0}", line {1}\n').format(filename, lineno) badline = None if self.text is not None: if type(self.text) is bytes: # Not decoded - get the line via linecache which will decode # for us. if self.lineno: badline = linecache.getline(filename, int(lineno)) if not badline: # But we can't for some reason, so fallback to attempting a # u cast. badline = u(self.text) else: badline = self.text offset = self.offset if badline is not None: yield u(' {0}\n').format(badline.strip()) if offset is not None: caretspace = badline.rstrip('\n') offset = min(len(caretspace), offset) - 1 caretspace = caretspace[:offset].lstrip() # non-space whitespace (likes tabs) must be kept for alignment caretspace = ((c.isspace() and c or ' ') for c in caretspace) yield u(' {0}^\n').format(''.join(caretspace)) msg = self.msg or u("<no detail available>") yield u("{0}: {1}\n").format(stype, msg)
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() str_songId = linecache.getlines('songId.log') #calculating that how many lines the log has worksheet.write('C1','songId') for i in range(len(str_songId)+1):
def line(self): if self._line is None: self._line = linecache.getline(self.filename, self.lineno).strip() return self._line
def readrandom(): with open("keywords.txt","r")as f: count = len(f.readlines()) num =random.randint(1,count) keyword =linecache2.getline("keywords.txt",num) return str(keyword)
def readrandom(): with open("keywords.txt", "r") as f: count = len(f.readlines()) num = random.randint(1, count) keyword = linecache2.getline("keywords.txt", num) return str(keyword)
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() str_nlu_reply = linecache.getlines('nlu_reply.log') #calculating that how many lines the log has