Skip to content

Commit b70bc5c

Browse files
walacglemco
authored andcommitted
rv/rvgen: replace __len__() calls with len()
Replace all direct calls to the __len__() dunder method with the idiomatic len() built-in function across the rvgen codebase. This change eliminates a Python anti-pattern where dunder methods are called directly instead of using their corresponding built-in functions. The changes affect nine instances across two files. In automata.py, the empty string check is further improved by using truthiness testing instead of explicit length comparison. In dot2c.py, all length checks in the get_minimun_type, __get_max_strlen_of_states, and get_aut_init_function methods now use the standard len() function. Additionally, spacing around keyword arguments has been corrected to follow PEP 8 guidelines. Direct calls to dunder methods like __len__() are discouraged in Python because they bypass the language's abstraction layer and reduce code readability. Using len() provides the same functionality while adhering to Python community standards and making the code more familiar to Python developers. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Reviewed-by: Gabriele Monaco <gmonaco@redhat.com> Reviewed-by: Nam Cao <namcao@linutronix.de> Link: https://lore.kernel.org/r/20260223162407.147003-5-wander@redhat.com Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
1 parent 908f377 commit b70bc5c

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

tools/verification/rvgen/rvgen/automata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __get_model_name(self) -> str:
8282
raise AutomataError(f"not a dot file: {self.__dot_path}")
8383

8484
model_name = ntpath.splitext(basename)[0]
85-
if model_name.__len__() == 0:
85+
if not model_name:
8686
raise AutomataError(f"not a dot file: {self.__dot_path}")
8787

8888
return model_name

tools/verification/rvgen/rvgen/dot2c.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def format_envs_enum(self) -> list[str]:
9696
def get_minimun_type(self) -> str:
9797
min_type = "unsigned char"
9898

99-
if self.states.__len__() > 255:
99+
if len(self.states) > 255:
100100
min_type = "unsigned short"
101101

102-
if self.states.__len__() > 65535:
102+
if len(self.states) > 65535:
103103
min_type = "unsigned int"
104104

105-
if self.states.__len__() > 1000000:
106-
raise AutomataError(f"Too many states: {self.states.__len__()}")
105+
if len(self.states) > 1000000:
106+
raise AutomataError(f"Too many states: {len(self.states)}")
107107

108108
return min_type
109109

@@ -159,12 +159,12 @@ def format_aut_init_envs_string(self) -> list[str]:
159159
return buff
160160

161161
def __get_max_strlen_of_states(self) -> int:
162-
max_state_name = max(self.states, key = len).__len__()
163-
return max(max_state_name, self.invalid_state_str.__len__())
162+
max_state_name = len(max(self.states, key=len))
163+
return max(max_state_name, len(self.invalid_state_str))
164164

165165
def get_aut_init_function(self) -> str:
166-
nr_states = self.states.__len__()
167-
nr_events = self.events.__len__()
166+
nr_states = len(self.states)
167+
nr_events = len(self.events)
168168
buff = []
169169

170170
maxlen = self.__get_max_strlen_of_states() + len(self.enum_suffix)

0 commit comments

Comments
 (0)