Skip to content

Commit 1c7e157

Browse files
authored
Merge pull request #372 from apdavison/fix-parameters
Fix a different in interface between NTParameterSet and the other ParameterSet classes
2 parents 77869db + 04a1886 commit 1c7e157

2 files changed

Lines changed: 44 additions & 37 deletions

File tree

sumatra/parameters.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ParameterSet(metaclass=abc.ABCMeta):
6060

6161
def _new_param_check(self, name, value):
6262
try:
63-
self.values[name]
63+
self._values[name]
6464
except KeyError:
6565
raise ValueError("")
6666

@@ -98,6 +98,9 @@ def parse_command_line_parameter(self, p):
9898
def diff(self, other):
9999
return _dict_diff(self, other)
100100

101+
def items(self):
102+
return self._values
103+
101104

102105
def _dict_diff(a, b):
103106
a_keys = set(a.keys())
@@ -139,16 +142,16 @@ def __init__(self, initialiser):
139142
try:
140143
if os.path.exists(initialiser):
141144
with open(initialiser) as fid:
142-
self.values = yaml.safe_load(fid)
145+
self._values = yaml.safe_load(fid)
143146
self.source_file = initialiser
144147
else:
145148
if initialiser:
146-
self.values = yaml.safe_load(initialiser)
149+
self._values = yaml.safe_load(initialiser)
147150
else:
148-
self.values = {}
151+
self._values = {}
149152
except yaml.YAMLError:
150153
raise SyntaxError("Misformatted YAML file")
151-
if not isinstance(self.values, dict):
154+
if not isinstance(self._values, dict):
152155
raise SyntaxError("YAML file cannot be represented as a dict")
153156
else:
154157
raise ImportError("Cannot import PyYAML module")
@@ -157,7 +160,7 @@ def __str__(self):
157160
return self.pretty()
158161

159162
def __getitem__(self, name):
160-
return self.values[name]
163+
return self._values[name]
161164

162165
def __eq__(self, other):
163166
return self.as_dict() == other.as_dict()
@@ -166,7 +169,7 @@ def __ne__(self, other):
166169
return not self.__eq__(other)
167170

168171
def keys(self):
169-
return self.values.keys()
172+
return self._values.keys()
170173

171174
def pretty(self, expand_urls=False):
172175
"""
@@ -177,26 +180,26 @@ def pretty(self, expand_urls=False):
177180
not used.
178181
"""
179182

180-
output = yaml.dump(self.values, indent=4)
183+
output = yaml.dump(self._values, indent=4)
181184
return output
182185

183186
def as_dict(self):
184-
return self.values
187+
return self._values
185188

186189
def save(self, filename, add_extension=False):
187190
if add_extension:
188191
filename += ".yaml"
189192
with open(filename, "w") as f:
190-
yaml.dump(self.values, f)
193+
yaml.dump(self._values, f)
191194
return filename
192195

193196
def update(self, E, **F):
194-
self.values.update(E, **F)
197+
self._values.update(E, **F)
195198
update.__doc__ = dict.update.__doc__
196199

197200
def pop(self, key, d=None):
198-
if key in self.values:
199-
return self.values.pop(key)
201+
if key in self._values:
202+
return self._values.pop(key)
200203
else:
201204
return d
202205

@@ -209,9 +212,13 @@ class NTParameterSet(parameters.ParameterSet, ParameterSet):
209212
def save(self, filename, add_extension=False):
210213
if add_extension:
211214
filename += ".params"
212-
super().save(filename)
215+
super(NTParameterSet, self).save(filename)
213216
return filename
214217

218+
def _new_param_check(self, name, value):
219+
if name not in self:
220+
raise ValueError("")
221+
215222

216223
@component
217224
class SimpleParameterSet(ParameterSet):
@@ -228,7 +235,7 @@ def __init__(self, initialiser):
228235
Create a new parameter set from a file or string. In both cases,
229236
parameters should be separated by newlines.
230237
"""
231-
self.values = {}
238+
self._values = {}
232239
self.types = {}
233240
self.comments = {}
234241
if isinstance(initialiser, dict):
@@ -298,7 +305,7 @@ def _add_or_update_parameter(self, name, value, comment=None):
298305
if value is not None and not isinstance(value, (int, float, str, bool, list, tuple)):
299306
raise TypeError("Value must be one of the basic types (a numeric value, bool, "
300307
"string, list, tuple or None. Got: '{}' ({})".format(value, type(value)))
301-
self.values[name] = value
308+
self._values[name] = value
302309
self.types[name] = type(value)
303310
if comment is not None:
304311
self.comments[name] = comment
@@ -307,20 +314,20 @@ def __str__(self):
307314
return self.pretty()
308315

309316
def __getitem__(self, name):
310-
return self.values[name]
317+
return self._values[name]
311318

312319
def __eq__(self, other):
313-
return ((self.values == other.values) and (self.types == other.types))
320+
return ((self._values == other._values) and (self.types == other.types))
314321

315322
def __ne__(self, other):
316323
return not self.__eq__(other)
317324

318325
def keys(self):
319-
return self.values.keys()
326+
return self._values.keys()
320327

321328
def pop(self, k, d=POP_NONE):
322-
if k in self.values:
323-
v = self.values.pop(k)
329+
if k in self._values:
330+
v = self._values.pop(k)
324331
self.types.pop(k)
325332
self.comments.pop(k, None)
326333
return v
@@ -338,7 +345,7 @@ def pretty(self, expand_urls=False):
338345
not used.
339346
"""
340347
output = []
341-
for name, value in self.values.items():
348+
for name, value in self._values.items():
342349
if isinstance(value, str):
343350
output.append('%s = "%s"' % (name, value))
344351
else:
@@ -348,7 +355,7 @@ def pretty(self, expand_urls=False):
348355
return "\n".join(output)
349356

