Skip to content

Commit

Permalink
Move _get_control_vars to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Jul 7, 2021
1 parent 2677a94 commit ce3f359
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions pysd/py_backend/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,47 +104,6 @@ def get_header(cls, outfile, force_root=False):

return text, _root

@staticmethod
def get_control_vars(control_vars):
"""
Create the section of control variables
Parameters
----------
control_vars: str
Functions to define control variables.
Returns
-------
text: str
Control variables section and header of model variables section.
"""
text = textwrap.dedent("""
#######################################################################
# CONTROL VARIABLES #
#######################################################################
def _init_outer_references(data):
for key in data:
__data[key] = data[key]
def time():
return __data['time']()
""")

text += control_vars

text += textwrap.dedent("""
#######################################################################
# MODEL VARIABLES #
#######################################################################
""")

return text

@classmethod
def reset(cls):
"""
Expand Down Expand Up @@ -315,7 +274,7 @@ def _build_main_module(elements, subscript_dict, file_name):
"version": __version__
})

text += Imports.get_control_vars(control_vars)
text += _get_control_vars(control_vars)

text += textwrap.dedent("""
# load modules from the modules_%(outfile)s directory
Expand Down Expand Up @@ -460,7 +419,7 @@ def build(elements, subscript_dict, namespace, outfile_name):
"version": __version__,
})

text += Imports.get_control_vars(control_vars) + funcs
text += _get_control_vars(control_vars) + funcs
text = black.format_file_contents(text, fast=True, mode=black.FileMode())

# Needed for various sessions
Expand All @@ -475,7 +434,6 @@ def build(elements, subscript_dict, namespace, outfile_name):


def _generate_functions(elements, subscript_dict):

"""
Builds all model elements as functions in string format.
NOTE: this function calls the build_element function, which updates the
Expand Down Expand Up @@ -511,6 +469,47 @@ def _generate_functions(elements, subscript_dict):
return funcs


def _get_control_vars(control_vars):
"""
Create the section of control variables
Parameters
----------
control_vars: str
Functions to define control variables.
Returns
-------
text: str
Control variables section and header of model variables section.
"""
text = textwrap.dedent("""
##########################################################################
# CONTROL VARIABLES #
##########################################################################
def _init_outer_references(data):
for key in data:
__data[key] = data[key]
def time():
return __data['time']()
""")

text += control_vars

text += textwrap.dedent("""
##########################################################################
# MODEL VARIABLES #
##########################################################################
""")

return text


def build_element(element, subscript_dict):
"""
Returns a string that has processed a single element dictionary.
Expand Down

0 comments on commit ce3f359

Please sign in to comment.