Example #1
0
def parse(expression):
    output = ffi.new('char[{}]'.format(MAX_CHARS))
    error = ffi.new('char[{}]'.format(ERROR_CHARS))
    handler = calculate.parse(expression.encode(), error)
    raise_if(decode(error))
    calculate.variables(handler, output)
    return Expression(expression, decode(output))
Example #2
0
 def evaluate(self, args):
     size = len(args)
     if size > 0:
         args = [float(x) for x in args]
         values = ffi.new('double[]', args)
     else:
         values = ffi.new('double *')
     error = ffi.new('char[{}]'.format(ERROR_CHARS))
     result = calculate.evaluate(self._handler, values, size, error)
     raise_if(decode(error))
     return result
Example #3
0
 def __init__(self, expression, variables=''):
     if not isinstance(variables, str) and isinstance(variables, Iterable):
         variables = ','.join(variables) if len(variables) > 0 else ''
     error = ffi.new('char[{}]'.format(ERROR_CHARS))
     self._handler = calculate.create(expression.encode(),
                                      variables.encode(), error)
     raise_if(decode(error))
Example #4
0
 def __getattr__(self, item):
     try:
         output = ffi.new('char[{}]'.format(MAX_CHARS))
         if item not in self._properties:
             raise KeyError
         getattr(calculate, item)(self._handler, output)
         output = decode(output)
         if item == 'variables':
             output = output.split(',') if output else []
         return lambda: output
     except KeyError:
         raise AttributeError('{} object has no attribute {}'.format(
             repr(self.__class__.__name__), repr(item)))