Skip to content

Commit

Permalink
remove tabulate and use rich for repr
Browse files Browse the repository at this point in the history
  • Loading branch information
Eiuyc committed Feb 11, 2023
1 parent 1556ecf commit 736e5cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
30 changes: 22 additions & 8 deletions mmengine/registry/registry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
import io
import logging
import sys
from collections.abc import Callable
from contextlib import contextmanager
from importlib import import_module
from typing import Any, Dict, Generator, List, Optional, Tuple, Type, Union

from tabulate import tabulate
from rich.console import Console
from rich.table import Table

from mmengine.config.utils import MODULE2PACKAGE
from mmengine.utils import is_seq_of
Expand Down Expand Up @@ -122,13 +124,25 @@ def __contains__(self, key):
return self.get(key) is not None

def __repr__(self):
table_headers = ['Names', 'Objects']
table = tabulate(
self._module_dict.items(),
headers=table_headers,
tablefmt='fancy_grid')
format_str = f'Registry of {self._name}:\n' + table
return format_str
table = Table(title=f'Registry of {self._name}')
table.add_column('Names', justify='right', style='cyan')
table.add_column('Objects', justify='right', style='green')

# In table header, len('Names') == 5 and len('Objects') == 7
max_name_width = 5
max_obj_width = 7
for name, obj in self._module_dict.items():
max_name_width = max(max_name_width, len(name))
max_obj_width = max(max_obj_width, len(str(obj)))
table.add_row(name, str(obj))

# number 3 is for table border
table_width = max_name_width + max_obj_width + 3
with io.StringIO() as sio:
console = Console(file=sio, width=table_width)
console.print(table)
table_str = sio.getvalue()
return table_str

@staticmethod
def infer_scope() -> str:
Expand Down
1 change: 0 additions & 1 deletion requirements/runtime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ numpy
pyyaml
regex;sys_platform=='win32'
rich
tabulate
termcolor
yapf
18 changes: 8 additions & 10 deletions tests/test_registry/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright (c) OpenMMLab. All rights reserved.
import io
import time

import pytest
from tabulate import tabulate
from rich.console import Console

from mmengine.config import Config, ConfigDict # type: ignore
from mmengine.registry import (DefaultScope, Registry, build_from_cfg,
Expand Down Expand Up @@ -474,15 +475,12 @@ class BritishShorthair:
class Munchkin:
pass

item_prefix = "<class 'test_registry.TestRegistry.test_repr.<locals>."
table_content = [
('BritishShorthair', item_prefix + "BritishShorthair'>"),
('Munchkin', item_prefix + "Munchkin'>"),
]
table = tabulate(
table_content, headers=['Names', 'Objects'], tablefmt='fancy_grid')
repr_str = 'Registry of cat:\n'
repr_str += table
with io.StringIO() as sio:
console = Console(file=sio, width=16 + 72 + 3)
console.print(CATS)
repr_str = sio.getvalue()
assert repr_str[-1] == '\n'
repr_str = repr_str[:-1]
assert repr(CATS) == repr_str


Expand Down

0 comments on commit 736e5cd

Please sign in to comment.