cctk.LazyLineObject¶
- class cctk.LazyLineObject(file, start, end)[source]¶
Bases:
object
Instead of storing
lines
as an array, this object can be used. It reduces the memory usage drastically! It looks up lines only when needed.Methods
__init__
(file, start, end)find_parameter
(parameter, expected_length, ...)- param parameter:
test to search for
search_for_block
(start, end[, count, join, ...])Search through a file (lines) and locate a block starting with "start" (inclusive) and ending with "end" (exclusive).
- find_parameter(parameter, expected_length, which_field, split_on=None, cast_to_float=True)[source]¶
- Parameters:
parameter (string) – test to search for
expected_length (int) – how many fields there should be
which_field (int or list) – which field(s) the parameter is (zero-indexed)
split_on (str) – additional non-space field on which to split
cast_to_float (Bool) – whether or not to cast extracted value to float
- Returns:
a list of all the extracted values
- search_for_block(start, end, count=1, join=' ', max_len=1000, format_line=None)[source]¶
Search through a file (lines) and locate a block starting with “start” (inclusive) and ending with “end” (exclusive).
- Parameters:
start (str) – a pattern that matches the start of the block (can contain special characters)
end (str) – a pattern that matches the end of the block (can contain special characters) -
None
removes this (so a selection ofmax_lines
is guaranteed)count (int) – how many matches to search for
join (str) – spacer between lines
max_len (int) – maximum length of matches (to prevent overflow)
format_line (function) – function to perform to each line before adding to match (e.g. remove leading space)
- Returns:
a single match (str) if count == 1 or a list of matches (str) if count > 1.