padl.dumptools.sourceget
Module for getting source-code strings of various things (source files, modules, ipython cells). In addition, the module maintains a cache of source-strings in which the values can be modified.
Use get_source to get source-strings given source filenames or ipython cell ids. get_module_source allows to get source of modules.
Both will try to get the source-strings from the replace_cache, which can contain modified versions of the source-strings.
To add a modified source-string to the replace_cache, use put_into_cache.
- class padl.dumptools.sourceget.ReplaceString(string: str, repl: str, from_line: int, to_line: int, from_col: int, to_col: int)
A string with a replaced section.
Has an attribute original which gives the original string (pre-replacing).
- Parameters
string – The original string.
repl – The string to insert at the specified location.
from_line – The first line to replace in.
to_line – The last line to replace in.
from_col – The first col on from_line to replace.
to_col – The last col on to_line to replace.
- cut(from_line: int, to_line: int, from_col: int, to_col: int)
Cut and return the resulting sub-ReplaceString.
- class padl.dumptools.sourceget.ReplaceStrings(rstrings: List[padl.dumptools.sourceget.ReplaceString])
A collection of replacestrings with different replacements in an original string.
- Parameters
rstrings – A list of ReplaceString`s with the same `original and non-overlapping replace locations.
- cut(from_line, to_line, from_col, to_col)
Cut and return the resulting sub-ReplaceStrings.
- padl.dumptools.sourceget.cut(string: str, from_line: int, to_line: int, from_col: int, to_col: int) str
Cut a string (can be a normal str, a ReplaceString or a ReplaceStrings) and return the resulting substring.
Example:
Given a string,
- “xxxxxxxxxxx
xxxxxAXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXBxxxx xxxx”
, to cut a substring from A to B, give as from_line the line of A (1) and to_line the line of B (2). from_col determines the position of A within the line (5) and to_col determines the position of B (13). The result would be:
- “AXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXB”
If used with a ReplaceString, the result will be a ReplaceString with original and replacement at the expected positions.
- Parameters
string – The input string.
from_line – The first line to include.
to_line – The last line to include.
from_col – The first col on from_line to include.
to_col – The last col on to_line to include.
- Returns
The cut-out string.
- padl.dumptools.sourceget.get_module_source(module: module, use_replace_cache: bool = True) str
Get the source code of a module.
If use_replace_cache, try getting the source from the “replace_cache”, which can contain explicit replacements of the original source strings.
- padl.dumptools.sourceget.get_source(filename: str, use_replace_cache: bool = True) str
Get source from filename.
The filename is as in the code object gotten from an inspect.frame, this can be “<ipython input-…>” in which case the source is taken from the ipython cache.
If use_replace_cache, try getting the source from the “replace_cache”, which can contain explicit replacements of the original source strings.
- padl.dumptools.sourceget.original(string: str) str
Get either the original of a ReplaceString or a string.
- padl.dumptools.sourceget.put_into_cache(key, source: str, repl: str, *loc)
Put a string into the “replace_cache”.
Specify the original string, a replacing string and the insertion replacement.
Example:
>>> put_into_cache('mykey', 'f(value(x))', 'CONSTANT', 0, 0, 2, 9) >>> x = replace_cache['mykey'] >>> isinstance(x, ReplaceStrings) True >>> x 'f(CONSTANT))' >>> x.original 'f(value(x))'
- Parameters
key – The key for the cache dict.
source – The source.
repl – The string new inserted / replaced part.
*loc –
The location where repl is to be inserted (give as from_line, to_line, from_col, to_col).