Skip to content

Commit

Permalink
Merge pull request #23 from mattiaverga/get
Browse files Browse the repository at this point in the history
Add get() method
  • Loading branch information
mattiaverga authored Mar 6, 2021
2 parents bfbc8e0 + e9025e6 commit bc0816f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Usage
::

>>> import pyongc
>>> DSOobject = pyongc.Dso("NGC7000")
>>> DSOobject = pyongc.get("NGC7000")
>>> DSOobject.coords
array([[20. , 59. , 17.14],
[44. , 31. , 43.6 ]])
Expand Down
2 changes: 2 additions & 0 deletions docs/ongc/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Functions provided by ongc module

Public Functions
----------------
.. autofunction:: ongc.get

.. autofunction:: ongc.getNeighbors

.. autofunction:: ongc.getSeparation
Expand Down
2 changes: 1 addition & 1 deletion pyongc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
__all__ = ['ongc', 'exceptions']

from .exceptions import InvalidCoordinates, ObjectNotFound, UnknownIdentifier
from .ongc import Dso, getNeighbors, getSeparation, listObjects, nearby, printDetails, stats
from .ongc import Dso, get, getNeighbors, getSeparation, listObjects, nearby, printDetails, stats
21 changes: 20 additions & 1 deletion pyongc/ongc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

from pyongc import InvalidCoordinates, ObjectNotFound, UnknownIdentifier

__version__ = '0.6.0'
__version__ = '0.6.1'
DBDATE = 20210306 # Version of database data
DBPATH = resource_filename(__name__, 'ongc.db')
PATTERNS = {'NGC|IC': r'^((?:NGC|IC)\s?)(\d{1,4})\s?((NED)(\d{1,2})|[A-Z]{1,2})?$',
Expand Down Expand Up @@ -911,6 +911,25 @@ def _str_to_coords(text: str) -> np.ndarray:
raise InvalidCoordinates(f'This text cannot be recognized as coordinates: {text}')


def get(name: str) -> Optional[Dso]:
"""Search and return an object from the database.
If an object name isn't recognized, it will return None.
Args:
name: the name of the object
Returns:
Dso or None.
"""
try:
obj = Dso(name)
except (ObjectNotFound, UnknownIdentifier):
return None
return obj


def getNeighbors(obj: Union[Dso, str], separation: Union[int, float],
catalog: str = "all") -> List[Tuple[Dso, float]]:
"""Find all neighbors of an object within a user selected range.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_ongc.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ def test_str_to_coords_not_recognized(self):
str_to_coords(bad_coords)
assert f'This text cannot be recognized as coordinates: {bad_coords}' == str(excinfo.value)

def test_get_return_obj(self):
"""The method should return the required object."""
obj = pyongc.get('NGC1')

assert obj.name == 'NGC0001'

def test_get_invalid_identifier(self):
"""An invalid identifier should return None."""
obj = pyongc.get('NGC100000')

assert obj == None

def test_get_not_found(self):
"""If the object is not found should return None."""
obj = pyongc.get('NGC1a')

assert obj == None

def test_get_separation_raw(self):
"""Test that the calculated apparent angular separation between two objects
is correct and reports the raw data to user.
Expand Down

0 comments on commit bc0816f

Please sign in to comment.