Skip to content

Commit

Permalink
Merge pull request #1143: Consolidate date code to one directory
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin authored Feb 1, 2023
2 parents e7965fc + 82a91fa commit e12dbe1
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 32 deletions.
7 changes: 4 additions & 3 deletions augur/dates.py → augur/dates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import pandas as pd
import re
import treetime.utils
from .errors import AugurError, InvalidDate
from augur.errors import AugurError
from .errors import InvalidDate

from augur.util_support.date_disambiguator import DateDisambiguator
from .ambiguous_date import AmbiguousDate

SUPPORTED_DATE_HELP_TEXT = dedent("""\
1. an Augur-style numeric date with the year as the integer part (e.g. 2020.42) or
Expand Down Expand Up @@ -114,7 +115,7 @@ def get_numerical_date_from_value(value, fmt=None, min_max_year=None):
value = fmt.replace('%Y', value).replace('%m', 'XX').replace('%d', 'XX')
if 'XX' in value:
try:
ambig_date = DateDisambiguator(value, fmt=fmt, min_max_year=min_max_year).range()
ambig_date = AmbiguousDate(value, fmt=fmt, min_max_year=min_max_year).range()
except InvalidDate as error:
raise AugurError(str(error)) from error
if ambig_date is None or None in ambig_date:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import re

from augur.errors import InvalidDate
from .errors import InvalidDate


def tuple_to_date(year, month, day):
Expand Down Expand Up @@ -38,7 +38,7 @@ def resolve_uncertain_int(uncertain_string, min_or_max):


# This was originally from treetime.utils.ambiguous_date_to_date_range.
class DateDisambiguator:
class AmbiguousDate:
"""Transforms a date string with uncertainty into the range of possible dates."""

def __init__(self, uncertain_date, fmt="%Y-%m-%d", min_max_year=None):
Expand Down
11 changes: 11 additions & 0 deletions augur/dates/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class InvalidDate(Exception):
"""Custom exception class to handle dates in unsupported formats."""

def __init__(self, date, message):
"""Initialize the error with the culprit date string and a meaningful message."""
self.date = date
self.message = message

def __str__(self):
"""Return a human-readable summary of the error."""
return f"Invalid date '{self.date}': {self.message}"
14 changes: 0 additions & 14 deletions augur/errors.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
class AugurError(Exception):
pass


# TODO: Move this to something like dates/errors.py
class InvalidDate(Exception):
"""Custom exception class to handle dates in unsupported formats."""

def __init__(self, date, message):
"""Initialize the error with the culprit date string and a meaningful message."""
self.date = date
self.message = message

def __str__(self):
"""Return a human-readable summary of the error."""
return f"Invalid date '{self.date}': {self.message}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
augur.util\_support.date\_disambiguator
augur.dates.ambiguous\_date
=======================================

.. automodule:: augur.util_support.date_disambiguator
.. automodule:: augur.dates.ambiguous_date
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/developer/augur.dates.errors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
augur.dates.errors
==================

.. automodule:: augur.dates.errors
:members:
:undoc-members:
:show-inheritance:
5 changes: 5 additions & 0 deletions docs/api/developer/augur.dates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ augur.dates
:members:
:undoc-members:
:show-inheritance:

.. toctree::

augur.dates.ambiguous_date
augur.dates.errors
1 change: 0 additions & 1 deletion docs/api/developer/augur.util_support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ augur.util\_support

augur.util_support.color_parser
augur.util_support.color_parser_line
augur.util_support.date_disambiguator
augur.util_support.node_data
augur.util_support.node_data_file
augur.util_support.node_data_reader
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import datetime
from augur.errors import InvalidDate

from augur.util_support import date_disambiguator
from augur.util_support.date_disambiguator import DateDisambiguator
from augur.dates import ambiguous_date
from augur.dates.ambiguous_date import AmbiguousDate
from augur.dates.errors import InvalidDate

from freezegun import freeze_time
import pytest


class TestDateDisambiguator:
class TestAmbiguousDate:
@freeze_time("2111-05-05")
@pytest.mark.parametrize(
"date_str, expected_range",
Expand All @@ -19,7 +19,7 @@ class TestDateDisambiguator:
],
)
def test_range(self, date_str, expected_range):
assert DateDisambiguator(date_str).range() == expected_range
assert AmbiguousDate(date_str).range() == expected_range

@pytest.mark.parametrize(
"date_str, fmt",
Expand All @@ -31,7 +31,7 @@ def test_range(self, date_str, expected_range):
],
)
def test_range_separators(self, date_str, fmt):
assert DateDisambiguator(date_str, fmt=fmt).range() == (
assert AmbiguousDate(date_str, fmt=fmt).range() == (
datetime.date(2005, 2, 1),
datetime.date(2005, 2, 28),
)
Expand All @@ -46,12 +46,12 @@ def test_range_separators(self, date_str, fmt):
)
def test_uncertain_date_components(self, date_str, expected_components):
assert (
DateDisambiguator(date_str).uncertain_date_components == expected_components
AmbiguousDate(date_str).uncertain_date_components == expected_components
)

def test_uncertain_date_components_error(self):
with pytest.raises(InvalidDate, match="Date does not match format"):
DateDisambiguator("5-5-5-5-5").uncertain_date_components
AmbiguousDate("5-5-5-5-5").uncertain_date_components

@pytest.mark.parametrize(
"date_str, min_or_max, expected",
Expand All @@ -68,7 +68,7 @@ def test_uncertain_date_components_error(self):
)
def test_resolve_uncertain_int(self, date_str, min_or_max, expected):
assert (
date_disambiguator.resolve_uncertain_int(date_str, min_or_max) == expected
ambiguous_date.resolve_uncertain_int(date_str, min_or_max) == expected
)

@pytest.mark.parametrize(
Expand All @@ -80,4 +80,4 @@ def test_resolve_uncertain_int(self, date_str, min_or_max, expected):
)
def test_assert_only_less_significant_uncertainty(self, date_str, expected_error):
with pytest.raises(InvalidDate, match=expected_error):
DateDisambiguator(date_str)
AmbiguousDate(date_str)
File renamed without changes.

0 comments on commit e12dbe1

Please sign in to comment.