All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rteval: Add __contains__ in rtevalConfig
@ 2021-07-29 22:07 John Kacur
  2021-07-30  0:54 ` Jeff Epler
  0 siblings, 1 reply; 5+ messages in thread
From: John Kacur @ 2021-07-29 22:07 UTC (permalink / raw)
  To: RT; +Cc: Clark Williams, John Kacur, Atsushi Nemoto

Add the __contains__ function to the rtevalCfgSection class  to make "in"
function correctly.

For example in cyclictest.py, self.__cfg is not a dictionary, it is an
instance of the rtevalCfgSection class, therefore
key in self.__cfg
does not work as expected. In order to make sure this is fixed
everywhere, instead of comparing to self.__cfg.keys() everywhere this is
used, implement the __contains__ function so that "in" works as
expected.

This bug was introduced by the commit fd3b732f714d ("rteval: 2to3
transformations")

Reported-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
 rteval/rtevalConfig.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py
index 578aaa3ab58f..b6fccc45ab7f 100644
--- a/rteval/rtevalConfig.py
+++ b/rteval/rtevalConfig.py
@@ -126,6 +126,10 @@ class rtevalCfgSection:
             return self.__cfgdata[key]
         return None
 
+    def __contains__(self, key):
+        if key in self.__cfgdata.keys():
+            return self.__cfgdata[key]
+        return None
 
     def items(self):
         return list(self.__cfgdata.items())
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rteval: Add __contains__ in rtevalConfig
  2021-07-29 22:07 [PATCH] rteval: Add __contains__ in rtevalConfig John Kacur
@ 2021-07-30  0:54 ` Jeff Epler
  2021-07-30  3:04   ` John Kacur
  0 siblings, 1 reply; 5+ messages in thread
From: Jeff Epler @ 2021-07-30  0:54 UTC (permalink / raw)
  To: John Kacur; +Cc: RT, Clark Williams, Atsushi Nemoto

On Thu, Jul 29, 2021 at 06:07:13PM -0400, John Kacur wrote:
> Add the __contains__ function to the rtevalCfgSection class  to make "in"
> function correctly.

Thank you.  A possible correction:

I believe the correct implementation (to delegate the 'in' operation to
the dictionary-like object self.__cfgdata) is

    def __contains__(self, key):
        return key in self.__cfgdata

I mocked a bit of rtevalCfgSection with your implementation:
	class rtevalCfgSection:
	    def __init__(self, cfgdata):
		self.__cfgdata = cfgdata

	    def __contains__(self, key):
		if key in self.__cfgdata.keys():
		    return self.__cfgdata[key]
		return None

and then tried it with some carefully chosen values

>>> d = {1: 'x', 2: 'y', 3: None, 4: False}
>>> r = rtevalConfig.rtevalCfgSection(d)
>>> 1 in d, 1 in r
(True, True)
>>> 9 in d, 9 in r
(False, False)
>>> 3 in d, 3 in r
(True, False)

With the corrected implementation, the results would always be the same,
not different in the '3' case.

Additionally, my version avoids extra operation on the underlying
dictionary.

Jeff

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rteval: Add __contains__ in rtevalConfig
  2021-07-30  0:54 ` Jeff Epler
@ 2021-07-30  3:04   ` John Kacur
  0 siblings, 0 replies; 5+ messages in thread
From: John Kacur @ 2021-07-30  3:04 UTC (permalink / raw)
  To: Jeff Epler; +Cc: RT, Clark Williams, Atsushi Nemoto



On Thu, 29 Jul 2021, Jeff Epler wrote:

> On Thu, Jul 29, 2021 at 06:07:13PM -0400, John Kacur wrote:
> > Add the __contains__ function to the rtevalCfgSection class  to make "in"
> > function correctly.
> 
> Thank you.  A possible correction:
> 
> I believe the correct implementation (to delegate the 'in' operation to
> the dictionary-like object self.__cfgdata) is
> 
>     def __contains__(self, key):
>         return key in self.__cfgdata
> 
> I mocked a bit of rtevalCfgSection with your implementation:
> 	class rtevalCfgSection:
> 	    def __init__(self, cfgdata):
> 		self.__cfgdata = cfgdata
> 
> 	    def __contains__(self, key):
> 		if key in self.__cfgdata.keys():
> 		    return self.__cfgdata[key]
> 		return None
> 
> and then tried it with some carefully chosen values
> 
> >>> d = {1: 'x', 2: 'y', 3: None, 4: False}
> >>> r = rtevalConfig.rtevalCfgSection(d)
> >>> 1 in d, 1 in r
> (True, True)
> >>> 9 in d, 9 in r
> (False, False)
> >>> 3 in d, 3 in r
> (True, False)
> 
> With the corrected implementation, the results would always be the same,
> not different in the '3' case.
> 
> Additionally, my version avoids extra operation on the underlying
> dictionary.
> 
> Jeff
> 

Your version is simpler and correct in the case where the item is None
Integrating your version into the patch and resending, thanks!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rteval: Add __contains__ in rtevalConfig
  2021-07-30  3:05 John Kacur
@ 2021-07-30  5:03 ` Atsushi Nemoto
  0 siblings, 0 replies; 5+ messages in thread
