I want to compare two lists that contain class Version(object)
objects to merge one into another but skip the duplicates but python seems to think two Version()
objects are the same even though their content is not.
I tried giving the object a custom "comparison" method as instructed on https://stackoverflow.com/a/1227325/10881866
This is the class I am trying to compare:
class Version(object):
valid_version = False
version = None
valid_platform = False
platform = None
valid_sign = False
sign = None
def __init__(self, version, platform, sign):
version_match = search(version_pattern, version)
if (version_match): self.version = version_match.string; self.valid_version = True
else: self.version = version
self.platform = platform
self.valid_platform = platform in platforms
sign_match = search(sign_pattern, sign)
if (sign_match): self.sign = sign_match.string; self.valid_sign = True
else: self.sign = sign
def __str__(self): return str(self.__dict__)
# def __eq__(self, other): return self.sign == other.sign
This is the helper function I used for merging (found here on SO aswell):
def merge_no_duplicates(iterable_1, iterable_2):
myset = set(iterable_1).union(set(iterable_2))
return list(myset)
This is the part where I merge the lists:
try:
remote_versions = getVersionsFromRemote()
logger.info("Loaded {} remote versions".format(len(remote_versions)))
versions = merge_no_duplicates(versions, remote_versions)
except: logger.error("Can't load remote versions!")
try:
local_versions = getVersionsFromLocal()
logger.info("Loaded {} local versions".format(len(local_versions)))
versions = merge_no_duplicates(versions, local_versions)
except: logger.error("Can't load local versions!")
versions = list(filter(None, versions))
logger.info("Got {} versions total.".format(len(versions)))
Expected:
2019-02-10 19:14:38,220|INFO | Loaded 156 remote versions
2019-02-10 19:14:38,223|INFO | Loaded 156 local versions
2019-02-10 19:14:38,223|INFO | Got 156 versions total.
Actual:
2019-02-10 19:14:38,220|INFO | Loaded 156 remote versions
2019-02-10 19:14:38,223|INFO | Loaded 156 local versions
2019-02-10 19:14:38,223|INFO | Got 312 versions total.