exec

how to exit exec() like in a function in Python

how to exit exec() like in a function in Python Question: I’m currently trying to make an self-modifying algorithm in Python. To do that, i have a string containing an algorithm that i run using the exec(algorithm) function inside another funciton. the algorithm that i want to run looks like this : if (a): do_something() …

Total answers: 1

python exec() raises SyntaxError exception when passing an object inside the string argument

python exec() raises SyntaxError exception when passing an object inside the string argument Question: So I am currently trying to deploy my smart contract using a brownie script as shown in the code below: from brownie import accounts, network import sys sys.path.append("../") import globalVars def main(): import_str = "from {0} import {1}".format("brownie", globalVars.contractName) exec(import_str) network.connect(‘development’) …

Total answers: 1

Using filename to initialize new DataFrame without exec

Using filename to initialize new DataFrame without exec Question: I have a series of files to process and I need DataFrames that will have the same names as the files. The number of files is large so I wanted to make the processing parallel with joblib. Unfortunately joblib is not accepting exec as an element …

Total answers: 1

How to close .txt file after "exec" use?

How to close .txt file after "exec" use? Question: I would like to use exec in a loop. Is there a need to close files after using exec? My code: f = exec(open("./settings.txt").read()) f.close() Result: AttributeError: ‘NoneType’ object has no attribute ‘exec’ What should I do to close this file? Asked By: jobberman || Source …

Total answers: 1

PHP exec cannot execute Python script

PHP exec cannot execute Python script Question: I use PHP in Windows 11. I cannot execute Python script in PHP exec. The current situation is as follows, Commands that do not call Python scripts can be executed: exec("cd E:/Python/WordFrequency && ipconfig", $output, $result_code); exec("Python -V", $output, $result_code); The above two lines of code return code …

Total answers: 1

Execute f-string in function

Execute f-string in function Question: I have a string = ‘long company name with technologies in it’ and want to replace all tokens starting with search_string =’techno’ with a new token replace_string = ‘tech’. I wrote a function: def group_tokens(company_name, string_search, string_replace): try: x = company_name.split(" ") print(f"x = [re.sub(‘^{string_search}.*’, ‘{string_replace}’, i) for i in …

Total answers: 3

Dynamically filtering a pandas dataframe

Dynamically filtering a pandas dataframe Question: I am trying to filter a pandas data frame using thresholds for three columns import pandas as pd df = pd.DataFrame({“A” : [6, 2, 10, -5, 3], “B” : [2, 5, 3, 2, 6], “C” : [-5, 2, 1, 8, 2]}) df = df.loc[(df.A > 0) & (df.B > …

Total answers: 4

exec() not working inside function python3.x

exec() not working inside function python3.x Question: I am trying to run this code but it seems that the exec() is not executing the string inside the function: def abc(xyz): for i in fn_lst: s = ‘temp=’ + i + ‘(xyz)’ exec(s) print (temp) abc(‘avdfbafadnf’) The error I am receiving: NameError Traceback (most recent call …

Total answers: 5

Setting variables with exec inside a function

Setting variables with exec inside a function Question: I just started self teaching Python, and I need a little help with this script: old_string = “didnt work” new_string = “worked” def function(): exec(“old_string = new_string”) print(old_string) function() I want to get it so old_string = “worked”. Asked By: Spacenaut || Source Answers: You’re almost there. …

Total answers: 2

Running a Python script from PHP

Running a Python script from PHP Question: I’m trying to run a Python script from PHP using the following command: exec(‘/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2′); However, PHP simply doesn’t produce any output. Error reporting is set to E_ALL and display_errors is on. Here’s what I’ve tried: I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7 I …

Total answers: 11