mercredi 22 janvier 2020

Pythonic way to pass arguments

The main() function is in model_executor.py. The executor fetches from parse_modelscore and in execute_model function calls eval_expression with arguments model_data and json_data. The evaluation Starts from the model eval_expression which has three different type of evaluation: eval_number_expression, eval_string_expression, eval_boolean_expression. The expression are nested expression which could contain many different types of expression inside. So during evaluation of arithmetic expression it needs to call back the eval_number_expression which may evaluate attribute _lookup. And eval_attribute_lookup is the only function that requires model_in which I’m using by declaring global variable. Is there any other way to pass model_in without adding as an arguments to every functions. There are many functions which calls eval_number_expression/string/boolean.

eval_model_def.py

def eval_literal_number(exp, definitions=None, defn_table=None):
      if isinstance(exp, numbers.Number):
        return exp
      return None

 def eval_tree(exp, ...):
     return eval_val
 def eval_case(exp, ...):
     return eval_val

def eval_arithmetic(exp,definitions=None, defn_table=None):
    if not isinstance(exp, list):
        return None
    operands = []
    for idx,item in enumerate(exp[1:], 1):
        print("item", item)
        result = eval_number_expression(item, definitions, defn_table)
        if result is None:
            return None
        operands.append(result)
    op = exp[0]
    if op =="+":
       ret =do mathematical operation for all operators    
    else:
      return None
    return ret

def eval_attribute_lookup(a, exp,definitions=None, defn_table=None):
    if isinstance(exp, list) and len(exp) == 2:
        if exp[0] == "attribute_lookup":
            if isinstance ( exp[1], dict):
                name = exp[1]["name"]
                source = exp[1]["source"]
                result = a[name]
                try:
                    result = int(result)
                except ValueError:
                    result = float(result)
                return result
    return None

def eval_number_expression(exp,definitions=None, defn_table= None):
    operations = [eval_literal_number, eval_arithmetic,eval_numeric_functions,eval_definition_lookup, eval_attribute_lookup,eval_tree, eval_case, eval_linear]
    for o in operations:
        if o ==eval_attribute_lookup:
           o = partial(eval_attribute_lookup, model_in)
        result = o(exp,i, definitions, defn_table)
        if result is not None:
            return result
    return None

def eval_expression(exp,input_at = None,definitions=None,defn_table=None):
    print("-" *i +"eval_expression")
    global model_in

    model_in = input_at

    operations = [eval_number_expression,eval_string_expression, eval_boolean_expression]
    for o in operations:
        result = o(exp, i,definitions, defn_table)
        if result is not None:
           return result
    return None

 model_executor.py
 import parse_modelscore as parse
 import eval_model_def as eval

 def prepare_data(a):
   get data from parse

 def get_json_exp(a):
      return exp

 def execute_model(model_data, json_data):
     data = prepare_data(model_data)
     exp  = get_json_exp(json_data) 
     value = eval.eval_expression(exp, data)

json example

"definitions": [
    {
      "name": "raw_score",
      "type": "number",
      "value": ["+", 1, 2, 3, ["*", 2,["definition_lookup", "add_thing"]]]
    },
    { "name": "add_thing",
      "type": "number",
      "value": ["+", 5, ["definition_lookup", "my_tree"]]
    },
    {
      "name": "my_tree",
      "type": "number",
      "value": ["case",
                 ["attribute_lookup", {"name":"aggs903", "source":"EADS14"}],
                 {
                  "when":[
                    {"op":"=", "comparand":-2.0, "value":0.0},
                    {"op":"=", "comparand": -1.0, "value":0.0},
                    {"op":"<", "comparand":242.0, "value":2.610552},
                    {"op": ">", "comparand":1156.0, "value":2.119683}
                  ],
                  "otherwise":-0.703241
                 }
                ]
    }
  ]

Aucun commentaire:

Enregistrer un commentaire