def build(elements, subscript_dict, namespace, outfile_name):
    """
    Actually constructs and writes the python representation of the model

    Parameters
    ----------
    elements: list
        Each element is a dictionary, with the various components needed to assemble
        a model component in python syntax. This will contain multiple entries for
        elements that have multiple definitions in the original file, and which need
        to be combined.

    subscript_dict: dictionary
        A dictionary containing the names of subscript families (dimensions) as keys, and
        a list of the possible positions within that dimension for each value

    namespace: dictionary
        Translation from original model element names (keys) to python safe
        function identifiers (values)

    outfile_name: string
        The name of the file to write the model to.
    """
    # Todo: deal with model level documentation
    # Todo: Make np, PySD.functions import conditional on usage in the file
    # Todo: Make presence of subscript_dict instantiation conditional on usage
    # Todo: Sort elements (alphabetically? group stock funcs?)
    elements = merge_partial_elements(elements)
    functions = [build_element(element, subscript_dict) for element in elements]

    text = '''
    """
    Python model "%(outfile)s"
    Translated using PySD version %(version)s
    """
    from __future__ import division
    import numpy as np
    from pysd import utils
    import xarray as xr

    from pysd.py_backend.functions import cache
    from pysd.py_backend import functions

    _subscript_dict = %(subscript_dict)s

    _namespace = %(namespace)s

    __pysd_version__ = "%(version)s"
    
    __data = {
        'scope': None,
        'time': lambda: 0
    }

    def _init_outer_references(data):
        for key in data:
            __data[key] = data[key]
    
    def time():
        return __data['time']()
    
    %(functions)s

    ''' % {'subscript_dict': repr(subscript_dict),
           'functions': '\n'.join(functions),
           # 'namespace': '{\n' + '\n'.join(['%s: %s' % (key, namespace[key]) for key in
           #                                namespace.keys()]) + '\n}',
           'namespace': repr(namespace),
           'outfile': os.path.basename(outfile_name),
           'version': __version__}

    style_file = pkg_resources.resource_filename("pysd", "py_backend/output_style.yapf")
    text = text.replace('\t', '    ')
    text, changed = yapf.yapf_api.FormatCode(textwrap.dedent(text),
                                             style_config=style_file)

    # this is used for testing
    if outfile_name == 'return':
        return text

    with open(outfile_name, 'w', encoding='UTF-8') as out:
        out.write(text)
Actually constructs and writes the python representation of the model

    Parameters
    ----------
    elements: list
        Each element is a dictionary, with the various components needed to assemble
        a model component in python syntax. This will contain multiple entries for
        elements that have multiple definitions in the original file, and which need
        to be combined.

    subscript_dict: dictionary
        A dictionary containing the names of subscript families (dimensions) as keys, and
        a list of the possible positions within that dimension for each value

    namespace: dictionary
        Translation from original model element names (keys) to python safe
        function identifiers (values)

    outfile_name: string
        The name of the file to write the model to.