Skip to content

Commit

Permalink
Add formats property to EpochParser
Browse files Browse the repository at this point in the history
  • Loading branch information
mgalloy committed Apr 23, 2019
1 parent f2e7551 commit 73176ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
2 changes: 2 additions & 0 deletions data/epochs_spec.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ nx : type=int, default=1024
ny : type=int, default=1024

cal_version : type=int, default=0

dist_filename : type=str
42 changes: 32 additions & 10 deletions epochs/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,6 @@ def is_valid(self) -> bool:
return True


def _parse_datetime(d: DateValue) -> datetime.datetime:
if isinstance(d, datetime.datetime):
return d
else:
return dateutil.parser.parse(d)


class EpochParser(ConfigParser):
'''EpochParser parses config files with dates as section name. Retrieving an
option for a given date returns the option value on the date closest, but
Expand All @@ -340,6 +333,7 @@ class EpochParser(ConfigParser):
def __init__(self, spec_filename: str=None, **kwargs) -> None:
super().__init__(spec_filename, **kwargs)
self._date = None
self._formats = None

@property
def date(self):
Expand All @@ -353,7 +347,35 @@ def date(self, date: DateValue):
date : DateValue
date as a string or ``datetime.datetime``
'''
self._date = _parse_datetime(date)
self._date = self._parse_datetime(date)

def _parse_datetime(self, d: DateValue) -> datetime.datetime:
if isinstance(d, datetime.datetime):
return d
else:
if self._formats is None:
return dateutil.parser.parse(d)
else:
for f in self._formats:
try:
dt = datetime.datetime.strptime(d, f)
return dt
except ValueError:
pass

@property
def formats(self):
return self._formats

@date.setter
def formats(self, formats: List[str]):
'''
Parameters
----------
formats : List[str]
formats to use for parsing dates via ``datetime.datetime.strptime``
'''
self._formats = formats

def get(self, option: str,
date: DateValue=None, raw: bool=False, **kwargs) -> OptionValue:
Expand All @@ -368,7 +390,7 @@ def get(self, option: str,
raw : bool
set to True is disable interpolation
'''
dt = self._date if date is None else _parse_datetime(date)
dt = self._date if date is None else self._parse_datetime(date)
if dt is None:
raise KeyError('no date for access given')

Expand All @@ -377,7 +399,7 @@ def get(self, option: str,

epoch_names = self.sections()

epoch_dts = [_parse_datetime(s) for s in epoch_names]
epoch_dts = [self._parse_datetime(s) for s in epoch_names]
sorted_epoch_dts = sorted(zip(epoch_dts, epoch_names),
key=lambda x: x[0])

Expand Down
47 changes: 41 additions & 6 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,47 @@ def test_epochparser_property():


def test_epochparser_is_valid():
cp = epochs.ConfigParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
cp.read(os.path.join(DATA_DIR, 'epochs_.cfg'))
assert(cp.is_valid())
ep = epochs.EpochParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
ep.read(os.path.join(DATA_DIR, 'epochs_.cfg'))
assert(ep.is_valid())


def test_epochparser_is_notvalid():
cp = epochs.ConfigParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
cp.read(os.path.join(DATA_DIR, 'epochs_extra.cfg'))
assert(not cp.is_valid()) # has "extra_option" which is not in spec
ep = epochs.EpochParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
ep.read(os.path.join(DATA_DIR, 'epochs_extra.cfg'))
assert(not ep.is_valid()) # has "extra_option" which is not in spec

def test_epoch_parser_interp():
ep = epochs.EpochParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
ep.read(os.path.join(DATA_DIR, 'epochs_interp.cfg'))

dist_filename = ep.get('dist_filename', '2018-01-02')

assert(type(dist_filename) == str)
# TODO: the below will fail
#assert(dist_filename == '/export/data1/Data/dist-1.ncdf')

def test_epoch_parser_format():
ep = epochs.EpochParser(os.path.join(DATA_DIR, 'epochs_spec.cfg'))
ep.formats = ['%Y%m%d', '%Y%m%d.%H%M%S']
ep.read(os.path.join(DATA_DIR, 'epochs_format.cfg'))

cal_version = ep.get('cal_version', '20171231')
assert(type(cal_version) == int)
assert(cal_version == 0)

cal_version = ep.get('cal_version', '20180101.060000')
assert(type(cal_version) == int)
assert(cal_version == 1)

cal_version = ep.get('cal_version', '20180101.100000')
assert(type(cal_version) == int)
assert(cal_version == 2)

cal_version = ep.get('cal_version', '20180102.100000')
assert(type(cal_version) == int)
assert(cal_version == 2)

cal_version = ep.get('cal_version', '20180103.060000')
assert(type(cal_version) == int)
assert(cal_version == 3)

0 comments on commit 73176ef

Please sign in to comment.