为作业编写代码。代码运行良好,但是当我尝试该函数时,我收到此 #'ed 错误(我不明白)。该代码应该采用坐标数据帧,保留坐标,并添加表示坐标所属半球的列。所以问题是如何更改函数/代码以消除错误?
此外,该代码使用了 sp 和 foreach 包。
这是数据框输入和我的错误输出:
set.seed(10)
n <- 10
df <- data.frame(xpos=runif(n,0,360),ypos=runif(n,-90,90))
df
outHemisphere <- hemisphereSummary(df=df)
outHemisphere
# Error in eval(expr, envir, enclos) : argument is missing, with no default
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的(在设置种子和 df 位于工作区之后):
outHemisphere <- hemisphereSummary(df=df)
outHemisphere
# coordinates EWhemisphere NShemisphere
#1 (182.692, 27.298) W N
#2 (110.437, 12.1928) E N
#3 (153.687, -69.5684) E S
#4 (249.517, 17.2666) W N
#5 (30.6489, -25.551) E S
#6 (81.1572, -12.8143) E S
#7 (98.831, -80.6574) E S …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中我想,让用户通过标准输入三个值,每种类型的float,Fraction或者int,所以我不能申请ast.literal_eval输入得到结果,我想要的。
我读到Eval 真的很危险,显示
eval提供意见的人如何利用弱点,以及eval调用更安全的方法。我应用了作者的建议并编写了此代码,替换了默认值eval:
import builtins
class DoubleUnderscoreInEval(ValueError):
pass
def eval(expression, globals={}, locals=None):
if '__' in expression:
raise DoubleUnderscoreInEval('Using __ in eval is not allowed for safety reasons.')
else:
if '__builtins__' not in globals:
globals['__builtins__']={}
return builtins.eval(expression, globals, locals)
Run Code Online (Sandbox Code Playgroud)
builtins.eval通过我的代码使用是否完全安全?
如果不是,我可以使eval调用完全安全吗?(鼓励绕过我的限制的字符串)
如果是,如何?如果不是,我可以用什么代替?
我是 Python 的初学者,所以鼓励任何告诉我如何改进我的代码的评论。
我如何使用 eval 在其他类中创建类定义?
evalstr = str("class MyScreen(Screen):\n\tpass\n")
eval(evalstr)
Run Code Online (Sandbox Code Playgroud)
我想在其他类方法中执行此代码。但它返回一个错误。
我在学习 ZSH 的过程中遇到了好奇心,并且我很难找到与此相关的信息。我想知道为什么这不起作用的技术解释(定义然后在单个 eval 调用中扩展别名):
eval "alias d='echo hello'; d"
zsh: command not found: d
Run Code Online (Sandbox Code Playgroud)
而这确实有效:
eval "function d = { echo hello; }; d"
hello
Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种简单的方法可以避免在以下代码中使用 eval:
eval('6001 >= 6005')
Run Code Online (Sandbox Code Playgroud)
在了解到 eval 不是一个好的做法之后,我一直在寻找一个简单的替代方案,尤其是当您不知道谁将使用该程序时。
我使用它的背景是这样的:
两个比较变量中的每一个都是 pandas 数据框列中的值,因此它也可能如下所示:
eval('"my_city_name" == "your_city_name"')
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!谢谢
所以,我正在制作一个程序(希望如此),它是一个“改进的”命令提示符。
在我的程序中,我希望它从文本文件中获取所有行。
文本文件将包含如下所示的行:
desktop=C:\Users\%username%\Desktop
scripts=D:\Scripts
Run Code Online (Sandbox Code Playgroud)
我希望我的程序做的是通读它,并创建新的变量,例如:
desktop = "C:\Users\%username%\Desktop"
scripts = "D:\Scripts"
Run Code Online (Sandbox Code Playgroud)
我知道可能有更好、更安全的方法来做到这一点,但现在,我想这样做。到目前为止,这是我的代码(它不起作用):
with open("custom_shortcuts.txt","r") as f:
customShortcuts = [x.strip('\n') for x in f.readlines()]
for i in customShortcuts:
parts = i.split("=")
varName = str(parts[0])
varValue = str(parts[1])
eval("varName = varValue")
Run Code Online (Sandbox Code Playgroud)
如您所见,我尝试使用 eval(),但失败了。
似乎不可能eval在 Node.js ES6 中使用创建变量,但我不明白为什么。这在 CentOS 7 上发生在我身上,但我不认为操作系统是这里的问题。
常规 Node.js 文件(test.js):
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
使用 .mjs 扩展名制作相同的文件以与 Node.js ES6 (test.mjs) 一起运行:
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
之后,使用 Node.js 和 Node.js ES6 运行 2 个文件:
$ node test.js
1
$ node --experimental-modules test.mjs
(node:9966) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: a is not defined
at file:///temp/test.mjs:2:13
at ModuleJob.run (internal/modules/esm/module_job.js:96:12)
Run Code Online (Sandbox Code Playgroud)
它是与 ES6 相关的问题吗?我在浏览器的控制台上试过,问题是一样的:
>> eval("var a=1"); console.log(a);
1
>> class c { static f(){ eval("var a=1"); console.log(a); } }
c.f()
ReferenceError: …Run Code Online (Sandbox Code Playgroud) 我曾尝试寻找与此类似的问题来为我提供解决方案,但我找不到一个能完全回答我的确切问题的问题。
我想在 eval 调用中的子 shell 中运行命令,并获取子 shell 中调用的函数返回的状态代码。
例如,我调用这样的命令:
eval "$(some_command)"
if [ "${?}" -ne 0 ]
then
# do stuff if `some_command` returned status code not equal to zero
fi
Run Code Online (Sandbox Code Playgroud)
该some_command函数返回环境变量及其分配的列表,如下所示:
$ some_command # Execute some_command in a standard fashion without eval or subshell
some_env_variable='some_value'
another_env_variable='another value'
Run Code Online (Sandbox Code Playgroud)
目标是运行这个命令将这些环境变量添加到当前环境。唯一的方法是some_command在子 shell 中调用,并让 eval 评估结果输出。如果我在没有子 shell 的情况下执行此操作,eval 将简单地运行some_command,但不会评估其输出以将环境变量添加到当前环境。
这有效:
$ eval "some_command"
-bash: some_command: command not found
$ echo $?
127
Run Code Online (Sandbox Code Playgroud)
这有效:
$ $(some_command)
-bash: some_command: …Run Code Online (Sandbox Code Playgroud) 我正在尝试应用更改函数代码的装饰器,然后使用更改后的代码执行此函数。
下面是temp带有示例功能的模块。我只是希望函数返回[*args, *kwargs.items(), 123]而不是[*args, *kwargs.items()]ifsome_decorator应用于此函数。
编辑:请注意,这只是一个玩具示例,我不打算将新值附加到列表中,而是重写一大块函数。
from inspect import getsource
def some_decorator(method):
def wrapper(*args, **kwargs):
source_code = getsource(method)
code_starts_at = source_code.find('):') + 2
head = source_code[:code_starts_at]
body = source_code[code_starts_at:]
lines = body.split('\n')
return_line = [i for i in lines if 'return' in i][0]
old_expr = return_line.replace(' return ', '')
new_expr = old_expr.replace(']', ', 123]')
new_expr = head + '\n' + ' return ' + new_expr
return eval(new_expr)
return wrapper
@some_decorator
def example_func(*args, …Run Code Online (Sandbox Code Playgroud) 使用一串整数:
myStr = '3848202160702781329 2256714569201620911 1074847147244043342'
Run Code Online (Sandbox Code Playgroud)
如何在保持字符串数据类型的同时计算字符串整数的平均值?这是我在伪代码中的想法:
sum = 0
res = eval(myStr[i] + ' + sum')
avg = res/len(myStr)
Run Code Online (Sandbox Code Playgroud)