comparison py/params.py @ 444:c5629d79b4ac

few more bits
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Nov 2012 23:50:40 +0800
parents
children b32e5a11a4cb
comparison
equal deleted inserted replaced
443:bca470d153fd 444:c5629d79b4ac
1 # -*- coding: utf-8 -*-
2 import collections
3 import json
4 import config
5
6 _FIELD_DEFAULTS = {
7 'fridge_setpoint': 16,
8 'fridge_difference': 0.2,
9 'overshoot_delay': 720, # 12 minutes
10 'overshoot_factor': 1, # ÂșC
11 }
12
13 class Params(dict):
14 class Error(Exception):
15 pass
16
17 def __init__(self):
18 self.update(_FIELD_DEFAULTS)
19
20 def __getattr__(self, k):
21 return self[k]
22
23 def __setattr__(self, k, v):
24 # fail if we set a bad value
25 self[k]
26 self[k] = v
27
28 def load(self, f = None):
29 if not f:
30 f = file(config.PARAMS_FILE, 'r')
31 u = json.load(f)
32 for k in u:
33 if k not in self:
34 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
35 self.update(u)
36
37 def save(self, f = None):
38 if not f:
39 f = file(config.PARAMS_FILE, 'w')
40 json.dump(self, f, sort_keys=True, indent=4)
41 f.flush()