Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mikes zhang
001
提交
d557fbf1
0
001
项目概览
mikes zhang
/
001
通知
6
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
0
001
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
未验证
提交
d557fbf1
编写于
4月 22, 2020
作者:
D
Dan Bader
提交者:
GitHub
4月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #103 from realpython/python-eval-mathrepl
Python eval mathrepl
上级
e55ef993
e221e8b9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
84 addition
and
0 deletion
+84
-0
python-eval-mathrepl/mathrepl.py
python-eval-mathrepl/mathrepl.py
+84
-0
未找到文件。
python-eval-mathrepl/mathrepl.py
0 → 100644
浏览文件 @
d557fbf1
#!/usr/bin/env python3
"""MathREPL, a math expression evaluator using Python's eval() and math."""
import
math
__version__
=
"1.0"
__author__
=
"Leodanis Pozo Ramos"
ALLOWED_NAMES
=
{
k
:
v
for
k
,
v
in
math
.
__dict__
.
items
()
if
not
k
.
startswith
(
"__"
)
}
PS1
=
"mr>>"
WELCOME
=
f
"""
MathREPL
{
__version__
}
, your Python math expressions evaluator!
Enter a valid math expression after the prompt "
{
PS1
}
".
Type "help" for more information.
Type "quit" or "exit" to exit.
"""
USAGE
=
f
"""
Usage:
Build math expressions using numeric values and operators.
Use any of the following functions and constants:
{
', '
.
join
(
ALLOWED_NAMES
.
keys
())
}
"""
def
evaluate
(
expression
):
"""Evaluate a math expression."""
# Compile the expression eventually raising a SyntaxError
# when the user enters an invalid expression
code
=
compile
(
expression
,
"<string>"
,
"eval"
)
# Validate allowed names
for
name
in
code
.
co_names
:
if
name
not
in
ALLOWED_NAMES
:
raise
NameError
(
f
"The use of '
{
name
}
' is not allowed"
)
# Evaluate the expression eventually raising a ValueError
# when the user uses a math function with a wrong input value
# e.g. math.sqrt(-10)
return
eval
(
code
,
{
"__builtins__"
:
{}},
ALLOWED_NAMES
)
def
main
():
"""Main loop: Read and evaluate user's input."""
print
(
WELCOME
)
while
True
:
# Read user's input
try
:
expression
=
input
(
f
"
{
PS1
}
"
)
except
(
KeyboardInterrupt
,
EOFError
):
raise
SystemExit
()
# Handle special commands
if
expression
.
lower
()
==
"help"
:
print
(
USAGE
)
continue
if
expression
.
lower
()
in
{
"quit"
,
"exit"
}:
raise
SystemExit
()
# Evaluate the expression and handle errors
try
:
result
=
evaluate
(
expression
)
except
SyntaxError
:
# If the user enters an invalid expression
print
(
"Invalid input expression syntax"
)
continue
except
(
NameError
,
ValueError
)
as
err
:
# If the user tries to use a name that isn't allowed
# or an invalid value to a given math function
print
(
err
)
continue
# Print the result if no error occurs
print
(
f
"The result is:
{
result
}
"
)
if
__name__
==
"__main__"
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录