Skip to content

Commit

Permalink
fix: col order now follow dict rule order and star rule must be expli…
Browse files Browse the repository at this point in the history
…citly added
  • Loading branch information
percevalw committed Dec 5, 2022
1 parent b193d39 commit 7c0960f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ logger_fields = {
"name": r"\1_r",
},
"duration": {"format": "{:.1f}", "name": "dur(s)"},
".*": True, # Any other field must be logged at the end
}


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rich-logger"
version = "0.2.1"
version = "0.3.0"
description = "Table logger using Rich"
authors = ["Perceval Wajsbürt <perceval.wajsburt@sorbonne-universite.fr>"]
license = "BSD 3-Clause"
Expand Down
12 changes: 8 additions & 4 deletions rich_logger/table_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def validate_rules(rules: Dict[str, Union[Rule, bool]]):


def get_last_matching_index(matchers: Sequence[str], name: str):
for i, matcher in reversed(list(enumerate(matchers))):
index = None
for i, (matcher, rule) in list(enumerate(matchers.items())):
if re.match(matcher, name):
return i
raise ValueError()
if rule is not True or index is None:
index = i
if index is None:
raise ValueError()
return index


def get_last_matching_value(matchers, name, field, default):
Expand All @@ -143,7 +147,7 @@ def get_last_matching_value(matchers, name, field, default):
if rule is False:
return matcher, False
elif rule is True:
return matcher, default
has_match = True
else:
field_value = getattr(rule, field)
has_match = True
Expand Down
44 changes: 23 additions & 21 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def test_simple(capsys):
printer.log({"step": 3, "col2": "ko", "col1": 4, "col4": "ok", "col5": 3})
captured = capsys.readouterr()
assert captured.out == (
"┏━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━┳━━━━━━\n"
"┃ step ┃ COLUMN2 ┃ COLUMN1 ┃ col5 ┃ col3 ┃\n"
"┡━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━╇━━━━━━\n"
"│ 1 │ │ 3 │ │\n"
"│ 2 │ ok │ │ │ 3.5 │\n"
"│ 3 │ ko │ 4 │ 3 │\n"
"└──────┴─────────┴─────────┴──────┴──────\n"
"┏━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━┓\n"
"┃ step ┃ COLUMN2 ┃ COLUMN1 ┃ col5 ┃\n"
"┡━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━┩\n"
"│ 1 │ │ 3 │ │\n"
"│ 2 │ ok │ │ │\n"
"│ 3 │ ko │ 4 │ 3 │\n"
"└──────┴─────────┴─────────┴──────┘\n"
)
assert captured.err == ""

Expand All @@ -51,6 +51,7 @@ def test_no_context(capsys):
"name": r"\1_r",
},
"duration": {"format": "{:.1f}", "name": "dur(s)"},
".*": True,
}

def optimization():
Expand Down Expand Up @@ -101,17 +102,18 @@ def optimization():
def test_tqdm(capsys):
logger_fields = {
"step": {},
"(.*)_precision": {
"(.*)_recall": {
"goal": "higher_is_better",
"format": "{:.4f}",
"name": r"\1_p",
"name": r"\1_r",
},
"(.*)_recall": {
"(.*)_precision": {
"goal": "higher_is_better",
"format": "{:.4f}",
"name": r"\1_r",
"name": r"\1_p",
},
"duration": {"format": "{:.1f}", "name": "dur(s)"},
".*": True,
}

def optimization():
Expand Down Expand Up @@ -144,18 +146,18 @@ def optimization():

assert captured.out == (
"┏━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━┓\n"
"┃ step ┃ task_ptask_r ┃ dur(s) ┃ test ┃\n"
"┃ step ┃ task_rtask_p ┃ dur(s) ┃ test ┃\n"
"┡━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━┩\n"
"│ 0 │ 0.0000 │ 0.0000 │ 0.2 │ 0 │\n"
"│ 1 │ 0.1000 │ 0.0000 │ 0.2 │ 1 │\n"
"│ 2 │ 0.2000 │ 0.0000 │ 0.2 │ 2 │\n"
"│ 3 │ 0.3000 │ 0.0000 │ 0.2 │ 3 │\n"
"│ 4 │ 0.4000 │ 0.1000 │ 0.2 │ 4 │\n"
"│ 5 │ 0.5000 │ 0.2000 │ 0.2 │ 5 │\n"
"│ 6 │ 0.4000 │ 0.3000 │ 0.2 │ 6 │\n"
"│ 7 │ 0.3000 │ 0.4000 │ 0.2 │ 7 │\n"
"│ 8 │ 0.2000 │ 0.5000 │ 0.2 │ 8 │\n"
"│ 9 │ 0.1000 │ 0.6000 │ 0.2 │ 9 │\n"
"│ 1 │ 0.0000 │ 0.1000 │ 0.2 │ 1 │\n"
"│ 2 │ 0.0000 │ 0.2000 │ 0.2 │ 2 │\n"
"│ 3 │ 0.0000 │ 0.3000 │ 0.2 │ 3 │\n"
"│ 4 │ 0.1000 │ 0.4000 │ 0.2 │ 4 │\n"
"│ 5 │ 0.2000 │ 0.5000 │ 0.2 │ 5 │\n"
"│ 6 │ 0.3000 │ 0.4000 │ 0.2 │ 6 │\n"
"│ 7 │ 0.4000 │ 0.3000 │ 0.2 │ 7 │\n"
"│ 8 │ 0.5000 │ 0.2000 │ 0.2 │ 8 │\n"
"│ 9 │ 0.6000 │ 0.1000 │ 0.2 │ 9 │\n"
"└──────┴────────┴────────┴────────┴──────┘\n"
" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 \n"
" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 \n"
Expand Down
1 change: 1 addition & 0 deletions tests/test_pytorch_lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def validation_epoch_end(self, outputs) -> None:
fields={
"epoch": True,
"step": True,
".*": True,
}
),
)
Expand Down

0 comments on commit 7c0960f

Please sign in to comment.