Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
strexpr = strexpr[0]
# Get the variable names used in the condition.
# At the same time, build its signature.
varnames = _get_variable_names(expr)
signature = [(var, typemap[var]) for var in varnames]
try:
# See the comments in `numexpr.evaluate()` for the
# reasons of inserting copy operators for unaligned,
# *unidimensional* arrays.
func = NumExpr(expr, signature)
except NotImplementedError as nie:
# Try to make this Numexpr error less cryptic.
raise _unsupported_operation_error(nie)
_, ex_uses_vml = getExprNames(condition, {})
kwargs = {'ex_uses_vml': ex_uses_vml}
params = varnames
# This is more comfortable to handle about than a tuple.
return CompiledCondition(func, params, idxexprs, strexpr, **kwargs)
def get_cnames(expr,
_names_cache=ne.utils.CacheDict(256) if ne else ne,
_numexpr_cache=ne.utils.CacheDict(256) if ne else ne,
**kwargs):
if not isinstance(expr, (str, unicode)):
raise ValueError("must specify expression as a string")
# Get the names for this expression
context = ne.necompiler.getContext(kwargs, frame_depth=1)
expr_key = (expr, tuple(sorted(context.items())))
if expr_key not in _names_cache:
_names_cache[expr_key] = ne.necompiler.getExprNames(expr.strip(),
context)
names, ex_uses_vml = _names_cache[expr_key]
return names
def get_cnames(expr,
_names_cache=ne.utils.CacheDict(256) if ne else ne,
_numexpr_cache=ne.utils.CacheDict(256) if ne else ne,
**kwargs):
if not isinstance(expr, (str, unicode)):
raise ValueError("must specify expression as a string")
# Get the names for this expression
context = ne.necompiler.getContext(kwargs, frame_depth=1)
expr_key = (expr, tuple(sorted(context.items())))
if expr_key not in _names_cache:
_names_cache[expr_key] = ne.necompiler.getExprNames(expr.strip(),
context)
names, ex_uses_vml = _names_cache[expr_key]
return names
self.stop = None
"""The stop range selection for the input."""
self.step = None
"""The step range selection for the input."""
self.values = []
"""The values of variables in expression (list)."""
self._compiled_expr = None
"""The compiled expression."""
self._single_row_out = None
"""A sample of the output with just a single row."""
# First, get the signature for the arrays in expression
vars_ = self._required_expr_vars(expr, uservars)
context = getContext(kwargs)
self.names, _ = getExprNames(expr, context)
# Raise a ValueError in case we have unsupported objects
for name, var in vars_.items():
if type(var) in (int, float, str):
continue
if not isinstance(var, (tb.Leaf, tb.Column)):
if hasattr(var, "dtype"):
# Quacks like a NumPy object
continue
raise TypeError("Unsupported variable type: %r" % var)
objname = var.__class__.__name__
if objname not in ("Array", "CArray", "EArray", "Column"):
raise TypeError("Unsupported variable type: %r" % var)
# NumPy arrays to be copied? (we don't need to worry about
# PyTables objects, as the reads always return contiguous and
def _validate(self):
"""
Ensure that our expression string has variables of the form x_0, x_1,
... x_(N - 1), where N is the length of our inputs.
"""
variable_names, _unused = getExprNames(self._expr, {})
expr_indices = []
for name in variable_names:
if name == 'inf':
continue
match = _VARIABLE_NAME_RE.match(name)
if not match:
raise ValueError("%r is not a valid variable name" % name)
expr_indices.append(int(match.group(2)))
expr_indices.sort()
expected_indices = list(range(len(self.inputs)))
if expr_indices != expected_indices:
raise ValueError(
"Expected %s for variable indices, but got %s" % (
expected_indices, expr_indices,
)
def evaluate(tree, expression):
cleaned_expression, alias_dict = preprocess_expression(expression)
context = numexpr.necompiler.getContext({}, frame_depth=1)
variables = numexpr.necompiler.getExprNames(cleaned_expression, context)[0]
try:
adaptor = TreeToDictAdaptor(tree, alias_dict, variables)
except ValueError:
msg = "Cannot broadcast all variables in expression: %s" % expression
logger.error(msg)
raise ValueError(msg)
result = numexpr.evaluate(cleaned_expression, local_dict=adaptor)
result = adaptor.apply_jaggedness(result)
return result