Skip to content

Commit 1c75b96

Browse files
Merge pull request #6 from Vitens/development
Development
2 parents ff596b1 + 534ce68 commit 1c75b96

16 files changed

+1619
-914
lines changed

appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ install:
1717
- activate test-environment
1818

1919
test_script:
20+
- python setup.py install
2021
- nosetests -v

epynet/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from network import Network
2-
from link import Link, Pipe, Pump, Valve
3-
from node import Node, Junction, Reservoir, Tank
4-
from objectcollection import ObjectCollection
1+
from .network import Network
2+
from .link import Link, Pipe, Pump, Valve
3+
from .node import Node, Junction, Reservoir, Tank
4+
from .objectcollection import ObjectCollection

epynet/baseobject.py

+42-20
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,90 @@
11
import pandas as pd
2+
import warnings
3+
import weakref
4+
5+
6+
def lazy_property(fn):
7+
'''Decorator that makes a property lazy-evaluated.
8+
'''
9+
attr_name = fn.__name__
10+
11+
@property
12+
def _lazy_property(self):
13+
if attr_name not in self._values.keys():
14+
self._values[attr_name] = fn(self)
15+
return self._values[attr_name]
16+
return _lazy_property
217

318
class BaseObject(object):
419

520
static_properties = {}
621
properties = {}
722

8-
def __init__(self, index):
23+
def __init__(self, uid, network):
924

1025
# the object index
11-
self.index = index
12-
# the object id
13-
self.uid = self.get_uid(index)
14-
# dictionary of static values
15-
self._static_values = {}
26+
self.uid = uid
27+
# weak reference to the network
28+
self.network = weakref.ref(network)
29+
# cache of values
30+
self._values = {}
1631
# dictionary of calculation results, only gets
1732
# filled during solve() method
1833
self.results = {}
1934
# list of times
2035
self.times = []
36+
# index caching
37+
self._index = None
2138

22-
def get_uid(self, index):
39+
def get_index(self, uid):
2340
raise NotImplementedError
2441

25-
def set_object_value(self, index, code, value):
42+
def set_object_value(self, code, value):
2643
raise NotImplementedError
2744

28-
def get_object_value(self, index, code):
45+
def get_object_value(self, code):
2946
raise NotImplementedError
3047

3148
def reset(self):
32-
self._static_values = {}
49+
self._values = {}
3350
self.results = {}
3451
self.times = []
3552

3653
def __str__(self):
3754
return "<epynet."+self.__class__.__name__ + " with id '" + self.uid + "'>"
3855

3956
def __getattr__(self, name):
57+
4058
if name in self.static_properties.keys():
41-
return self.get_static_property(self.static_properties[name])
59+
return self.get_property(self.static_properties[name])
60+
4261
elif name in self.properties.keys():
62+
if not self.network().solved:
63+
warnings.warn("requesting dynamic properties from an unsolved network")
4364
if self.results == {}:
4465
return self.get_property(self.properties[name])
4566
else:
4667
return pd.Series(self.results[name], index=self.times)
4768
else:
48-
raise AttributeError('Nonexistant Attribute',name)
69+
raise AttributeError('Nonexistant Attribute', name)
4970

5071
def __setattr__(self, name, value):
5172
if name in self.properties.keys():
5273
raise AttributeError("Illegal Assignment to Computed Value")
74+
5375
if name in self.static_properties.keys():
54-
self.set_static_property(self.static_properties[name],value)
76+
self.set_static_property(self.static_properties[name], value)
5577
else:
5678
super(BaseObject, self).__setattr__(name, value)
5779

5880
def set_static_property(self, code, value):
59-
self._static_values[code] = value
60-
self.set_object_value(self.index, code, value)
81+
# set network as unsolved
82+
self.network().solved = False
83+
self._values[code] = value
84+
self.set_object_value(code, value)
6185

6286
def get_property(self, code):
63-
return self.get_object_value(self.index, code)
87+
if code not in self._values.keys():
88+
self._values[code] = self.get_object_value(code)
89+
return self._values[code]
6490

65-
def get_static_property(self, code):
66-
if code not in self._static_values.keys():
67-
self._static_values[code] = self.get_property(code)
68-
return self._static_values[code]

epynet/curve.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from . import epanet2
2+
import weakref
3+
4+
class Curve(object):
5+
6+
def __init__(self, uid, network):
7+
self.uid = uid
8+
self.network = weakref.ref(network)
9+
10+
def __str__(self):
11+
return "<epynet."+self.__class__.__name__ + " with id '" + self.uid + "'>"
12+
13+
@property
14+
def index(self):
15+
return self.network().ep.ENgetcurveindex(self.uid)
16+
17+
@property
18+
def values(self):
19+
return self.network().ep.ENgetcurve(self.index)
20+
21+
@values.setter
22+
def values(self, value):
23+
self.network().ep.ENsetcurve(self.index, value)

0 commit comments

Comments
 (0)