exatomic.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 exatomic.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 (exatomic 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.

name

Data/file/misc name

Type:

str

description

Data/file/misc description

Type:

str

meta

Additional metadata as key, value pairs

Type:

dict

nrpint

Number of lines to display when printing

Type:

int

cursor

Line number position of the cusor (see find_next())

Type:

int

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

head(n=10)[source]

Display the top of the file.

Parameters:

n (int) – Number of lines to display

tail(n=10)[source]

Display the bottom of the file.

Parameters:

n (int) – Number of lines to display

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 use append().

Parameters:

lines (dict) – Dictionary of lines of form (lineno, string) pairs

remove_blank_lines()[source]

Remove all blank lines (blank lines are those with zero characters).

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:
  • strings (str) – Any number of strings to search for

  • keys_only (bool) – Only return keys

  • start (int) – Optional line to start searching on

  • stop (int) – Optional line to stop searching on

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)

replace(pattern, replacement)[source]

Replace all instances of a pattern with a replacement.

Parameters:
  • pattern (str) – Pattern to replace

  • replacement (str) – Text to insert

pandas_dataframe(start, stop, ncol, **kwargs)[source]

Returns the result of tab-separated pandas.read_csv on a subset of the file.

Parameters:
  • start (int) – line number where structured data starts

  • stop (int) – line number where structured data stops

  • ncol (int or list) – the number of columns in the structured data or a list of that length with column names

Returns:

pd.DataFrame – structured data

to_stream()[source]

Create an StringIO object from the current editor text.

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.

classmethod from_file(path, **kwargs)[source]

Create an editor instance from a file on disk.

classmethod from_stream(f, **kwargs)[source]

Create an editor instance from a file stream.

classmethod from_string(string, **kwargs)[source]

Create an editor instance from a string template.

exatomic.exa.core.editor.lines_from_file(path, as_interned=False, encoding=None)[source]

Create a list of file lines from a given filepath.

Parameters:
  • path (str) – File path

  • as_interned (bool) – List of “interned” strings (default False)

Returns:

strings (list) – File line list

exatomic.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

exatomic.exa.core.editor.lines_from_string(string, as_interned=False)[source]

Create a list of file lines from a given string.

Parameters:
  • string (str) – File string

  • as_interned (bool) – List of “interned” strings (default False)

Returns:

strings (list) – File line list