在字符串中缩进 Python 函数并使用 eval 执行它
Indenting Python function within string and executing it using eval
我编写了简单的代码来处理一种情况并更正包含使用 def
关键字并执行。
def fix_index(string):
i=0;
t=string.find("def")+3;
string=string.replace(string[string.find("def"):t], "@")
while string.find(" ") != -1:
string = string.replace(" ", "")
i += 1
l=list(string);l[string.find(":")-i+2]+="$$$$"
return "".join(l).replace("$$$$", " ").replace("@", "def ").lstrip();
def switch(exp):
def exec(obj):
items = obj.items();
for k, v in items:
if(k==exp):
print(fix_index(v))
return eval(fix_index(v))();
return {"case":exec};
bread = "bread"
switch(bread)["case"]({
"cheese":
"""
def a():
print("cheese");
""",
"bread":
"""
def b():
print("bread");
"""
})
格式化函数字符串的输出:
C:\Users\User>python -u "c:\Users\User\folder\switch.py"
def b():
print("bread");
我遇到的错误:
Traceback (most recent call last):
File "c:\Users\User\folder\switch.py", line 27, in <module>
switch(bread)["case"]({
File "c:\Users\User\folder\switch.py", line 21, in exec
return eval(fix_index(v))();
File "<string>", line 1
def b():
^
SyntaxError: invalid syntax
我也刚刚意识到我没有按照 indented 的意图命名函数(应该在什么时候发布这个醒来以避免意外的双关语)。
无论如何,我无法理解的是我生成的字符串中的哪一部分恰好包含“无效语法”。
如有任何帮助,我将不胜感激。
如果您正在寻找的是重现 switch 语句,您可以使用以下函数来实现:
def switch(v): yield lambda *c: v in c
它使用单遍 for 循环模拟 switch 语句,其中 if/elif/else 条件不重复切换值:
例如:
for case in switch(x):
if case(3):
# ... do something
elif case(4,5,6):
# ... do something else
else:
# ... do some other thing
它也可以用在更C的风格中:
for case in switch(x):
if case(3):
# ... do something
break
if case(4,5,6):
# ... do something else
break
else:
# ... do some other thing
对于您的示例,它可能如下所示:
meal = "bread"
for case in switch(meal):
if case("cheese"):
print("Cheese!")
elif case("bread"):
print("Bread!")
或者这个:
meal = "bread"
for case in switch(meal):
if case("cheese"):
print("Cheese!")
break
if case("bread"):
print("Bread!")
break
我编写了简单的代码来处理一种情况并更正包含使用 def
关键字并执行。
def fix_index(string):
i=0;
t=string.find("def")+3;
string=string.replace(string[string.find("def"):t], "@")
while string.find(" ") != -1:
string = string.replace(" ", "")
i += 1
l=list(string);l[string.find(":")-i+2]+="$$$$"
return "".join(l).replace("$$$$", " ").replace("@", "def ").lstrip();
def switch(exp):
def exec(obj):
items = obj.items();
for k, v in items:
if(k==exp):
print(fix_index(v))
return eval(fix_index(v))();
return {"case":exec};
bread = "bread"
switch(bread)["case"]({
"cheese":
"""
def a():
print("cheese");
""",
"bread":
"""
def b():
print("bread");
"""
})
格式化函数字符串的输出:
C:\Users\User>python -u "c:\Users\User\folder\switch.py"
def b():
print("bread");
我遇到的错误:
Traceback (most recent call last):
File "c:\Users\User\folder\switch.py", line 27, in <module>
switch(bread)["case"]({
File "c:\Users\User\folder\switch.py", line 21, in exec
return eval(fix_index(v))();
File "<string>", line 1
def b():
^
SyntaxError: invalid syntax
我也刚刚意识到我没有按照 indented 的意图命名函数(应该在什么时候发布这个醒来以避免意外的双关语)。
无论如何,我无法理解的是我生成的字符串中的哪一部分恰好包含“无效语法”。
如有任何帮助,我将不胜感激。
如果您正在寻找的是重现 switch 语句,您可以使用以下函数来实现:
def switch(v): yield lambda *c: v in c
它使用单遍 for 循环模拟 switch 语句,其中 if/elif/else 条件不重复切换值:
例如:
for case in switch(x):
if case(3):
# ... do something
elif case(4,5,6):
# ... do something else
else:
# ... do some other thing
它也可以用在更C的风格中:
for case in switch(x):
if case(3):
# ... do something
break
if case(4,5,6):
# ... do something else
break
else:
# ... do some other thing
对于您的示例,它可能如下所示:
meal = "bread"
for case in switch(meal):
if case("cheese"):
print("Cheese!")
elif case("bread"):
print("Bread!")
或者这个:
meal = "bread"
for case in switch(meal):
if case("cheese"):
print("Cheese!")
break
if case("bread"):
print("Bread!")
break