我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用linecache.getlines()。
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec(compile(source, fn, 'single'), ns) inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(keepends=True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec(compile(source, fn, 'single'), ns) inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
def getlines(filename, module_globals=None): """Get the lines (as unicode) for a file from the cache. Update the cache if it doesn't contain an entry for this file already.""" filename = py3compat.cast_bytes(filename, sys.getfilesystemencoding()) lines = linecache.getlines(filename, module_globals=module_globals) # The bits we cache ourselves can be unicode. if (not lines) or isinstance(lines[0], py3compat.unicode_type): return lines readline = openpy._list_readline(lines) try: encoding, _ = openpy.detect_encoding(readline) except SyntaxError: encoding = 'ascii' return [l.decode(encoding, 'replace') for l in lines] # This is a straight copy of linecache.getline
def get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): """ Returns context_lines before and after lineno from file. Returns (pre_context_lineno, pre_context, context_line, post_context). """ source = None if loader is not None and hasattr(loader, 'get_source'): try: source = loader.get_source(module_name) except ImportError: source = None if source is not None: source = source.splitlines() if source is None: try: source = linecache.getlines(filename) except (OSError, IOError): return None, None, None if not source: return None, None, None lower_bound = max(0, lineno - context_lines) upper_bound = min(lineno + 1 + context_lines, len(source)) try: pre_context = [line.strip('\r\n') for line in source[lower_bound:lineno]] context_line = source[lineno].strip('\r\n') post_context = [line.strip('\r\n') for line in source[(lineno + 1):upper_bound]] except IndexError: # the file may have changed since it was loaded into memory return None, None, None return pre_context, context_line, post_context
def test(): f = [ x.replace('\n','') for x in linecache.getlines('doc/test.txt')] random_int = random.randint(0, len(f)-1) return f[random_int]
def user(): f = [ x.replace('\n','') for x in linecache.getlines('doc/user.txt')] random_int = random.randint(0, len(f)-1) return f[random_int]
def write_text(self, stream): if not path.exists(self.filename): stream.write("ERROR: %s\n" % self.filename) return stream.write("File: %s\n" % self.filename) stream.write("Name: %s\n" % self.name) stream.write("Total time: %g [sec]\n" % self.total_time) linecache.clearcache() lines = linecache.getlines(self.filename) if self.name != "<module>": lines = inspect.getblock(lines[self.firstlineno - 1:]) template = '%6s %9s %12s %-s' header = template % ("Line", "Hits", "Time", "Code") stream.write(header) stream.write("\n") stream.write("=" * len(header)) stream.write("\n") d = {} for i, code in zip(itertools.count(self.firstlineno), lines): timing = self.timings.get(i) if timing is None: d[i] = { "hits": "", "time": "", "code": code } else: d[i] = { "hits": timing.n_hits, "time": timing.total_time, "code": code } for i in sorted(d.keys()): r = d[i] stream.write(template % (i, r["hits"], r["time"], r["code"])) stream.write("\n")
def _fixed_getinnerframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:] # Helper function -- largely belongs to VerboseTB, but we need the same # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they # can be recognized properly by ipython.el's py-traceback-line-re # (SyntaxErrors have to be treated specially because they have no traceback)
def getlines(filename, module_globals=None): """ Deprecated since IPython 6.0 """ warn(("`IPython.utils.ulinecache.getlines` is deprecated since" " IPython 6.0 and will be removed in future versions."), DeprecationWarning, stacklevel=2) return linecache.getlines(filename, module_globals=module_globals)
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_no_ending_newline(self): self.addCleanup(support.unlink, support.TESTFN) with open(support.TESTFN, "w") as fp: fp.write(SOURCE_3) lines = linecache.getlines(support.TESTFN) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
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' for index, line in enumerate(open(filename)): self.assertEqual(line, getline(filename, index + 1)) # Check module loading for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' for index, line in enumerate(open(filename)): 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_memoryerror(self): lines = linecache.getlines(FILENAME) self.assertTrue(lines) def raise_memoryerror(*args, **kwargs): raise MemoryError with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines2 = linecache.getlines(FILENAME) self.assertEqual(lines2, lines) linecache.clearcache() with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines3 = linecache.getlines(FILENAME) self.assertEqual(lines3, []) self.assertEqual(linecache.getlines(FILENAME), lines)
def _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybe_start = lnum - 1 - context // 2 start = max(maybe_start, 0) end = start + context lines = linecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]
def format_outer_frames(context=5, stack_start=None, stack_end=None, ignore_ipython=True): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = inspect.getouterframes(inspect.currentframe()) output = list() for i, (frame, filename, line_no, func_name, lines, index) \ in enumerate(records): # Look inside the frame's globals dictionary for __file__, which should # be better. better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn if filename.endswith('.pyc'): filename = filename[:-4] + '.py' if ignore_ipython: # Hack to avoid printing the internals of IPython if (os.path.basename(filename) in ('iplib.py', 'py3compat.py') and func_name in ('execfile', 'safe_execfile', 'runcode')): break maybe_start = line_no - 1 - context // 2 start = max(maybe_start, 0) end = start + context lines = linecache.getlines(filename)[start:end] buf = list(records[i]) buf[LNUM_POS] = line_no buf[INDEX_POS] = line_no - 1 - start buf[LINES_POS] = lines output.append(tuple(buf)) return '\n'.join(format_records(output[stack_end:stack_start:-1]))
def _fixed_getinnerframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in zip(range(len(records)), aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = ulinecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:] # Helper function -- largely belongs to VerboseTB, but we need the same # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they # can be recognized properly by ipython.el's py-traceback-line-re # (SyntaxErrors have to be treated specially because they have no traceback)
def getlines(filename, module_globals=None): return linecache.getlines(filename, module_globals=module_globals)
def getline(filename, lineno, module_globals=None): lines = getlines(filename, module_globals) if 1 <= lineno <= len(lines): return lines[lineno-1] else: return ''
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 = support.findfile( entry + '.py') for index, line in enumerate(open(filename)): self.assertEqual(line, getline(filename, index + 1)) # Check module loading for entry in MODULES: filename = support.findfile( entry + '.py') for index, line in enumerate(open(filename)): 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 _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(file)[start:end] # pad with empty lines if necessary if maybeStart < 0: lines = (['\n'] * -maybeStart) + lines if len(lines) < context: lines += ['\n'] * (context - len(lines)) buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]
def format_outer_frames(context=5, stack_start=None, stack_end=None, ignore_ipython=True): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = inspect.getouterframes(inspect.currentframe()) output = list() for i, (frame, filename, line_no, func_name, lines, index) \ in enumerate(records): # Look inside the frame's globals dictionary for __file__, which should # be better. better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn if filename.endswith('.pyc'): filename = filename[:-4] + '.py' if ignore_ipython: # Hack to avoid printing the internals of IPython if (os.path.basename(filename) == 'iplib.py' and func_name in ('safe_execfile', 'runcode')): break maybeStart = line_no - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(filename)[start:end] # pad with empty lines if necessary if maybeStart < 0: lines = (['\n'] * -maybeStart) + lines if len(lines) < context: lines += ['\n'] * (context - len(lines)) buf = list(records[i]) buf[LNUM_POS] = line_no buf[INDEX_POS] = line_no - 1 - start buf[LINES_POS] = lines output.append(tuple(buf)) return '\n'.join(format_records(output[stack_end:stack_start:-1]))
def get_anno(path): f = open(path) d = {} cache_data = linecache.getlines(path) title = True for line in range(len(cache_data)): if title: title = False t = cache_data[line] continue ll = cache_data[line].strip().split('\t') key = tuple(ll[-8:-3]) d[key] = ll linecache.clearcache() return d, t
def add_genotype_from_vcf_to_annovar(vcf_f, anno_f, new_f, sample_file): anno_d, anno_title = get_anno(anno_f) nf = open(new_f, 'w') print 'get anno info' sg = data.SampleGroup(sample_file) cases = sg.cases_id ctrls = sg.ctrls_id if os.path.exists(vcf_f): cache_data = linecache.getlines(vcf_f) print 'get vcf info' for line in range(1, len(cache_data)): if cache_data[line][:2] == '##': continue elif cache_data[line][0] == '#': vcf_title = cache_data[line].strip().split('\t')[8:] + ['allele_count', 'sample_count', 'allele_freq'] ntl = anno_title.strip().split('\t') + vcf_title nt = '\t'.join(ntl) + '\n' nf.write(nt) continue ll = cache_data[line].strip().split('\t') key = tuple(ll[:5]) if key in anno_d: nl = anno_d[key]+ll[8:]+count_alle_freq(sample_info=ll[9:], sample_id=vcf_title[1:-3], cases=cases, ctrls=ctrls) nf.write('\t'.join(nl)+'\n') return else: print('the path [{}] is not exist!'.format(vcf_f))
def add_key(self, key="", key_file=""): if key != '' and not self.is_key_registered(key): print "add key \'{key}\'(the 1st 8 chars) to {file} ...".format(key=self.get_fingerprint_short(key), file=self.authorized_keys_file) with open(self.authorized_keys_file, 'a') as modified: modified.write(key.strip() + '\n') if os.path.exists(key_file): import linecache for line in linecache.getlines(key_file): self.add_key(key=line)