Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (37 loc) · 1.07 KB

File metadata and controls

42 lines (37 loc) · 1.07 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sympy as sp
class PythonExpressionRunner:
"""
模拟 QLExpress 的 Python 表达式解析器
"""
def __init__(self):
"""
初始化表达式运行器
"""
self.context = {}
def add_variable(self, name, value):
"""
将变量添加到上下文
:param name: 变量名
:param value: 变量值
"""
self.context[name] = value
def execute(self, expression):
"""
执行表达式
:param expression: 要执行的表达式字符串
:return: 执行结果或None(执行失败时)
"""
try:
# 将上下文中的变量替换到表达式中
result = eval(expression, {}, self.context)
return result
except Exception as e:
print(f"执行表达式失败: {e}")
return None
def add_function(self, name, function):
"""
添加自定义函数
:param name: 函数名
:param function: 函数对象
"""
self.context[name] = function
Morty Proxy This is a proxified and sanitized view of the page, visit original site.