Reputation: 843
I'm trying to perform some set operations using this object.
def Group(object):
def __init__(self, name, email, description, skip_if_deleted, membership=None)
self.name = name
self.email = remove_special_characters(email.lower(), bad_chars)
self.description = description
self.action = None
self.skip_if_deleted = skip_if_deleted
if membership is not None:
self.membership = set(membership)
else:
self.membership = set([])
def __hash__(self):
return hash(self.email)
def __eq__(self, other):
return self.email == other.email
I am finding that when doing set intersections, I seem to be dropping the populated key 'membership', in exchange for an empty one.
An example:
Fetch from the first source, which does not provide membership information at time of fetch
# Example obj in unpopulated_membership looks like: Group(name, email, description, membership = empty set)
unpopulated_membership = set(get_groups_from_first_source())
Fetch from the second source, which does return membership information
# Example obj in populated_membershop looks like: Group(name, email, description, membership = populated set)
populated_membership = set(get_groups_from_other_source())
common_groups = populated_membership.intersection(unpopulated_membership)
Now at this point, common groups gives us the commonalities between the two sets, but the key 'membership' is now equal to unpopulated_membership's
value. Regardless of whether i do populated_membership.intersection() or unpopulated_membership.intersection()
Why is this? How can i retain the membership information found in populated_membership
when performing this intersection?
Thanks!
Upvotes: 0
Views: 32
Reputation: 281330
The quick-fix would be to use a set comprehension:
common_groups = {group for group in populated_membership
if group in unpopulated_membership}
I would recommend choosing a different way to represent your data, though. For example, using dicts to match email addresses with the information associated with them. You've defined your Group.__hash__
and Group.__eq__
in such a way that "equal" Groups hold different data, which is highly unusual.
Also, if this is Python 2, don't forget to define __ne__
. You should also define __eq__
and __ne__
in such a way that they return NotImplemented
instead of throwing an AttributeError
when the other object isn't a Group.
Upvotes: 1