exa.core.editor module¶
Editor¶
Text-editor-like functionality for programatically manipulating raw text input and output data and converting this data into container objects. This class does not behave like a fully fledged text editor but does have some basic find, replace, insert, etc. functionality.
-
class
exa.core.editor.
Editor
(path_stream_or_string, as_interned=False, nprint=30, name=None, description=None, meta=None, encoding=None, ignore=False)[source]¶ Bases:
object
An editor is a representation of a text file on disk that can be programmatically manipulated.
Text lines are stored in memory; no files remain open. This class does not strive to be a fully fledged text editor rather a base class for converting input and output data from text on disk to some type of (exa framework) container object (and vice versa).
>>> template = "Hello World!\nHello {user}" >>> editor = Editor(template) >>> editor[0] 'Hello World!' >>> len(editor) 2 >>> del editor[0] >>> len(editor) 1 >>> editor.write(fullpath=None, user='Alice') Hello Alice
Tip
Editor line numbers use a 0 base index. To increase the number of lines displayed by the repr, increase the value of the nprint attribute.
Warning
For large text with repeating strings be sure to use the as_interned argument.
-
cursor
¶ Line number position of the cusor (see
find_next()
)- Type
-
property
log
¶
-
write
(path=None, *args, **kwargs)[source]¶ Perform formatting and write the formatted string to a file or stdout.
Optional arguments can be used to format the editor’s contents. If no file path is given, prints to standard output.
- Parameters
path (str) – Full file path (default None, prints to stdout)
*args – Positional arguments to format the editor with
**kwargs – Keyword arguments to format the editor with
-
format
(*args, **kwargs)[source]¶ Format the string representation of the editor.
- Parameters
inplace (bool) – If True, overwrite editor’s contents with formatted contents
-
append
(lines)[source]¶ - Parameters
lines (list) – List of line strings to append to the end of the editor
-
prepend
(lines)[source]¶ - Parameters
lines (list) – List of line strings to insert at the beginning of the editor
-
insert
(lines=None)[source]¶ Insert lines into the editor.
Note
To insert before the first line, use
preappend()
(or key 0); to insert after the last line useappend()
.- Parameters
lines (dict) – Dictionary of lines of form (lineno, string) pairs
-
delete_lines
(lines)[source]¶ Delete all lines with given line numbers.
- Parameters
lines (list) – List of integers corresponding to line numbers to delete
-
find
(*strings, **kwargs)[source]¶ Search the entire editor for lines that match the string.
string = '''word one word two three''' ed = Editor(string) ed.find('word') # [(0, "word one"), (1, "word two")] ed.find('word', 'three') # {'word': [...], 'three': [(2, "three")]}
- Parameters
- Returns
results – If multiple strings searched a dictionary of string key, (line number, line) values (else just values)
-
find_next
(*strings, **kwargs)[source]¶ From the editor’s current cursor position find the next instance of the given string.
- Parameters
strings (iterable) – String or strings to search for
- Returns
tup (tuple) – Tuple of cursor position and line or None if not found
Note
This function cycles the entire editor (i.e. cursor to length of editor to zero and back to cursor position).
-
regex
(*patterns, **kwargs)[source]¶ Search the editor for lines matching the regular expression. re.MULTILINE is not currently supported.
- Parameters
patterns – Regular expressions to search each line for
keys_only (bool) – Only return keys
flags (re.FLAG) – flags passed to re.search
- Returns
results (dict) – Dictionary of pattern keys, line values (or groups - default)
-
pandas_dataframe
(start, stop, ncol, **kwargs)[source]¶ Returns the result of tab-separated pandas.read_csv on a subset of the file.
-
property
variables
¶ Display a list of templatable variables present in the file.
Templating is accomplished by creating a bracketed object in the same way that Python performs string formatting. The editor is able to replace the placeholder value of the template. Integer templates are positional arguments.
-
-
exa.core.editor.
lines_from_file
(path, as_interned=False, encoding=None)[source]¶ Create a list of file lines from a given filepath.
-
exa.core.editor.
lines_from_stream
(f, as_interned=False)[source]¶ Create a list of file lines from a given file stream.
- Parameters
f (io.TextIOWrapper) – File stream
as_interned (bool) – List of “interned” strings (default False)
- Returns
strings (list) – File line list