350357
def as_dict(self):
351-
return self.values.copy()
358+
return self._values.copy()
352359

353360
def save(self, filename, add_extension=False):
354361
if add_extension:
@@ -506,21 +513,21 @@ def __init__(self, initialiser):
506513
try:
507514
if os.path.exists(initialiser):
508515
with open(initialiser) as fid:
509-
self.values = json.load(fid)
516+
self._values = json.load(fid)
510517
self.source_file = initialiser
511518
else:
512519
if initialiser:
513-
self.values = json.loads(initialiser)
520+
self._values = json.loads(initialiser)
514521
else:
515-
self.values = {}
522+
self._values = {}
516523
except ValueError:
517524
raise SyntaxError("Misformatted JSON file")
518525

519526
def __str__(self):
520527
return self.pretty()
521528

522529
def __getitem__(self, name):
523-
return self.values[name]
530+
return self._values[name]
524531

525532
def __eq__(self, other):
526533
return self.as_dict() == other.as_dict()
@@ -529,7 +536,7 @@ def __ne__(self, other):
529536
return not self.__eq__(other)
530537

531538
def keys(self):
532-
return self.values.keys()
539+
return self._values.keys()
533540

534541
def pretty(self, expand_urls=False):
535542
"""
@@ -540,26 +547,26 @@ def pretty(self, expand_urls=False):
540547
not used.
541548
"""
542549

543-
output = json.dumps(self.values, sort_keys=True, indent=4)
550+
output = json.dumps(self._values, sort_keys=True, indent=4)
544551
return output
545552

546553
def as_dict(self):
547-
return self.values
554+
return self._values
548555

549556
def save(self, filename, add_extension=False):
550557
if add_extension:
551558
filename += ".json"
552559
with open(filename, "w") as f:
553-
json.dump(self.values, f)
560+
json.dump(self._values, f)
554561
return filename
555562

556563
def update(self, E, **F):
557-
self.values.update(E, **F)
564+
self._values.update(E, **F)
558565
update.__doc__ = dict.update.__doc__
559566

560567
def pop(self, key, d=None):
561-
if key in self.values:
562-
return self.values.pop(key)
568+
if key in self._values:
569+
return self._values.pop(key)
563570
else:
564571
return d
565572

test/unittests/test_parameters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test__init__should_accept_hash_as_comment_character(self):
8686

8787
def test__init__should_accept_an_empty_initializer(self):
8888
P = SimpleParameterSet("")
89-
self.assertEqual(P.values, {})
89+
self.assertEqual(P._values, {})
9090

9191
def test__init__should_accept_dict(self):
9292
P = SimpleParameterSet({'x': 2, 'y': 3})
@@ -99,7 +99,7 @@ def test__init__should_accept_a_filename_or_string(self):
9999
with open("test_file", "w") as f:
100100
f.write(init)
101101
P2 = SimpleParameterSet("test_file")
102-
self.assertEqual(P1.values, P2.values)
102+
self.assertEqual(P1.items(), P2.items())
103103
os.remove("test_file")
104104

105105
def test__init__should_raise_a_TypeError_if_initializer_is_not_a_filename_or_string(self):
@@ -167,7 +167,7 @@ def test__pretty__output_should_be_useable_to_create_an_identical_parameterset(s
167167
init = "x = 2\ny = 3\nz = 'hello'"
168168
P1 = SimpleParameterSet(init)
169169
P2 = SimpleParameterSet(P1.pretty())
170-
self.assertEqual(P1.values, P2.values)
170+
self.assertEqual(P1.items(), P2.items())
171171

172172
def test__save__should_backup_an_existing_file_before_overwriting_it(self):
173173
# not really sure what the desired behaviour is here

0 commit comments

Comments
 (0)