From: Atsushi Nemoto @ 2021-07-30  5:03 UTC (permalink / raw)
  To: jkacur; +Cc: linux-rt-users, williams, jepler

On Thu, 29 Jul 2021 23:05:40 -0400, John Kacur <jkacur@redhat.com> wrote:
> Add the __contains__ function to the rtevalCfgSection class  to make "in"
> function correctly.
> 
> For example in cyclictest.py, self.__cfg is not a dictionary, it is an
> instance of the rtevalCfgSection class, therefore
> key in self.__cfg
> does not work as expected. In order to make sure this is fixed
> everywhere, instead of comparing to self.__cfg.keys() everywhere this is
> used, implement the __contains__ function so that "in" works as
> expected.
> 
> This bug was introduced by the commit fd3b732f714d ("rteval: 2to3
> transformations")
> 
> Reported-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
> Signed-off-by: John Kacur <jkacur@redhat.com>
> Correction
> Suggested-by: Jeff Epler <jepler@unpythonic.net>
> Signed-off-by: John Kacur <jkacur@redhat.com>
> ---
>  rteval/rtevalConfig.py | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py
> index 578aaa3ab58f..56bbc9ee0de6 100644
> --- a/rteval/rtevalConfig.py
> +++ b/rteval/rtevalConfig.py
> @@ -126,6 +126,8 @@ class rtevalCfgSection:
>              return self.__cfgdata[key]
>          return None
>  
> +    def __contains__(self, key):
> +        return key in self.__cfgdata
>  
>      def items(self):
>          return list(self.__cfgdata.items())
> -- 
> 2.31.1

Thank you for review and correct fix.

---
Atsushi Nemoto

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] rteval: Add __contains__ in rtevalConfig
@ 2021-07-30  3:05 John Kacur
  2021-07-30  5:03 ` Atsushi Nemoto
  0 siblings, 1 reply; 5+ messages in thread
From: John Kacur @ 2021-07-30  3:05 UTC (permalink / raw)
  To: RT; +Cc: Clark Williams, John Kacur, Atsushi Nemoto, Jeff Epler

Add the __contains__ function to the rtevalCfgSection class  to make "in"
function correctly.

For example in cyclictest.py, self.__cfg is not a dictionary, it is an
instance of the rtevalCfgSection class, therefore
key in self.__cfg
does not work as expected. In order to make sure this is fixed
everywhere, instead of comparing to self.__cfg.keys() everywhere this is
used, implement the __contains__ function so that "in" works as
expected.

This bug was introduced by the commit fd3b732f714d ("rteval: 2to3
transformations")

Reported-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
Signed-off-by: John Kacur <jkacur@redhat.com>
Correction
Suggested-by: Jeff Epler <jepler@unpythonic.net>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
 rteval/rtevalConfig.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py
index 578aaa3ab58f..56bbc9ee0de6 100644
--- a/rteval/rtevalConfig.py
+++ b/rteval/rtevalConfig.py
@@ -126,6 +126,8 @@ class rtevalCfgSection:
             return self.__cfgdata[key]
         return None
 
+    def __contains__(self, key):
+        return key in self.__cfgdata
 
     def items(self):
         return list(self.__cfgdata.items())
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-30  5:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 22:07 [PATCH] rteval: Add __contains__ in rtevalConfig John Kacur
2021-07-30  0:54 ` Jeff Epler
2021-07-30  3:04   ` John Kacur
2021-07-30  3:05 John Kacur
2021-07-30  5:03 ` Atsushi Nemoto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.