Skip to main content

Cache and colorize lines and file information which are generally Python program or Python Bytecode

Project description

Pypi Installs License Supported Python Versions

packagestatus

Synopsis

The pyficache module allows one to get any line from any file, caching lines of the file on first access to the file. Although the file may be any file, this package is more tailored to the case where the file is a Python script.

Here, the file is parsed to determine statement boundaries, and a copies of the file syntax-highlighted are also saved.

Also saved is file information, such as when the file was last modified and a SHA1 of the file. These are useful in determining if the file has changed and verifying the contents of the file.

By caching contents, access is sped up when small random sets of lines are read from a single file, in particular in a debugger, to show source lines.

A file path can be remapped to another path. This is useful, for example, when debugging remotely, and the remote file path may be different from the path on a local filesystem. In the trepan3k and trepan2 debuggers, eval and exec strings are saved in a temporary file and then the pseudo-filename <string> is remapped to that temporary file name.

Similarly, lines within a file can be remapped to other lines. This may be useful in preprocessors or template systems where one wants to make a correspondence between the template file and the expanded Python file as seen in a tool using that underlying Python file, such as a debugger or profiler.

Summary

import pyficache
filename = __file__ # e.g. '/tmp/myprogram'
 # return all lines of filename as an array
lines = pyficache.getlines(filename)

 # return line 6, and reload all lines if the file has changed.
line = pyficache.getline(filename, 6, {'reload_on_change': True})

# return line 6 syntax highlighted via pygments using style 'colorful'
line = pyficache.getline(filename, 6, {'style': 'colorful'})

pyficache.remap_file('/tmp/myprogram.py', 'another-name')
line_from_alias = pyficache.getline('another-name', 6)

assert __file__, pyficache.remove_remap_file('another-name')

# another-name is no longer an alias for /tmp/myprogram
assert None, pyficache.remove_remap_file('another-name')

# Clear cache for __file__
 pyficache.clear_file_cache(__file__)

# Clear all cached files.
pyficache.clear_file_cache()

# Check for modifications of all cached files.
pyficache.update_cache()

Credits

This is a port of my Ruby linecache module, which in turn is based on the Python linecache module.

xdis provides the cool stuff to figure out the lines containing Python statements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyficache-2.5.0.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyficache-2.5.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file pyficache-2.5.0.tar.gz.

File metadata

  • Download URL: pyficache-2.5.0.tar.gz
  • Upload date:
  • Size: 36.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyficache-2.5.0.tar.gz
Algorithm Hash digest
SHA256 99310caaf0f407c90f6beae37dfdba32c05981c7e35d7da008584b838308ac49
MD5 ddb8ec272f386ab9eca93487a7e0c564
BLAKE2b-256 1cfc41deb7077407e427ea34f2977979409f91f792f3683a30fe1eca031d2552

See more details on using hashes here.

File details

Details for the file pyficache-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: pyficache-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyficache-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b61861312bec9a5ca8ee6d38cb6708a078399bf0917b73b73f98dc88b7a5cbe0
MD5 a14369bcce4b02557e20e00cd68295e6
BLAKE2b-256 f24c139cc6aff8d8867e18f605fc07104b90de5305b33390e2979d38c6035692

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page