Beispiel #1
0
def get_evaluation(json):
    """
    Get the result operation for one side
    :param json: the object to evaluate
    :param side: the side to be evaluate - 'r' is right and 'l' is left
    """
    if isinstance(json, dict):
        return json if check_type(json, 'num') else remote_call(json)
    else:
        raise InvalidOperationError(f'Invalid node: {json}')
Beispiel #2
0
def add(json):
    """
    left + right
    """
    if not json.get('t') or json.get('t') != 'add':
        raise InvalidOperationError('Add operation was expected')

    left = __evaluation(json, 'l')
    right = __evaluation(json, 'r')

    return get_number(left.get('v') + right.get('v'))
Beispiel #3
0
def div(json):
    """
    left / right
    """
    if not json.get('t') or json.get('t') != 'div':
        raise InvalidOperationError('Div operation was expected')

    left = __evaluation(json, 'l')
    right = __evaluation(json, 'r')

    return get_number(left.get('v') / right.get('v'))
Beispiel #4
0
def mul(json):
    """
    left * right
    """
    if not json.get('t') or json.get('t') != 'mul':
        raise InvalidOperationError('Mul operation was expected')

    left = __evaluation(json, 'l')
    right = __evaluation(json, 'r')

    return get_number(left.get('v') * right.get('v'))
Beispiel #5
0
def sub(json):
    """
    left - right
    """
    if not json.get('t') or json.get('t') != 'sub':
        raise InvalidOperationError('Sub operation was expected')

    left = __evaluation(json, 'l')
    right = __evaluation(json, 'r')

    return get_number(left.get('v') - right.get('v'))
Beispiel #6
0
def remote_call(json):
    """
    Execute a remote call to evaluate an operation_type
    """
    operation_type = json.get('t')
    if not operation_type or operation_type.lower() not in _LOCATIONS.keys():
        raise InvalidOperationError()

    response = requests.post(_LOCATIONS[operation_type.lower()],
                             data=msgpack.packb(json, raw=False),
                             headers={'Content-Type': 'application/msgpack'})
    return msgpack.unpack(response.content, raw=False)
Beispiel #7
0
def __evaluation(json, side):
    """
    if current operation is on a side, operate it from local, otherwise call remote evaluation
    """
    if not isinstance(json, dict):
        raise BadRequestError()

    node = json['v'].get(side)
    result = div(node) if check_type(node, 'div') else get_evaluation(node)
    if not check_type(result, 'num'):
        raise InvalidOperationError(f'Cannot process operation for node {result}')

    return result