Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses https://github.com/JamesPHoughton/pysd/issues/80 #95

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/pysd.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pysd/pysd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import time
import utils
import tabulate
import warnings

from documentation import SDVarDoc

Expand Down Expand Up @@ -283,6 +284,9 @@ def set_components(self, params):
else:
raise NameError('%s is not recognized as a model component' % key)

if func_name in self.components._stocknames: # warn when setting stock values using params
warnings.warn("Replacing the equation of stock {} with params".format(key), stacklevel=2)

setattr(self.components, func_name, new_function)
self.components._funcs[func_name] = new_function # facilitates lookups

Expand Down
34 changes: 23 additions & 11 deletions tests/unit_test_pysd.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ def test_set_component_with_real_name(self):
model.run(params={'Room Temperature': 70})
self.assertEqual(model.components.room_temperature(), 70)

def test_set_components_warnings(self):
"""Addresses https://github.com/JamesPHoughton/pysd/issues/80"""
import pysd
import warnings
model = pysd.read_vensim(test_model)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
model.set_components({'Teacup Temperature': 20, 'Characteristic Time': 15}) # set stock value using params
self.assertEqual(len(w), 1)
self.assertTrue('Teacup Temperature' in str(w[0].message)) # check that warning references the stock

def test_docs(self):
""" Test that the model prints some documentation """
import pysd
Expand Down Expand Up @@ -310,6 +321,18 @@ def test_default_returns_with_construction_functions(self):
'Stock DelayN',
'Stock Delay3']))

def test_py_model_file(self):
"""Addresses https://github.com/JamesPHoughton/pysd/issues/86"""
import pysd
model = pysd.read_vensim(test_model)
self.assertEqual(model.py_model_file, test_model.replace('.mdl', '.py'))

def test_mdl_file(self):
"""Relates to https://github.com/JamesPHoughton/pysd/issues/86"""
import pysd
model = pysd.read_vensim(test_model)
self.assertEqual(model.mdl_file, test_model)


class TestModelInteraction(unittest.TestCase):
""" The tests in this class test pysd's interaction with itself
Expand Down Expand Up @@ -362,15 +385,4 @@ def test_restart_cache(self):
self.assertEqual(new, 345)
self.assertNotEqual(old, new)

def test_py_model_file(self):
"""Addresses https://github.com/JamesPHoughton/pysd/issues/86"""
import pysd
model = pysd.read_vensim(test_model)
self.assertEqual(model.py_model_file, test_model.replace('.mdl', '.py'))

def test_mdl_file(self):
"""Relates to https://github.com/JamesPHoughton/pysd/issues/86"""
import pysd
model = pysd.read_vensim(test_model)
self.assertEqual(model.mdl_file, test_model)