Thread (3 messages) 3 messages, 2 authors, 2021-07-30

Re: [PATCH] rteval: Add __contains__ in rtevalConfig

From: John Kacur <jkacur@redhat.com>
Date: 2021-07-30 03:04:57


On Thu, 29 Jul 2021, Jeff Epler wrote:
On Thu, Jul 29, 2021 at 06:07:13PM -0400, John Kacur wrote:
quoted
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
quoted
quoted
quoted
d = {1: 'x', 2: 'y', 3: None, 4: False}
r = rtevalConfig.rtevalCfgSection(d)
1 in d, 1 in r
(True, True)
quoted
quoted
quoted
9 in d, 9 in r
(False, False)
quoted
quoted
quoted
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!
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help