Skip to content

Commit

Permalink
Fix issue with setting time object into nested models or macros
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Mar 15, 2019
1 parent 5be28c6 commit dc3b3df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pysd/py_backend/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def add_macro(macro_name, filename, arg_names, arg_vals):
[utils.make_python_identifier(f)[0] for f in arg_vals]),
'real_name': 'Macro Instantiation of ' + macro_name,
'doc': 'Instantiates the Macro',
'py_expr': "functions.Macro('%s', %s, '%s', __data['time'])" % (filename, func_args, macro_name),
'py_expr': "functions.Macro('%s', %s, '%s', time_initialization=lambda: __data['time'])" % (filename, func_args, macro_name),
'unit': 'None',
'lims': 'None',
'eqn': 'None',
Expand Down
25 changes: 17 additions & 8 deletions pysd/py_backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Macro(Stateful):
execution.
"""

def __init__(self, py_model_file, params=None, return_func=None, time=None):
def __init__(self, py_model_file, params=None, return_func=None, time=None, time_initialization=None):
"""
The model object will be created with components drawn from a translated python
model file.
Expand All @@ -290,6 +290,7 @@ def __init__(self, py_model_file, params=None, return_func=None, time=None):
"""
super(Macro, self).__init__()
self.time = time
self.time_initialization = time_initialization

# need a unique identifier for the imported module.
module_name = os.path.splitext(py_model_file)[0] + str(random.randint(0, 1000000))
Expand Down Expand Up @@ -328,6 +329,13 @@ def initialize(self, initialization_order=None):
go on to the other state initializations. Then come back to it and try again.
"""

# Initialize time
if self.time is None:
if self.time_initialization is None:
self.time = Time()
else:
self.time = self.time_initialization()

# if self.time is None:
# self.time = time
# self.components.time = self.time
Expand Down Expand Up @@ -689,13 +697,14 @@ def _default_return_columns(self):
parsed_expr = []

for key, value in self.components._namespace.items():
sig = signature(getattr(self.components, value))
# The `*args` reference handles the py2.7 decorator.
if len(set(sig.parameters) - {'args'}) == 0:
expr = self.components._namespace[key]
if not expr in parsed_expr:
return_columns.append(key)
parsed_expr.append(expr)
if hasattr(self.components, value):
sig = signature(getattr(self.components, value))
# The `*args` reference handles the py2.7 decorator.
if len(set(sig.parameters) - {'args'}) == 0:
expr = self.components._namespace[key]
if not expr in parsed_expr:
return_columns.append(key)
parsed_expr.append(expr)

return return_columns

Expand Down
1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def assert_allclose(x, y, rtol=1.e-5, atol=1.e-5):
def detect_encoding(file):
detector = UniversalDetector()
for line in open(file, 'rb').readlines():
print(line)
detector.feed(line)
if detector.done: break
detector.close()
Expand Down

0 comments on commit dc3b3df

Please sign in to comment.