Python Linecache Source code

Source: Internet
Author: User
Tags float number

The source code of linecache.py from Python 2.0 are here.

 from Import *

Get File Information

>>> Filestat = Os.stat ('README. TXT')>>> filestatnt.stat_result (st_mode=33206, st_ino=0l, st_dev=0, St_ Nlink=0, St_uid=0, St_gid=0, st_size=2623l, st_atime=1450607830l, st_mtime=1398844440l, St_ctime=1398844440L) >>> filestat.st_mtimeimport  statfromimport *>> > filestat[st_mtime]1398844440L

Notice Filesta.st_mtime is a float number and filestat[st_mtime] is a long number which is strange.

Each cache is a entry are a dictionary with Key=filename value= (size, mtime, lines, Fullnames) (line 91)

Cache is only updated when a cache miss occurs. (line 39)

Function Checkcache () is not called automatically and it seems, the user should call this function manually.

Source Code

1 """Cache lines from files.2 3 This is intended to read lines from modules imported--hence if a filename4 is not found, it would look down the module search path for a file by5 that name.6 """7 8 ImportSYS9 ImportOSTen  fromStatImport* One  A defgetline (filename, Lineno): -Lines =getlines (filename) -     if1 <= Lineno <=Len (lines): the         returnLines[lineno-1] -     Else: -         return "' -  +  - #The cache +  Acache = {}#The cache at  -  - defClearCache (): -     """Clear the cache entirely.""" -  -     GlobalCache inCache = {} -  to  + defgetlines (filename): -     """Get the lines for a file from the cache. the Update The cache if it doesn ' t contain an entry for this file already.""" *  $     ifcache.has_key (filename):Panax Notoginseng         returnCache[filename][2] -     Else: the         returnupdatecache (filename) +  A  the defCheckcache (): +     """Discard Cache entries that is out of date. - (this isn't checked upon each call!)""" $  $      forFileNameinchCache.keys (): -Size, Mtime, lines, FullName =Cache[filename] -         Try: theStat =Os.stat (FullName) -         exceptOs.error:Wuyi             delCache[filename] the             Continue -         ifSize <> Stat[st_size]orMtime <>Stat[st_mtime]: Wu             delCache[filename] -  About  $ defupdatecache (filename): -     """Update A cache entry and return its list of lines. - If something ' s wrong, print a message, discard the cache entry, - And return an empty list.""" A  +     ifcache.has_key (filename): the         delCache[filename] -     if  notFileNameorFilename[0] + filename[-1] = ='<>': $         return [] theFullName =filename the     Try: theStat =Os.stat (FullName) the     exceptos.error, msg: -         #Try looking through the module search path inbasename = os.path.split (filename) [1] the          forDirNameinchSys.path: theFullName =Os.path.join (dirname, basename) About             Try: theStat =Os.stat (FullName) the                  Break the             exceptOs.error: +                 Pass -         Else: the             #No LuckBayi ## print ' * * * * cannot stat ', filename, ': ', msg the             return [] the     Try: -fp = open (FullName,'R') -Lines =Fp.readlines () the fp.close () the     exceptIOError, msg: the ## print ' * * * * cannot open ', FullName, ': ', msg the         return [] -Size, Mtime =Stat[st_size], Stat[st_mtime] theCache[filename] =size, Mtime, lines, fullname the     returnLines

Python Linecache Source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.