Skip to content

add an arbitrary-metadata option to parse_variables #1394

@mjburton11

Description

@mjburton11

@bqpd I don't think that parse_variables supports evalfn right now. If there was a syntax something like this, that would be awesome:

class NewModel(Model):
    """
    Variables
    ---------
    x         [-]       stupid var 1
    y         [-]       stupid var 2         evalfn=self.calc_y
    """
    def calc_y(self, v):
        return v(self.x) + 1
    def setup(self):
        exec parse_variables(NewModel.__doc__)

Possible solution is to add this to docstring.py

                while line[labelstart] == " ":
                    labelstart += 1
                label = line[labelstart:].replace("'", "\\'")
                if 'evalfn=' in label:
                    evalfn = label.split('evalfn=')[1]
                    label = label.split('evalfn=')[0]
                else:
                    evalfn = None
            countstr += variable_declaration(nameval, units, label, line, evalfn,
                                             errorcatch)
        if declaration_func is None:
            overallstr += countstr
        else:
            overallstr += declaration_func(string, flag, idx2, countstr)
        string = string[idx2+len(flag):]
    return overallstr


PARSETIP = ("Is this line following the format `Name (optional Value) [Units]"
            " (Optional Description)` without any whitespace in the Name or"
            " Value fields?")


def variable_declaration(nameval, units, label, line, evalfn, errorcatch=True):
    "Turns parsed output into a Variable declaration"
    if len(nameval) > 2:
        raise ValueError("while parsing the line '%s', additional fields"
                         " (separated by whitespace) were found between Value"
                         " '%s' and the Units `%s`. %s"
                         % (line, nameval[1], units, PARSETIP))
    elif len(nameval) == 2:
        out = ("{0} = self.{0} = Variable('{0}', {1}, '{2}', '{3}')")
        out = out.format(nameval[0], nameval[1], units, label)
    elif len(nameval) == 1:
        out = ("{0} = self.{0} = Variable('{0}', '{1}', '{2}', evalfn={3})")
        out = out.format(nameval[0], units, label, evalfn)
    if errorcatch:
        out = """

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions