简体   繁体   中英

Checking if a key exists in an OrderedDict

I am dealing with a code migration from python2 to python3. I don't have much experience with OOP and OrderedDict in python. Here is the issue, which I am not able to solve. Similar questions have been asked before but nothing seems to work for this case. I have the following code:

for row in rows:
    new_ = dummy(row.attr, row.error_type, row.array_agg_1,
                   row.with_error, row.total, row.level, row.order_)

    if new_ in result[n.id]: # <- never becomes true
         print('I am in the if')
         if new_.with_error is not None:
             result[n.id][new_][1] += new_.with_error
             result['total'][new_][1] += new_.with_error
    else:
         print('I am in the else')
         if new_.with_error is not None:
             result[n.id][new_] = [new_.attr, new_.with_error]

This code is functional in python2. However, python3 version fails to execute the outer if statement.

result[n.id] is an OrderedDict looks like this:

#an example of result[n.id]
>>> ([(Eclipse Required Items'nce'[1540972], ['Eclipse Required Items', 1, 1, [1540972], 'nce', 1, 1681]), 
(Other Story Tab Info learned/discovered documentation and accuracy'nce'[1540973], ['Other Story Tab Info learned/discovered documentation and accuracy', 1, 1, [1540973], 'nce', 1, 1684]),
(Other (please provide detail in Comments section)'bce'[1541001], ['Other (please provide detail in Comments section', 1, 1, [1540973], 'bce', 1, 1684]
(Other Static bar information was documented'nce'[1540974], ['Other Static bar information was documented', 1, 1, [1541001], 'nce', 1, 1707])])

#type of result[n.id]
>>> <class 'collections.OrderedDict'>

new_ looks like this:

#print(new_)
Other (please provide detail in Comments section)'bce'[1541001]

#print(type(new_))
<class 'smd.lib.asynch.calibration.dummy'>

Things I have tried

if str(new_) in str(result[n.id]):

It works however fails inside with KeyError and I am not sure if it is the best way to do this. Also, it can break the other functionality of the software.

if new_ in result[n.id].keys():

doesn't work. Any help and directions to resolve the issue will be really appreciated.

dummy class looks like this:

class dummy(object):

    def __init__(self, attr, error_type, path, with_error=None, total=None,
                 level=None, order=None):
        self.attr = attr
        self.error_type = error_type
        self.path = path
        self.with_error = with_error
        self.total = total
        self.level = level
        self.order = order

    def __cmp__(self, other):
        return not (
            self.attr == other.attr and self.error_type == other.error_type
            and self.path == other.path
        )

    def __base_repr__(self):
        return "{0}{1}{2}".format(self.attr, self.error_type, self.path)

    def __hash__(self):
        return hash(self.__base_repr__())

    def __repr__(self):
        return self.__base_repr__()

In Python 3 __cmp__ is not in use anymore. You need to implement __eq__ to your class and remove __cmp__

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM