numexpr
numexpr copied to clipboard
add an example of using strings (literals) on Python3
From [email protected] on June 05, 2013 20:42:28
discovered thru PyTables 3.00, issue here: https://github.com/PyTables/PyTables/issues/265 In Python 3.3
In [13]: import numpy as np
In [14]: np.version Out[14]: '1.7.1'
In [15]: import numexpr as ne
In [16]: ne.version Out[16]: '2.1'
In [17]: arr = np.array([b'str-0', b'str-1', b'str-2', b'str-3', b'str-4', b'str-0', b'str-1', b'str-2', b'str-3', b'str-4'], dtype='|S25')
In [18]: type(arr[0]) Out[18]: numpy.bytes_
These both fail
In [19]: ne.evaluate('arr=="str-2"') TypeError: string argument without an encoding
In [20]: ne.evaluate('arr=="str-2"'.encode('UTF-8')) ValueError: must specify expression as a string
However, can be worked around like this:
In [25]: arg = arr[2]
In [26]: ne.evaluate('arr==arg',{ 'arg' : arg}) Out[26]: array([False, False, True, False, False, False, False, True, False, False], dtype=bool)
So the question is how to specify this literal which could be encoded
Original issue: http://code.google.com/p/numexpr/issues/detail?id=105
From [email protected] on June 05, 2013 16:28:51
We figured out that a literal encoded string CAN be embedded, but should have NO enclosing quotes
In [38]: "arr==" + str('str-2'.encode('UTF-8')) Out[38]: "arr==b'str-2'"
So not sure if this is a 'bug' anymore (maybe should this literal be allowed to have quotes, to be consistent?)
also could be a nice example in the docs?
From [email protected] on June 12, 2013 05:46:27
Summary: add an example of using strings (literals) on Python3 (was: BUG: numexpr 2.1 evaluate of literal against encoded bytes fails)
Labels: -Type-Defect Type-Enhancement
Message to comment on stale issues. If none provided, will not mark issues stale