The Filer Class¶
-
class
helpers.filer.
Filer
(path: pathlib.Path, create: bool = True, delimiter: str = '\t')¶ Bases:
object
A class for gracefully handling file interactions with delimited data
Designed particularly for passing context in a CLI, it is a thin wrapper for many common file I/O actions, including reading, writing (both lines and columns), and deleting.
-
append
(rows: List[List[str]]) → None¶ Appends contents of rows to self.path
Note
This will not over-write the contents of the file, mirroring the modes of open()
- Parameters
rows (List[List[str]]) – A list of strings to write to self.path.
- Returns
None
Examples
>>> example.append([['f','g', 'h'], ['i', 'j', 'k']])
-
delete
(contains: str) → bool¶ Deletes all lines from self where contains in line
- Parameters
contains (str) – String to match for line deletion
- Returns
bool – True if successulf, false otherwise
Example
>>> example.delete('j') True
-
read
() → List[List[str]]¶ Read the lines of self.path
Note
Reads in all lines, so will suffer on large files
- Parameters
None
- Returns
List[List[str]] – A list of lines where each line is a list of column values
Examples
>>> example.read() [['ID', 'Rank', 'Date', 'Task'], ['f', 'g', 'h']]
-
sort
(cols: List[int], header: bool = False) → None¶ Sort the contents of self.path by columns
- Parameters
cols (List[int]) – List of column indices indicating what to sort by. Remember, Python is 0-indexed
header (bool) – Whether or not row 0 is a header. If True, row 0 is skipped for sorting
- Returns
None
Example
>>> example.sort([1, 2], header=False)
-
write
(rows: List[List[str]]) → None¶ Writes contents of rows to self.path.
Warning
If the file already has content, that will be overwritten! This mirrors the modes used by open()
- Parameters
rows (List[List[str]]) – A list of strings to write to self.path. rows[0] represents line 1, and rows[0][0] is line 1, column 1.
- Returns
None
Examples
>>> example.write([['a', 'b', 'c']])
-
write_col
(col: List[str], index: int = 0) → None¶ Writes contents of col to self.path at specified index
Warning
If the column already has content, that will be overwritten! This mirrors the modes used by open()
- Parameters
col (List[str]) – A list of strings to write to self.path. This should be the same length as self.length
index (int) – Which column to write at. Remember, Python is 0-indexed.
- Returns
None
- Raises
IndexError – When col has more or less items than self.length
Examples
>>> example.write_col(['d'], index=2)
-