The :code:`NumExpr` class is the core encapsulation of the :code:`numexpr3` module. It encapsulates a `CompiledExec` object which is the virtual machine object from the C-api. It also handles all of the parsing of string statements via a functional dictionary that uses :code:`ast
| 304 | # End of ne3.evaluate() |
| 305 | |
| 306 | class NumExpr(object): |
| 307 | ''' |
| 308 | The :code:`NumExpr` class is the core encapsulation of the :code:`numexpr3` |
| 309 | module. It encapsulates a `CompiledExec` object which is the virtual |
| 310 | machine object from the C-api. It also handles all of the parsing of |
| 311 | string statements via a functional dictionary that uses :code:`ast` module |
| 312 | Nodes as keys. This approach results in the Abstract Syntax Tree being |
| 313 | parsed in a single-pass, whereas NumExpr 2 required multiple passes for |
| 314 | different tasks. See the :code:`_ASTAssembler` dictionary and its |
| 315 | associated functions for implementation details. |
| 316 | |
| 317 | Attributes |
| 318 | ^^^^^^^^^^ |
| 319 | |
| 320 | * :code:`program`: The self.program attribute is a `bytes` object and consists of operations |
| 321 | followed by registers with the form:: |
| 322 | |
| 323 | opcode + return_reg + arg1_reg + arg2_reg + arg3_reg |
| 324 | |
| 325 | Currently opcode is a uint16 and the register numbers are uint8. This means |
| 326 | there can be up to 64k operations in the virtual machine and up to 254 |
| 327 | registers/arguments (255 is reserved) in a code block. However, |
| 328 | note that the NumPy code :code:`#define NPY_MAXARGS 32` limits the maximum |
| 329 | number of arguments to 32. |
| 330 | |
| 331 | * :code:`_codeStream`: a :code:`BytesIO` buffer that is used to build the |
| 332 | program. This was found to be the fastest way to construct the large byte |
| 333 | strings formed. |
| 334 | |
| 335 | TODO: other NumExpr attribs |
| 336 | ''' |
| 337 | |
| 338 | |
| 339 | def __init__(self, expr, lib=LIB_STD, casting=CAST_SAFE, local_dict=None, |
| 340 | stackDepth=1): |
| 341 | ''' |
| 342 | Evaluate a mutli-line expression element-wise, using NumPy broadcasting |
| 343 | rules:: |
| 344 | |
| 345 | neObj = NumExpr('c=2*a+3*b') # Builds an NumExpr object |
| 346 | neObj() # Executes with original arrays |
| 347 | neObj(verify=True) # Checks the calling frame for the array names |
| 348 | neObj(a=foo, b=bar, c=moo) # Executes the calculation with new arrays |
| 349 | neObj(**local_dict) # Unpack a dictionary with variable names as keys |
| 350 | |
| 351 | Multi-line statements, typically using triple-quote strings, or semi-colon |
| 352 | seperated statements, are supported. If an intermediate assignment target |
| 353 | exists in the calling frame, it is treated by convention as a second |
| 354 | (or third, ...) output. Otherwise it is a named temporary, and it will |
| 355 | never be a full-size array:: |
| 356 | |
| 357 | big = NumExpr('') |
| 358 | |
| 359 | TODO: example |
| 360 | |
| 361 | Arguments |
| 362 | ^^^^^^^^^ |
| 363 |
no outgoing calls
no test coverage detected
searching dependent graphs…