Skip to content

Commit fce6f72

Browse files
committed
WIP: add minimal validation of dicts on template init
1 parent 0b673eb commit fce6f72

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

ncwriter/template.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
import numpy as np
2222

2323

24+
def validate_dict(o, name):
25+
"""
26+
Check that the given object is a dictionary. If not, raise TypeError.
27+
28+
:param o: The object to validate.
29+
:param str name: The name of the object to use in the error message.
30+
:return: None
31+
"""
32+
33+
if not isinstance(o, dict):
34+
raise TypeError("{name} should be a dictionary (got {o})".format(name=name, o=repr(o)))
35+
36+
2437
class NetCDFGroupDict(object):
2538
def __init__(self,
2639
dimensions=None,
@@ -54,6 +67,10 @@ def __init__(self,
5467
#w4.variables.keys() = ['temp']
5568
#w4.dimensions = {'lon':720,'lat':330,'time':300}
5669
"""
70+
self._dimensions = None
71+
self._variables = None
72+
self._global_attributes = None
73+
5774
self.title = title
5875
self.dimensions = dimensions or OrderedDict()
5976
self.variables = variables or OrderedDict()
@@ -83,6 +100,33 @@ def __add__(self, other):
83100
self_copy.title = "{t1} + {t2}".format(t1=self.title, t2=other.title)
84101
return self_copy
85102

103+
@property
104+
def dimensions(self):
105+
return self._dimensions
106+
107+
@dimensions.setter
108+
def dimensions(self, value):
109+
validate_dict(value, 'dimensions')
110+
self._dimensions = value
111+
112+
@property
113+
def variables(self):
114+
return self._variables
115+
116+
@variables.setter
117+
def variables(self, value):
118+
validate_dict(value, 'variables')
119+
self._variables = value
120+
121+
@property
122+
def global_attributes(self):
123+
return self._global_attributes
124+
125+
@global_attributes.setter
126+
def global_attributes(self, value):
127+
validate_dict(value, 'global_attributes')
128+
self._global_attributes = value
129+
86130
def is_dim_consistent(self):
87131
"""Check if the variable dictionary
88132
is consistent with current dimensions"""

test_ncwriter/test_template.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ def test_init_from_dicts(self):
5656
self.assertEqual(self.variables, template.variables)
5757
self.assertEqual(self.global_attributes, template.global_attributes)
5858

59+
def test_init_from_dicts_validation(self):
60+
with self.assertRaises(TypeError):
61+
DatasetTemplate(dimensions='TIME')
62+
with self.assertRaises(TypeError):
63+
DatasetTemplate(dimensions=100)
64+
with self.assertRaises(TypeError):
65+
DatasetTemplate(dimensions=['TIME', 'Z'])
66+
67+
with self.assertRaises(TypeError):
68+
DatasetTemplate(variables='TEMP')
69+
with self.assertRaises(TypeError):
70+
DatasetTemplate(variables=['TEMP', 'PSAL'])
71+
72+
with self.assertRaises(TypeError):
73+
DatasetTemplate(global_attributes=['title', 'abstract'])
74+
5975
def test_init_from_json(self):
6076
template = DatasetTemplate.from_json(TEMPLATE_JSON)
6177
self.assertEqual(self.dimensions, template.dimensions)

0 commit comments

Comments
 (0)