Skip to content

Commit d83b891

Browse files
committed
add ability to replace strings when loading
1 parent 7bc4510 commit d83b891

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

mp_reader/analyzer.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def _key(x: Field | Base | ChildAllocStats):
580580
def do_load(
581581
input_file: Path,
582582
strip_from_strings: list[str] | None,
583+
replace_strings: list[tuple[str, str]] | None = None,
583584
) -> OutputRecord:
584585
from .loader import load_from_file
585586

@@ -592,6 +593,10 @@ def do_load(
592593
for i in range(len(record.strtab)):
593594
record.strtab[i] = record.strtab[i].replace(s, "")
594595

596+
if replace_strings:
597+
for find, repl in replace_strings:
598+
for i in range(len(record.strtab)):
599+
record.strtab[i] = record.strtab[i].replace(find, repl)
595600
return record
596601

597602

@@ -683,9 +688,25 @@ def type_stats(
683688
help="Strip a substring from all the strings in the strtab. Useful for cleaning up output"
684689
),
685690
] = None,
691+
replace: Annotated[
692+
list[str] | None,
693+
typer.Option(help="String to find (for replacement)"),
694+
] = None,
695+
replace_with: Annotated[
696+
list[str] | None,
697+
typer.Option(help="String to replace with (defaults to ...)")
698+
] = None,
686699
filter_peak: Annotated[bool, typer.Option(help="Filter for peak usage")] = False,
687700
):
688-
record = do_load(input_file, strip_from_strings)
701+
replace_strings: None | list[tuple[str, str]] = None
702+
if replace is not None:
703+
if replace_with is None:
704+
replace_with = ['...' for _ in replace]
705+
706+
replace_strings = list(
707+
zip(replace, replace_with)
708+
)
709+
record = do_load(input_file, strip_from_strings, replace_strings)
689710

690711
events: list[OutputEvent] = (
691712
record.peak_free_events() if filter_peak else record.free_events()

0 commit comments

Comments
 (0)