示例#1
0
    def insert_code(self, script, optimise=True):
        """Insert Python code blocks into script.

        Parameters
        ==========
        script : string
            Script containing markers for replacement
        optimise : boolean
            If `True`, attempt to optimise the equation order to achieve a
            'more-recursive' system, to reduce the number of iterations to
            convergence.

        Returns
        =======
        script : string
            Copy of `script` with markers replaced with code

        See also
        ========
        parse_chunks()
        build_initialise()
        build_endogenous_variables()
        build_results()

        FSIC.optimise.order.recursive()
        FSIC.utilities.string.indent_lines()

        """
        # Generate class code, optimising as necessary
        equations = self.parse_chunks()
        if optimise:
            try:
                from FSIC.optimise.order import recursive
            except:
                pass
            else:
                equations = equations.splitlines()
                equations = recursive(equations)
                equations = '\n'.join(equations)
        initialise = self.build_initialise(equations)
        endogenous = self.build_endogenous_variables(equations)
        results = self.build_results(equations)
        # Insert into `script`
        from FSIC.utilities.string import indent_lines
        script = script.replace(
            '___INITIALISE___',
            indent_lines(initialise, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___SOLVE_EQUATIONS___',
            indent_lines(equations, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___GET_ENDOGENOUS_VARIABLE_VALUES___',
            indent_lines(endogenous, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___GET_RESULTS___',
            indent_lines(results, num_tabs=2, skip_first_line=True))
        # Return
        return script
示例#2
0
文件: build.py 项目: ChrisThoung/fsic
    def insert_code(self, script, optimise=True):
        """Insert Python code blocks into script.

        Parameters
        ==========
        script : string
            Script containing markers for replacement
        optimise : boolean
            If `True`, attempt to optimise the equation order to achieve a
            'more-recursive' system, to reduce the number of iterations to
            convergence.

        Returns
        =======
        script : string
            Copy of `script` with markers replaced with code

        See also
        ========
        parse_chunks()
        build_initialise()
        build_endogenous_variables()
        build_results()

        FSIC.optimise.order.recursive()
        FSIC.utilities.string.indent_lines()

        """
        # Generate class code, optimising as necessary
        equations = self.parse_chunks()
        if optimise:
            try:
                from FSIC.optimise.order import recursive
            except:
                pass
            else:
                equations = equations.splitlines()
                equations = recursive(equations)
                equations = '\n'.join(equations)
        initialise = self.build_initialise(equations)
        endogenous = self.build_endogenous_variables(equations)
        results = self.build_results(equations)
        # Insert into `script`
        from FSIC.utilities.string import indent_lines
        script = script.replace(
            '___INITIALISE___',
            indent_lines(initialise, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___SOLVE_EQUATIONS___',
            indent_lines(equations, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___GET_ENDOGENOUS_VARIABLE_VALUES___',
            indent_lines(endogenous, num_tabs=2, skip_first_line=True))
        script = script.replace(
            '___GET_RESULTS___',
            indent_lines(results, num_tabs=2, skip_first_line=True))
        # Return
        return script
示例#3
0
    def insert_info(self, script):
        """Insert Python code blocks into script.

        Parameters
        ==========
        script : string
            Script containing markers for replacement

        Returns
        =======
        script : string
            Copy of `script` with markers replaced with code

        See also
        ========
        parse_chunks()
        get_descriptors()

        FSIC.utilities.string.indent_lines()
        FSIC.utilities.string.wrap_lines()

        """
        # Extract model information
        cfg = self.parse_chunks(classes='ini', language='ini')
        cfg = self.get_descriptors(cfg)
        # Insert into `script`
        from FSIC.utilities.string import indent_lines, wrap_lines
        # Model name and accompanying version information
        script = script.replace('___MODEL___', cfg['name'])
        script = script.replace('___NAME___',
                                cfg['name'] + '\n' + ('=' * len(cfg['name'])))
        script = script.replace(
            '___FSIC_VERSION___', 'Model version: ' + cfg['version'] + '\n' +
            'Generated by FSIC version: ' + version)
        model_version = '''\
self.MAJOR = %d
self.MINOR = %d
self.PATCH = %d
self.VERSION = \'%s\'
self.FSIC_BUILD = \'%s\'
''' % (int(cfg['major']), int(cfg['minor']), int(
            cfg['patch']), cfg['version'], version)
        model_version = indent_lines(model_version,
                                     num_tabs=2,
                                     skip_first_line=True)
        script = script.replace('___MODEL_VERSION___', model_version)
        # Module docstring
        module_docstring = 'Implementation of class %s: %s.' % (
            cfg['name'], cfg['description'])
        module_docstring = wrap_lines(module_docstring, line_length=80)
        script = script.replace('___MODULE_DOCSTRING___', module_docstring)
        # Short description: one-sentence description of the model class
        short_description = 'Class definition for %s: %s' % (
            cfg['name'], cfg['description'])
        if not short_description.endswith('.'):
            short_description += '.'
        script = script.replace('___SHORT_DESCRIPTION___', short_description)
        # Long description: reference to further documentation/information
        long_description = wrap_lines(cfg['reference'], line_length=72)
        long_description = indent_lines(long_description)
        long_description = 'For more information, see:\n' + long_description
        long_description = indent_lines(long_description, skip_first_line=True)
        script = script.replace('___LONG_DESCRIPTION___', long_description)
        # Return
        return script
示例#4
0
文件: build.py 项目: ChrisThoung/fsic
    def insert_info(self, script):
        """Insert Python code blocks into script.

        Parameters
        ==========
        script : string
            Script containing markers for replacement

        Returns
        =======
        script : string
            Copy of `script` with markers replaced with code

        See also
        ========
        parse_chunks()
        get_descriptors()

        FSIC.utilities.string.indent_lines()
        FSIC.utilities.string.wrap_lines()

        """
        # Extract model information
        cfg = self.parse_chunks(classes='ini', language='ini')
        cfg = self.get_descriptors(cfg)
        # Insert into `script`
        from FSIC.utilities.string import indent_lines, wrap_lines
        # Model name and accompanying version information
        script = script.replace('___MODEL___', cfg['name'])
        script = script.replace(
            '___NAME___',
            cfg['name'] + '\n' + ('=' * len(cfg['name'])))
        script = script.replace(
            '___FSIC_VERSION___',
            'Model version: ' + cfg['version'] + '\n' +
            'Generated by FSIC version: ' + version)
        model_version = '''\
self.MAJOR = %d
self.MINOR = %d
self.PATCH = %d
self.VERSION = \'%s\'
self.FSIC_BUILD = \'%s\'
''' % (
    int(cfg['major']),
    int(cfg['minor']),
    int(cfg['patch']),
    cfg['version'],
    version)
        model_version = indent_lines(
            model_version,
            num_tabs=2,
            skip_first_line=True)
        script = script.replace(
            '___MODEL_VERSION___',
            model_version)
        # Module docstring
        module_docstring = 'Implementation of class %s: %s.' % (
            cfg['name'], cfg['description'])
        module_docstring = wrap_lines(module_docstring, line_length=80)
        script = script.replace(
            '___MODULE_DOCSTRING___',
            module_docstring)
        # Short description: one-sentence description of the model class
        short_description = 'Class definition for %s: %s' % (
            cfg['name'], cfg['description'])
        if not short_description.endswith('.'):
            short_description += '.'
        script = script.replace(
            '___SHORT_DESCRIPTION___',
            short_description)
        # Long description: reference to further documentation/information
        long_description = wrap_lines(
            cfg['reference'],
            line_length=72)
        long_description = indent_lines(long_description)
        long_description = 'For more information, see:\n' + long_description
        long_description = indent_lines(
            long_description,
            skip_first_line=True)
        script = script.replace(
            '___LONG_DESCRIPTION___',
            long_description)
        # Return
        return script