Detailed description of linecache module loading and cache file content,

Source: Internet
Author: User

Detailed description of linecache module loading and cache file content,

Linecache Module

I came into contact with the linecache module because when I read the source code of attrs two days ago, I saw the internal code referencing this module to simulate a false file. I read the source code of this module with a question, this is actually the case. There are not many codes. Let's summarize them here.

The linecache module can read files and cache the files to facilitate subsequent reading. This module was originally designed to read the source code of the Python module. Therefore, when a file name is not in the specified path, the module tries to read the file through the search path.

Interface

In the linecache module, the _ all _ parameter only provides the getline, javasache, and checkcache interfaces. However, you can use more than these interfaces. I will introduce all interfaces one by one.

Linecache. getline (filename, lineno, module_globals = None)

Obtain a row of a specified file. filename specifies the file name, lineno specifies the row number, and module_globals is used to specify the context of the module, the last parameter is actually passed to linecache. in updatecache (), it is used to try to use _ loader _ to load files. Generally, the last parameter is not used. Ignore this parameter. If the row number is smaller than one or greater than the maximum row number of the file, the function returns an empty string.

Linecache. Sort AchE ()

Clear all caches. Note that all caches are cleared.

Linecache. checkcache (filename = None)

This function is used to check the cache. If the file size or modification time changes, the original cache of the file will be deleted. If the file is loaded lazily, it will remain unchanged. When filename is None, check all files in the cache.

The following interface is not written into the module _ all _ parameter.

Linecache. lazycache (filename, module_globals)

Lazy loading is used for specified files. If the lazy loading is enabled, the file content is loaded into the memory only when the interface for obtaining the content is called. This can avoid unnecessary file IO. The return value is a Boolean value. True is returned when the load is successful. If the file content has been actually loaded into the memory or fails to be loaded, False is returned.

The module_globals parameter of this function is required. In fact, it is used to input the context of the module corresponding to the file to be loaded. For example, if the linecache module is loaded, linecache is input. _ dict _ or vars (linecache ). Then, the function obtains the get_source function of _ loader _ according to the context and saves it to the cache. This function is added after Python3.5.

Linecache. updatecache (filename, module_globals = None)

This function is the core of the entire module and is used to update the File Cache and return the file content. If an error occurs in any stage of the function, an empty list is returned.

For normal files, use the tokenize. open () function internally to open the file, detect the file encoding and open the file using the detected encoding, if the missing encoding is by default using the UTF-8. If the file cannot be opened in the specified path, use the path specified by sys. path to try to load the file. If \ n is not included in the last line of the file content, \ n is automatically added to the last character.

For files that are loaded in a lazy manner, call the get_source function saved during lazy loading to obtain the file content.

Note: After opening a file, linecache uses readlines to load all the file content at a time. Therefore, problems may occur when there are many files or the file is too large. Therefore, you should exercise caution when using linecache.

Linecache. getlines (filename, module_globals = None)

Obtain all the content of the file. If the file is not loaded yet or it is loaded lazily, linecache. updatecache () is called to load the file content. If MemoryError occurs, the cache is cleared. Linecache. getline () actually calls this function internally.

Linecache. cache

This is a dictionary where the cache of all files exists. The dictionary Key is the filename you pass in when reading, and Value is a tuple that stores the file size, modification time, content, and name, when the file is loaded lazily, It is the get_source function corresponding to _ loader.

Summary

The Python standard library has many built-in basic modules that you will not notice at ordinary times, but there is always some other code that will depend on these infrastructure. For example, pdb and traceback in the standard library are useful to the linecache module. In fact, there are a lot of code that can be used. It is a pleasant surprise to read the source code a lot at ordinary times.

The above is all the details about linecache module loading and cache file content. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.