Get the variables required by the `expression`. A new dictionary defining the variables used in the `expression` is returned. Required variables are first looked up in the `uservars` mapping, then in the set of top-level columns of the table. Unknown variables caus
(
self,
expression: str,
uservars: dict[str, Column | np.ndarray] | None,
depth: int = 1,
)
| 1237 | self._enabled_indexing_in_queries = True |
| 1238 | |
| 1239 | def _required_expr_vars( |
| 1240 | self, |
| 1241 | expression: str, |
| 1242 | uservars: dict[str, Column | np.ndarray] | None, |
| 1243 | depth: int = 1, |
| 1244 | ) -> dict[str, Column | np.ndarray]: |
| 1245 | """Get the variables required by the `expression`. |
| 1246 | |
| 1247 | A new dictionary defining the variables used in the `expression` |
| 1248 | is returned. Required variables are first looked up in the |
| 1249 | `uservars` mapping, then in the set of top-level columns of the |
| 1250 | table. Unknown variables cause a `NameError` to be raised. |
| 1251 | |
| 1252 | When `uservars` is `None`, the local and global namespace where |
| 1253 | the API callable which uses this method is called is sought |
| 1254 | instead. This mechanism will not work as expected if this |
| 1255 | method is not used *directly* from an API callable. To disable |
| 1256 | this mechanism, just specify a mapping as `uservars`. |
| 1257 | |
| 1258 | Nested columns and columns from other tables are not allowed |
| 1259 | (`TypeError` and `ValueError` are raised, respectively). Also, |
| 1260 | non-column variable values are converted to NumPy arrays. |
| 1261 | |
| 1262 | `depth` specifies the depth of the frame in order to reach local |
| 1263 | or global variables. |
| 1264 | |
| 1265 | """ |
| 1266 | # Get the names of variables used in the expression. |
| 1267 | exprvarscache = self._exprvars_cache |
| 1268 | if expression not in exprvarscache: |
| 1269 | # Protection against growing the cache too much |
| 1270 | if len(exprvarscache) > 256: |
| 1271 | # Remove 10 (arbitrary) elements from the cache |
| 1272 | for k in list(exprvarscache)[:10]: |
| 1273 | del exprvarscache[k] |
| 1274 | cexpr = compile(expression, "<string>", "eval") |
| 1275 | exprvars = [ |
| 1276 | var |
| 1277 | for var in cexpr.co_names |
| 1278 | if var not in ["None", "False", "True"] |
| 1279 | and var not in ne.expressions.functions |
| 1280 | ] |
| 1281 | exprvarscache[expression] = exprvars |
| 1282 | else: |
| 1283 | exprvars = exprvarscache[expression] |
| 1284 | |
| 1285 | # Get the local and global variable mappings of the user frame |
| 1286 | # if no mapping has been explicitly given for user variables. |
| 1287 | user_locals, user_globals = {}, {} |
| 1288 | if uservars is None: |
| 1289 | # We use specified depth to get the frame where the API |
| 1290 | # callable using this method is called. For instance: |
| 1291 | # |
| 1292 | # * ``table._required_expr_vars()`` (depth 0) is called by |
| 1293 | # * ``table._where()`` (depth 1) is called by |
| 1294 | # * ``table.where()`` (depth 2) is called by |
| 1295 | # * user-space functions (depth 3) |
| 1296 | user_frame = sys._getframe(depth) |
no outgoing calls
no test coverage detected