Exemplo n.º 1
0
def test_eval():
    from pykern.pkcollections import PKDict
    from pykern.pkunit import pkeq
    from sirepo.template.code_variable import CodeVar, PurePythonEval
    code_var = CodeVar(
        [
            PKDict(
                name='gamma',
                value='(bend_energy * 1e-3 + EMASS) / EMASS',
            ),
            PKDict(
                name='bend_energy',
                value=6.50762633,
            ),
        ],
        PurePythonEval(PKDict(
            CLIGHT=299792458,
            EMASS=0.00051099892,
        )),
    )
    pkeq(0.9973461123895662, code_var.eval_var('sqrt(1 - (1/pow(gamma, 2)))')[0])
    pkeq('unknown token: abc', code_var.eval_var('abc + 123')[1])
    pkeq('division by zero', code_var.eval_var('100 / 0')[1])
    pkeq('1 1 gamma 2 pow / - sqrt', code_var.infix_to_postfix('sqrt(1 - (1/pow(gamma, 2)))'))
    pkeq(True, code_var.is_var_value('abc'))
    pkeq(False, code_var.is_var_value('-1.234e-6'))
    pkeq(False, code_var.is_var_value('0'))
Exemplo n.º 2
0
    def _add_variables_for_lattice_references(self):
        # iterate all values, adding "x->y" lattice referenes as variables "x.y"
        from sirepo.template.code_variable import CodeVar

        def _fix_value(value, names):
            value = re.sub(r'\-\>', '.', value)
            expr = CodeVar.infix_to_postfix(value.lower())
            for v in expr.split(' '):
                if CodeVar.is_var_value(v):
                    m = re.match(r'^(.*?)\.(.*)', v)
                    if m:
                        names[v] = [m.group(1), m.group(2)]
            return value

        names = {}
        for v in self.data.models.rpnVariables:
            if CodeVar.is_var_value(v.value):
                v.value = _fix_value(v.value, names)
        for el in self.data.models.elements:
            for f in el:
                v = el[f]
                if CodeVar.is_var_value(v):
                    el[f] = _fix_value(v, names)
        for name in names:
            for el in self.data.models.elements:
                if el.name.lower() == names[name][0]:
                    f = names[name][1]
                    if f in el:
                        self.data.models.rpnVariables.append(
                            PKDict(
                                name=name,
                                value=el[f],
                            ))
Exemplo n.º 3
0
 def render_lattice(self,
                    fields,
                    quote_name=False,
                    want_semicolon=False,
                    want_name=True,
                    want_var_assign=False):
     """Render lattice elements.
     """
     from sirepo.template.code_variable import CodeVar
     res = ''
     for el in fields:
         # el is [model, [[f, v], [f, v]...]]
         el_type = self.type_for_data(el[0])
         if want_name:
             name = el[0].name.upper()
             if quote_name:
                 name = '"{}"'.format(name)
             res += '{}: {},'.format(name, el_type)
         else:
             res += '{},'.format(el_type)
         for f in el[1]:
             var_assign = ''
             if want_var_assign:
                 s = self.schema.model[el_type]
                 if f[0] in s and s[
                         f[0]][1] == 'RPNValue' and CodeVar.is_var_value(
                             f[1]):
                     var_assign = ':'
             res += '{}{}={},'.format(f[0], var_assign, f[1])
         res = res[:-1]
         if want_semicolon:
             res += ';'
         res += '\n'
     return res
Exemplo n.º 4
0
 def _fix_value(value, names):
     value = re.sub(r'\-\>', '.', value)
     expr = CodeVar.infix_to_postfix(value.lower())
     for v in expr.split(' '):
         if CodeVar.is_var_value(v):
             m = re.match(r'^(.*?)\.(.*)', v)
             if m:
                 names[v] = [m.group(1), m.group(2)]
     return value
Exemplo n.º 5
0
 def __is_default(self, model, field_schema, field):
     from sirepo.template.code_variable import CodeVar
     if len(field_schema) < 3:
         return True
     default_value = field_schema[2]
     value = model[field]
     if value is not None and default_value is not None:
         if value == default_value:
             return True
         if field_schema[1] == 'RPNValue':
             if value and not CodeVar.is_var_value(value):
                 return float(value) == default_value
         return str(value) == str(default_value)
     if value is not None:
         return False
     return True