Commit 04bdbd7c authored by abuddenberg's avatar abuddenberg
Browse files

Refactored date handling in Dataset from __init__ to properties to make...

Refactored date handling in Dataset from __init__ to properties to make handling available throughout object lifecycle; added test cases for same
parent 76e97430
......@@ -5,6 +5,7 @@ __author__ = 'abuddenberg'
from copy import deepcopy
import json
from dateutil.parser import parse
import re
class Gcisbase(object):
......@@ -184,10 +185,10 @@ class Dataset(Gcisbase):
#This desperately needs to get added to the webform
self._identifiers = {
'Global Historical Climatology Network - Daily': 'GHCN-D',
'Global Historical Climatology Network - Monthly': 'GHCN-M',
'Global Historical Climatology Network - Daily': 'ghcn-daily',
'Global Historical Climatology Network - Monthly': 'ghcn-monthly',
'NCDC Merged Land and Ocean Surface Temperature': 'MLOST',
'Climate Division Database Version 2': 'CDDv2',
'Climate Division Database Version 2': 'cddv2',
'Eighth degree-CONUS Daily Downscaled Climate Projections by Katharine Hayhoe': 'CMIP3-Downscaled', #Problem
'Eighth degree-CONUS Daily Downscaled Climate Projections': 'CMIP3-Downscaled', #Problem
'Earth Policy Institute Atmospheric Carbon Dioxide Concentration, 1000-2012': 'EPI-CO2',
......@@ -196,22 +197,14 @@ class Dataset(Gcisbase):
'NCDC Global Surface Temperature Anomalies': 'NCDC-GST-Anomalies',
'GRACE Static Field Geopotential Coefficients JPL Release 5.0 GSM': 'GRACE'
}
self._release_dt = None
self._access_dt = None
self._publication_year = None
super(Dataset, self).__init__(data, fields=self.gcis_fields, trans=self.translations)
self.identifier = self._identifiers[self.name] if self.name in self._identifiers else self.name
try:
self.access_dt = parse(self.access_dt).isoformat() if self.access_dt else None
except TypeError:
# print "Problem with date: " + self.access_dt
self.access_dt = None
try:
self.release_dt = parse(self.release_dt).isoformat() if self.release_dt else None
except TypeError:
self.release_dt = None
def __str__(self):
return 'Dataset: {id} {name}'.format(id=self.identifier, name=self.name)
......@@ -224,4 +217,39 @@ class Dataset(Gcisbase):
#TODO: Shoot myself for professional negligence.
if hasattr(other, k) and (self.__dict__[k] in (None, '') or len(getattr(other, k)) > self.__dict__[k]):
self.__dict__[k] = getattr(other, k)
return self
\ No newline at end of file
return self
@property
def release_dt(self):
return self._release_dt
@release_dt.setter
def release_dt(self, value):
try:
self._release_dt = parse(value).isoformat() if value else None
except TypeError:
self._release_dt = None
@property
def access_dt(self):
return self._access_dt
@access_dt.setter
def access_dt(self, value):
try:
self._access_dt = parse(value).isoformat() if value else None
except TypeError:
# print "Problem with date: " + self.access_dt
self._access_dt = None
@property
def publication_year(self):
return self._publication_year
@publication_year.setter
def publication_year(self, value):
match = re.search('\d{4}', value) if value else None
if match:
self._publication_year = match.group()
else:
self._publication_year = None
\ No newline at end of file
......@@ -321,4 +321,31 @@ webform_json_precip = """
"list_the_creator_of_the_figure": "Kenneth Kunkel, NOAA NCDC/CICS-NC",
"path": "/system/files/cs_extreme_heavy_precipitation_v7.png"
}
"""
test_dataset_json = """
{
"vertical_extent": null,
"native_id": "TBD",
"cite_metadata": "TBD",
"scale": null,
"temporal_extent": "1895-01-01T00:00:00 2013-08-31T00:00:00",
"version": "2",
"release_dt": "2014-01-01T00:00:00",
"scope": null,
"type": null,
"processing_level": null,
"data_qualifier": null,
"access_dt": "2011-12-31T00:00:00",
"description": "Historical monthly temperature and precipitation time series for 344 climate divisions in the conterminous United States for 1895-present.",
"spatial_ref_sys": null,
"spatial_res": null,
"spatial_extent": "maximum_latitude: 49; minimum_latitude: 24; maximum_longitude: 125; minimum_longitude: 65;",
"doi": null,
"name": "Climate Division Database Version 2",
"url": "https://www.ncdc.noaa.gov/ersst/merge.php",
"publication_year": "2014",
"attributes": "Monthly average maximum temperature, monthly average minimum temperature, monthly average temperature, total monthly precipitation",
"identifier": "cddv2"
}
"""
\ No newline at end of file
......@@ -4,7 +4,8 @@ __author__ = 'abuddenberg'
# py.test -v test_suite.py
from test_data import test_figure_json, test_image_json, webform_json_temp, webform_json_precip
from test_data import test_figure_json, test_image_json, webform_json_temp, webform_json_precip, test_dataset_json
import pytest
import json
from domain import Gcisbase, Figure, Image, Dataset, Chapter
......@@ -67,5 +68,23 @@ def test_chapter_parsing():
assert isinstance(merged_figure.chapter, Chapter)
assert merged_figure.chapter.number == 2
def test_dataset_special_properties():
ds = Dataset(json.loads(test_dataset_json))
assert isinstance(ds, Dataset)
assert ds.release_dt == '2014-01-01T00:00:00'
with pytest.raises(ValueError):
ds.release_dt = '2014-01-01T99:00:00'
assert ds.publication_year == '2014'
ds.publication_year = '2014 (projected)'
assert ds.publication_year == '2014'
ds.publication_year = 'TBD'
assert ds.publication_year is None
if __name__ == '__main__':
test_chapter_parsing()
\ No newline at end of file
test_dataset_special_properties()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment