MCPcopy Create free account
hub / github.com/pydata/numexpr / _name

Function _name

numexpr3/ne3compiler.py:1075–1122  ·  view source on GitHub ↗

AST factory function for NumReg registers from variable names parsed by the AST. Handles three cases: 1. node.id is already in namesReg, in which case re-use it 2. node.id exists in the calling frame, in which case it's KIND_ARRAY, 3. it doesn't, in which case it's a na

(self: NumExpr, node: ast.AST)

Source from the content-addressed store, hash-verified

1073
1074
1075def _name(self: NumExpr, node: ast.AST) -> NumReg:
1076 '''AST factory function for NumReg registers from variable names parsed
1077 by the AST.
1078
1079 Handles three cases:
1080 1. node.id is already in namesReg, in which case re-use it
1081 2. node.id exists in the calling frame, in which case it's KIND_ARRAY,
1082 3. it doesn't, in which case it's a named temporary.
1083 '''
1084 # node.ctx (context) is not something that needs to be tracked
1085
1086 nodeId = node.id
1087 if nodeId in self.registers:
1088 # info( 'ast.Name: found {} in namesReg'.format(nodeId) )
1089 return self.registers[nodeId]
1090
1091 else: # Get address so we can find the dtype
1092 if nodeId in self.local_dict:
1093 nodeRef = self.local_dict[nodeId]
1094 # Should we get rid of _global_dict? It's slowing us down.
1095 elif nodeId in self._global_dict:
1096 nodeRef = self._global_dict[nodeId]
1097 else:
1098 nodeRef = None
1099
1100 if nodeRef is None:
1101 # info( 'ast.Name: named temporary {}'.format(nodeId) )
1102 # It's a named temporary.
1103 # Named temporaries can re-use an existing temporary but they cannot be
1104 # re-used except explicitely by the user!
1105 # TODO: this could also be a return array, check self.assignTarget?
1106 return self._newTemp(None, nodeId)
1107
1108 else:
1109 # It's an existing array or scalar we haven't seen before
1110 # info( 'ast.Name: new existing array {}'.format(nodeId) )
1111 regToken = next(self._regCount)
1112
1113 # We have to make temporary versions of the node reference object
1114 # if it's not an ndarray in order ot coerce out the dtype.
1115 # (NumPy scalars have dtypes but not Python floats, ints)
1116 if np.isscalar(nodeRef):
1117 dchar = np.asarray(nodeRef).dtype.char
1118 # scalars cannot be weak-referenced, but it's not a significant memory-leak.
1119 self.registers[nodeId] = register = NumReg(regToken, nodeId, nodeRef, dchar, _KIND_ARRAY)
1120 else:
1121 self.registers[nodeId] = register = NumReg(regToken, nodeId, weakref.ref(nodeRef), nodeRef.dtype.char, _KIND_ARRAY)
1122 return register
1123
1124def _const(self: NumExpr, node: ast.AST) -> NumReg:
1125 '''

Callers

nothing calls this directly

Calls 2

NumRegClass · 0.85
_newTempMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…