Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __hash__, __eq__, and __ne__ support for Observer objects #562

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions astroplan/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,85 @@ def __repr__(self):
attributes_strings.append("{}={}".format(name, value))
return "<{}: {}>".format(class_name, ",\n ".join(attributes_strings))

def _key(self):
"""
Generate a tuple of the attributes that determine uniqueness of
`~astroplan.Observer` objects.

Returns
-------
key : tuple

Examples
--------

>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck._key()
('Keck', None, None, None, <Longitude -155.47833333 deg>,
<Latitude 19.82833333 deg>, <Quantity 4160. m>,
<DstTzInfo 'US/Hawaii' LMT-1 day, 13:29:00 STD>)
"""

return (self.name,
self.pressure,
self.temperature,
self.relative_humidity,
self.longitude,
self.latitude,
self.elevation,
self.timezone,)

def __hash__(self):
"""
Hash the `~astroplan.Observer` object.

Examples
--------

>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> hash(keck)
-3872382927731250571
"""

return hash(self._key())

def __eq__(self, other):
"""
Equality check for `~astroplan.Observer` objects.

Examples
--------

>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck2 = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck == keck2
True
"""

if isinstance(other, Observer):
return self._key() == other._key()
else:
return NotImplemented

def __ne__(self, other):
"""
Inequality check for `~astroplan.Observer` objects.

Examples
--------

>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> kpno = Observer.at_site("KPNO", timezone="US/Arizona")
>>> keck != kpno
True
"""

return not self.__eq__(other)

@classmethod
def at_site(cls, site_name, **kwargs):
"""
Expand Down
29 changes: 29 additions & 0 deletions astroplan/tests/test_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,3 +1358,32 @@ def test_observer_lon_lat_el():
obs = Observer.at_site('Subaru')
for attr in ['longitude', 'latitude', 'elevation']:
assert hasattr(obs, attr)


def test_hash_observer():
"""Test that Observer objects are hashable."""
obs1 = Observer.at_site('Subaru')
obs2 = Observer.at_site('Subaru')
assert hash(obs1) == hash(obs2)

obs3 = Observer.at_site('Keck', timezone='US/Hawaii')
assert hash(obs1) != hash(obs3)

obs4 = Observer.at_site('Keck', timezone='US/Hawaii')
assert hash(obs3) == hash(obs4)


def test_eq_observer():
"""Test that Observer objects are comparable."""
obs1 = Observer.at_site('Subaru')
obs2 = Observer.at_site('Subaru')
assert obs1 == obs2

obs3 = Observer.at_site('Keck')
assert obs1 != obs3

obs4 = Observer.at_site('Subaru', timezone='US/Hawaii')
assert obs1 != obs4

obs5 = Observer.at_site('Subaru', timezone='US/Hawaii')
assert obs4 == obs5