Skip to content

Commit

Permalink
Merge pull request #6 from Vitens/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AbelHeinsbroek authored May 1, 2017
2 parents ff596b1 + 534ce68 commit 1c75b96
Show file tree
Hide file tree
Showing 16 changed files with 1,619 additions and 914 deletions.
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ install:
- activate test-environment

test_script:
- python setup.py install
- nosetests -v
8 changes: 4 additions & 4 deletions epynet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from network import Network
from link import Link, Pipe, Pump, Valve
from node import Node, Junction, Reservoir, Tank
from objectcollection import ObjectCollection
from .network import Network
from .link import Link, Pipe, Pump, Valve
from .node import Node, Junction, Reservoir, Tank
from .objectcollection import ObjectCollection
62 changes: 42 additions & 20 deletions epynet/baseobject.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,90 @@
import pandas as pd
import warnings
import weakref


def lazy_property(fn):
'''Decorator that makes a property lazy-evaluated.
'''
attr_name = fn.__name__

@property
def _lazy_property(self):
if attr_name not in self._values.keys():
self._values[attr_name] = fn(self)
return self._values[attr_name]
return _lazy_property

class BaseObject(object):

static_properties = {}
properties = {}

def __init__(self, index):
def __init__(self, uid, network):

# the object index
self.index = index
# the object id
self.uid = self.get_uid(index)
# dictionary of static values
self._static_values = {}
self.uid = uid
# weak reference to the network
self.network = weakref.ref(network)
# cache of values
self._values = {}
# dictionary of calculation results, only gets
# filled during solve() method
self.results = {}
# list of times
self.times = []
# index caching
self._index = None

def get_uid(self, index):
def get_index(self, uid):
raise NotImplementedError

def set_object_value(self, index, code, value):
def set_object_value(self, code, value):
raise NotImplementedError

def get_object_value(self, index, code):
def get_object_value(self, code):
raise NotImplementedError

def reset(self):
self._static_values = {}
self._values = {}
self.results = {}
self.times = []

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

def __getattr__(self, name):

if name in self.static_properties.keys():
return self.get_static_property(self.static_properties[name])
return self.get_property(self.static_properties[name])

elif name in self.properties.keys():
if not self.network().solved:
warnings.warn("requesting dynamic properties from an unsolved network")
if self.results == {}:
return self.get_property(self.properties[name])
else:
return pd.Series(self.results[name], index=self.times)
else:
raise AttributeError('Nonexistant Attribute',name)
raise AttributeError('Nonexistant Attribute', name)

def __setattr__(self, name, value):
if name in self.properties.keys():
raise AttributeError("Illegal Assignment to Computed Value")

if name in self.static_properties.keys():
self.set_static_property(self.static_properties[name],value)
self.set_static_property(self.static_properties[name], value)
else:
super(BaseObject, self).__setattr__(name, value)

def set_static_property(self, code, value):
self._static_values[code] = value
self.set_object_value(self.index, code, value)
# set network as unsolved
self.network().solved = False
self._values[code] = value
self.set_object_value(code, value)

def get_property(self, code):
return self.get_object_value(self.index, code)
if code not in self._values.keys():
self._values[code] = self.get_object_value(code)
return self._values[code]

def get_static_property(self, code):
if code not in self._static_values.keys():
self._static_values[code] = self.get_property(code)
return self._static_values[code]
23 changes: 23 additions & 0 deletions epynet/curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from . import epanet2
import weakref

class Curve(object):

def __init__(self, uid, network):
self.uid = uid
self.network = weakref.ref(network)

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

@property
def index(self):
return self.network().ep.ENgetcurveindex(self.uid)

@property
def values(self):
return self.network().ep.ENgetcurve(self.index)

@values.setter
def values(self, value):
self.network().ep.ENsetcurve(self.index, value)
Loading

0 comments on commit 1c75b96

Please sign in to comment.