Skip to content

Commit b2bb144

Browse files
committed
Minor refactoring
added __str__ to CaptureFile
1 parent ed69edb commit b2bb144

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

CaptureFile/CaptureFile.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def __post_init__(
109109
self._new_file(initial_metadata)
110110
self.open(to_write)
111111

112+
def __str__(self):
113+
if self._file:
114+
status = f'opened for {"writing" if self.to_write else "reading"}'
115+
else:
116+
status = "currently closed but last seen"
117+
return f'"{self._file_name}" {status} with {self._record_count:,} records'
118+
112119
def open(self, to_write: bool = False):
113120
"""Opens this CaptureFile instance for reading or writing, depending on
114121
the value of `to_write`.
@@ -655,10 +662,11 @@ def record_at(self, record_number: int, /) -> Record:
655662
)
656663

657664
# skip nodes as long as path follows rightmost nodes.
658-
while (child_index := next(root_to_leaf_path)) == len(
659-
(current_rightmost_node := next(root_to_leaf_rightmost_nodes)).children
665+
for child_index, current_rightmost_node in zip(
666+
root_to_leaf_path, root_to_leaf_rightmost_nodes
660667
):
661-
pass
668+
if child_index != len(current_rightmost_node.children):
669+
break
662670

663671
# get first persistent child's data cooridnates. This child will refer
664672
# to either the record or the root of a perfect sub-tree of which no
@@ -1393,9 +1401,7 @@ def leaf_to_root_path(position: int, height: int, fan_out: int, /) -> List[int]:
13931401
root."""
13941402

13951403
path = [0] * height
1396-
13971404
for i in range(height):
1398-
position, child_index = divmod(position, fan_out)
1399-
path[i] = child_index
1405+
position, path[i] = divmod(position, fan_out)
14001406

14011407
return path

tests/test_captureFile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ def test_new_file():
1616
assert cf.record_count() == 0
1717
cf.close()
1818

19+
def test_str():
20+
with CaptureFile(file_name_1, to_write=True, force_new_empty_file=True) as cf:
21+
for i in range(1, 10001):
22+
cf.add_record(f"Hey this is my record {i:,}")
23+
LOGGER.info(str(cf))
24+
cf.close()
25+
LOGGER.info(str(cf))
26+
cf.open()
27+
LOGGER.info(str(cf))
28+
1929

2030
def test_record_out_of_range():
2131
cf = CaptureFile(file_name_1, to_write=True, force_new_empty_file=True)

0 commit comments

Comments
 (0)