[PATCH 04/26] rv/rvgen: replace __len__() calls with len()

Wander Lairson Costa posted 26 patches 2 weeks, 5 days ago
There is a newer version of this series
[PATCH 04/26] rv/rvgen: replace __len__() calls with len()
Posted by Wander Lairson Costa 2 weeks, 5 days ago
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>
---
 tools/verification/rvgen/rvgen/automata.py |  2 +-
 tools/verification/rvgen/rvgen/dot2c.py    | 16 ++++++++--------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/tools/verification/rvgen/rvgen/automata.py b/tools/verification/rvgen/rvgen/automata.py
index 3e24313a2eaec..ed02d0c69b410 100644
--- a/tools/verification/rvgen/rvgen/automata.py
+++ b/tools/verification/rvgen/rvgen/automata.py
@@ -42,7 +42,7 @@ class Automata:
             raise AutomataError(f"not a dot file: {self.__dot_path}")
 
         model_name = ntpath.splitext(basename)[0]
-        if model_name.__len__() == 0:
+        if not model_name:
             raise AutomataError(f"not a dot file: {self.__dot_path}")
 
         return model_name
diff --git a/tools/verification/rvgen/rvgen/dot2c.py b/tools/verification/rvgen/rvgen/dot2c.py
index 78959d345c26e..0fb3617ad8ce9 100644
--- a/tools/verification/rvgen/rvgen/dot2c.py
+++ b/tools/verification/rvgen/rvgen/dot2c.py
@@ -86,14 +86,14 @@ class Dot2c(Automata):
     def get_minimun_type(self):
         min_type = "unsigned char"
 
-        if self.states.__len__() > 255:
+        if len(self.states) > 255:
             min_type = "unsigned short"
 
-        if self.states.__len__() > 65535:
+        if len(self.states) > 65535:
             min_type = "unsigned int"
 
-        if self.states.__len__() > 1000000:
-            raise AutomataError(f"Too many states: {self.states.__len__()}")
+        if len(self.states) > 1000000:
+            raise AutomataError(f"Too many states: {len(self.states)}")
 
         return min_type
 
@@ -149,12 +149,12 @@ class Dot2c(Automata):
         return buff
 
     def __get_max_strlen_of_states(self):
-        max_state_name = max(self.states, key = len).__len__()
-        return max(max_state_name, self.invalid_state_str.__len__())
+        max_state_name = len(max(self.states, key=len))
+        return max(max_state_name, len(self.invalid_state_str))
 
     def get_aut_init_function(self):
-        nr_states = self.states.__len__()
-        nr_events = self.events.__len__()
+        nr_states = len(self.states)
+        nr_events = len(self.events)
         buff = []
 
         maxlen = self.__get_max_strlen_of_states() + len(self.enum_suffix)
-- 
2.52.0
Re: [PATCH 04/26] rv/rvgen: replace __len__() calls with len()
Posted by Nam Cao 2 weeks, 2 days ago
Wander Lairson Costa <wander@redhat.com> writes:
> 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: Nam Cao <namcao@linutronix.de>
Re: [PATCH 04/26] rv/rvgen: replace __len__() calls with len()
Posted by Gabriele Monaco 2 weeks, 5 days ago
On Mon, 2026-01-19 at 17:45 -0300, Wander Lairson Costa wrote:
> 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>
> ---

Totally needed.

Reviewed-by: Gabriele Monaco <gmonaco@redhat.com>

Thanks,
Gabriele

>  tools/verification/rvgen/rvgen/automata.py |  2 +-
>  tools/verification/rvgen/rvgen/dot2c.py    | 16 ++++++++--------
>  2 files changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/verification/rvgen/rvgen/automata.py
> b/tools/verification/rvgen/rvgen/automata.py
> index 3e24313a2eaec..ed02d0c69b410 100644
> --- a/tools/verification/rvgen/rvgen/automata.py
> +++ b/tools/verification/rvgen/rvgen/automata.py
> @@ -42,7 +42,7 @@ class Automata:
>              raise AutomataError(f"not a dot file: {self.__dot_path}")
>  
>          model_name = ntpath.splitext(basename)[0]
> -        if model_name.__len__() == 0:
> +        if not model_name:
>              raise AutomataError(f"not a dot file: {self.__dot_path}")
>  
>          return model_name
> diff --git a/tools/verification/rvgen/rvgen/dot2c.py
> b/tools/verification/rvgen/rvgen/dot2c.py
> index 78959d345c26e..0fb3617ad8ce9 100644
> --- a/tools/verification/rvgen/rvgen/dot2c.py
> +++ b/tools/verification/rvgen/rvgen/dot2c.py
> @@ -86,14 +86,14 @@ class Dot2c(Automata):
>      def get_minimun_type(self):
>          min_type = "unsigned char"
>  
> -        if self.states.__len__() > 255:
> +        if len(self.states) > 255:
>              min_type = "unsigned short"
>  
> -        if self.states.__len__() > 65535:
> +        if len(self.states) > 65535:
>              min_type = "unsigned int"
>  
> -        if self.states.__len__() > 1000000:
> -            raise AutomataError(f"Too many states: {self.states.__len__()}")
> +        if len(self.states) > 1000000:
> +            raise AutomataError(f"Too many states: {len(self.states)}")
>  
>          return min_type
>  
> @@ -149,12 +149,12 @@ class Dot2c(Automata):
>          return buff
>  
>      def __get_max_strlen_of_states(self):
> -        max_state_name = max(self.states, key = len).__len__()
> -        return max(max_state_name, self.invalid_state_str.__len__())
> +        max_state_name = len(max(self.states, key=len))
> +        return max(max_state_name, len(self.invalid_state_str))
>  
>      def get_aut_init_function(self):
> -        nr_states = self.states.__len__()
> -        nr_events = self.events.__len__()
> +        nr_states = len(self.states)
> +        nr_events = len(self.events)
>          buff = []
>  
>          maxlen = self.__get_max_strlen_of_states() + len(self.enum_suffix)