eval

isinstance with string representing types

isinstance with string representing types Question: isinstance("my string", "str | int") isinstance("my string", "list") Is there a way to check the type of a variable ("my string" in this case) based on a valid type which is stored as a string? I don’t need to import the type, I know it’s builtin (int / str …

Total answers: 3

Evaluating a string as a numpy array

Evaluating a string as a numpy array Question: My team is migrating from Clickhouse to Azure Data Explorer (ADX). We are currently experiencing difficulties to query our data from ADX: the queried values are correct, but the data are read as a string rather than as an array of floats. Here is an example string: …

Total answers: 3

Allow eval() to evaluate only arithmetic expressions and certain functions

Allow eval() to evaluate only arithmetic expressions and certain functions Question: I have a calculator that uses the eval() function to evaluate expressions, but I am aware of the fact that using eval() is dangerous as it can be used to run arbitrary code on the machine. So, I want it to only be able …

Total answers: 1

How can I use the python compile function on an empty string?

How can I use the python compile function on an empty string? Question: I have a piece of code that calculates the sum of a number of variables. For example, with 3 variables (A = 1, B = 2, C = 3) it outputs the sum X = 6. The way the code is implemented …

Total answers: 2

Using eval() to assess user input and some condition. What alternatives?

Using eval() to assess user input and some condition. What alternatives? Question: First, to provide some context, I am designing a console based version of Blackjack for entertainment and learning purposes. The code below was my solution to evaluate different conditions given user input. The function below handles sleep times and user inputs and its …

Total answers: 1

How to make menu using dict evaluation for small python guessing game

How to make menu using dict evaluation for small python guessing game Question: menu = { "1": "start_game()" "2": "player_stats()" "3": "high_scores()" "0": "exit_game()" } So let’s say if user inputs 1 in menu screen, I want to call start_game() function, I know there is other way to do this but just interested in this …

Total answers: 1

strange behaviour of locals() dictionaries when using eval

strange behaviour of locals() dictionaries when using eval Question: Please consider this code: from sklearn.model_selection import ParameterGrid def conf1(): return {"p1": [1,2], "p2": [4,5,6]} def f(aa, bb, cc): c = locals() print("CONFIG, locals():") for k, v, in c.items(): print(f"{k}: {v}") #> params = eval("conf1()") grid = ParameterGrid(params) #< d = {**c} print("CONF after eval") for …

Total answers: 1