From 4a063752121b5ba1ca2530d390f9b6003464e91c Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Wed, 7 Sep 2022 12:31:18 -0700 Subject: [PATCH] stdlib: Add __repr__ to pystats For Statistics the value is returned. E.g.: ``` print(simstats.board.core.some_integer) > 5 ``` For Groups the names of the stats in that group are listed. E.g.: ``` print(stats.board.core) > [Group: [some_integer, another_stat, another_group]] ``` Change-Id: I94cea907608fba622f4fc141d5b22ac95d8cde40 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/63271 Tested-by: kokoro Maintainer: Bobby Bruce Reviewed-by: Bobby Bruce --- src/python/m5/ext/pystats/group.py | 14 +++++++++++++- src/python/m5/ext/pystats/statistic.py | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/python/m5/ext/pystats/group.py b/src/python/m5/ext/pystats/group.py index 6e2da87dfc..680a5d547a 100644 --- a/src/python/m5/ext/pystats/group.py +++ b/src/python/m5/ext/pystats/group.py @@ -56,7 +56,7 @@ class Group(SerializableStat): time_conversion: Optional[TimeConversion] = None, **kwargs: Dict[ str, Union["Group", Statistic, List["Group"], List["Statistic"]] - ] + ], ): if type is None: self.type = "Group" @@ -140,6 +140,15 @@ class Group(SerializableStat): pattern = regex yield from self.children(lambda _name: bool(pattern.search(_name))) + def _repr_name(self) -> str: + return "Group" + + def __repr__(self) -> str: + stats_list = [] + for key in self.__dict__: + stats_list.append(key) + return f"{self._repr_name()}: {stats_list}" + class Vector(Group): """ @@ -152,3 +161,6 @@ class Vector(Group): def __init__(self, scalar_map: Mapping[str, Scalar]): super().__init__(type="Vector", time_conversion=None, **scalar_map) + + def _repr_name(self) -> str: + return "Vector" diff --git a/src/python/m5/ext/pystats/statistic.py b/src/python/m5/ext/pystats/statistic.py index 446c9ba346..b018060ddc 100644 --- a/src/python/m5/ext/pystats/statistic.py +++ b/src/python/m5/ext/pystats/statistic.py @@ -56,6 +56,9 @@ class Statistic(ABC, SerializableStat): self.description = description self.datatype = datatype + def __repr__(self): + return str(self.value) + class Scalar(Statistic): """