Skip to content

Commit

Permalink
Merge pull request #372 from lbl-anp/add-datetime-format
Browse files Browse the repository at this point in the history
support datetime format with all underscores
  • Loading branch information
jvavrek authored Jun 14, 2023
2 parents c01d26c + 79e0ba1 commit 76fbfa6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
7 changes: 6 additions & 1 deletion becquerel/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
from dateutil.parser import parse as dateutil_parse
from dateutil.parser import ParserError
from uncertainties import UFloat, unumpy
import warnings
import numpy as np
Expand Down Expand Up @@ -87,6 +88,7 @@ def handle_datetime(input_time, error_name="datetime arg", allow_none=False):
Raises:
TypeError: if input_time is not a string, datetime, date, or None
ValueError: if input_time is a string but can't be parsed
Returns:
a datetime.datetime, or None
Expand All @@ -100,7 +102,10 @@ def handle_datetime(input_time, error_name="datetime arg", allow_none=False):
)
return datetime.datetime(input_time.year, input_time.month, input_time.day)
elif isinstance(input_time, str):
return dateutil_parse(input_time)
try:
return dateutil_parse(input_time)
except ParserError:
return datetime.datetime.strptime(input_time, "%Y_%m_%d_%H_%M_%S")
elif input_time is None and allow_none:
return None
else:
Expand Down
28 changes: 0 additions & 28 deletions tests/test_utils.py

This file was deleted.

61 changes: 61 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import datetime
import pytest
import numpy as np
import becquerel as bq


# ----------------------------------------------
# Test utils
# ----------------------------------------------


def test_sqrt_bins():
"""Test basic functionality of utils.sqrt_bins."""
edge_min = 0
edge_max = 3000
n_bins = 128
be = bq.utils.sqrt_bins(edge_min, edge_max, n_bins)
bc = (be[1:] + be[:-1]) / 2
bw = np.diff(be)
# compute slope of line
m = np.diff(bw**2) / np.diff(bc)
# assert that the square of the bin
assert np.allclose(m[0], m)
# negative edge_min
with pytest.raises(AssertionError):
be = bq.utils.sqrt_bins(-10, edge_max, n_bins)
# edge_max < edge_min
with pytest.raises(AssertionError):
be = bq.utils.sqrt_bins(100, 50, n_bins)


@pytest.mark.parametrize(
"timestamp",
[
datetime.date(year=2023, month=6, day=14),
datetime.datetime(year=2023, month=6, day=14, hour=0, minute=0, second=0),
"2023_06_14_00_00_00",
"2023-06-14T00:00:00.000Z-0000", # ISO 8601, with timezone
],
)
def test_handle_datetime(timestamp):
expected = datetime.datetime(year=2023, month=6, day=14, hour=0, minute=0, second=0)
assert bq.core.utils.handle_datetime(timestamp).replace(tzinfo=None) == expected


def test_handle_datetime_None():
assert bq.core.utils.handle_datetime(None, allow_none=True) is None
with pytest.raises(TypeError):
bq.core.utils.handle_datetime(None, allow_none=False)


@pytest.mark.parametrize(
"arg,error_type",
[
("2023_06_14-08_01_02", ValueError),
(2022, TypeError),
],
)
def test_handle_datetime_err(arg, error_type):
with pytest.raises(error_type):
bq.core.utils.handle_datetime(arg)

0 comments on commit 76fbfa6

Please sign in to comment.