pydata/numexpr

numpy.logical_or TypeError: unsupported operand type(s) for |: 'int' and 'OpNode'

fyrestone opened this issue · 4 comments

reproduce

from numpy import array
import numexpr

a = array([[[1, 1, 1, 1],
            [0, 1, 1, 0],
            [1, 3, 2, 2],
            [1, 0, 2, 0],
            [0, 0, 2, 2],
            [0, 1, 1, 2]],
           [[0, 1, 1, 2],
            [2, 2, 1, 0],
            [2, 0, 2, 0],
            [2, 1, 2, 2],
            [2, 2, 0, 1],
            [0, 2, 1, 3]],
           [[2, 2, 0, 2],
            [1, 2, 1, 3],
            [0, 0, 1, 0],
            [0, 1, 0, 2],
            [2, 1, 0, 2],
            [1, 2, 2, 0]],
           [[1, 0, 1, 0],
            [2, 0, 1, 0],
            [1, 1, 2, 1],
            [1, 0, 1, 3],
            [0, 2, 3, 1],
            [2, 3, 0, 1]]])

print(10 | (a >> 5))
print(numexpr.__version__)
print(numexpr.evaluate("10 | (a >> 5)"))

output

[[[10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]]

 [[10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]]

 [[10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]]

 [[10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]
  [10 10 10 10]]]
2.8.1
Traceback (most recent call last):
  File "test.py", line 31, in <module>
    print(numexpr.evaluate("10 | (a >> 5)"))
  File "/admin/.pyenv/versions/3.8.7/lib/python3.8/site-packages/numexpr/necompiler.py", line 809, in evaluate
    _names_cache[expr_key] = getExprNames(ex, context)
  File "/admin/.pyenv/versions/3.8.7/lib/python3.8/site-packages/numexpr/necompiler.py", line 696, in getExprNames
    ex = stringToExpression(text, {}, context)
  File "/admin/.pyenv/versions/3.8.7/lib/python3.8/site-packages/numexpr/necompiler.py", line 289, in stringToExpression
    ex = eval(c, names)
  File "<expr>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'int' and 'OpNode'

| refers to bitwise or. Try using or:

print(numexpr.evaluate("10 or (a >> 5)"))

Does that solve your problem?

Sorry, above is misinformation, I was thinking of numexpr3.

| refers to bitwise or. Try using or:

print(numexpr.evaluate("10 or (a >> 5)"))

Does that solve your problem?

This code works. Thanks.

The issue is caused by my incorrect usage. But, the document and the error are misleading: https://numexpr.readthedocs.io/projects/NumExpr3/en/latest/user_guide.html#supported-operators

NumExpr supports the set of operators listed below:

  • Logical operators: &, |, ~

TypeError: unsupported operand type(s) for |: 'int' and 'OpNode'

Message to comment on stale issues. If none provided, will not mark issues stale