很难理解'eval' [英] Hard to understand 'eval'

查看:110
本文介绍了很难理解'eval'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在eval语句中给我语法错误似乎很奇怪。

我正在仔细查看它但是我看不出任何瑕疵。


这里是代码的一部分:

stn_items中nn的


value = eval(''cp。%s''%nn)

if value和(nn in''log,trash,multithread,verbose,download''):

cfl [wchkey] [nn] = chkbool(价值)

继续

如果价值:

cnfg =' 'cfl [wchkey] [nn] = _%s(值)''%nn

eval(cnfg)


和pdb上的输出:


(Pdb)p cnfg

''cfl [wchkey] [nn] = _ append(value)''

(Pdb )p cfl [wchkey] [nn]

False

(Pdb)eval(''cfl [wchkey] [nn] = _append(value)'')

*** SyntaxError:语法无效(< string>,第1行)

(Pdb)p值

''230k''

(Pdb)p nn

''追加''


显然我有一个_append()函数可以将给定值转换为十进制。


其他eval在此之前没有发出问题而且相当复杂,

但是我没有在这里看到错误。

我想学习一堂课,可能会得到一个字符串并将其转换为

函数,一旦在程序中找到它。


-

Mailsweeper主页: http://it.geocities.com/call_me_not_now/index.html

解决方案

11月14日,10:31,TheSaint< fc14301 ... @ icqmail.comwrote:





在eval语句中给出语法错误似乎很奇怪。

我正在寻找仔细看,但我看不出任何缺陷。


这里是代码的一部分:


for nn in stn_items:

value = eval(''cp。%s''%nn)



Hem ...不明显来自这个片段,但是

getattr(cp,nn)有什么问题?


04:08,domenica 15 giugno 2008 br *************** **@gmail.com 写道:


getattr(cp,nn)有什么问题?



进入这些编程方式的学习曲线。

gettattr是否运行传入的代码?

考虑到nn是函数的名称,它将被调用,并且(cfl,

value)是传递给该函数的参数。


我'我会在getattr上使用它。

-

Mailsweeper主页: http://it.geocities.com/call_me_not_now/index.html


06:34 ,domenica 15 giugno 2008 Dennis Lee Bieber写道:


>对于stn_items中的nn:


我在Agent中查看时发现语法错误...缺少

在for下的缩进级别



该程序不会抱怨错误的缩进,我大多肯定是错误的

复制粘贴错误。

错误不会出现在那里。


。你也不需要继续,如果你改成第二个如果进入elif。



我的错误,我认为这改善了循环。


是一个if .... elif .... elif探测第一个匹配的情况并放弃

剩余的支票?


什么类型的结构是cfl?



你接近了,这是一本字典词典,我正在尝试更新

吧。
< blockquote class =post_quotes>
想知道这个神秘的_append()函数应该做什么;



Append()是关于文件记录的传统名称。

可以选择设置字节大小的配额。 br />


嗯......我猜你的意思是从文本十进制转换



它不是所以,函数查看字符串以查看是否以K或M结尾,

表示Kbytes或Mbytes。它将返回十进制转换。

如果该值设置为布尔值,那么当它为True时它将附加到日志

或在那里停止追加'是配额。


def _append(c,v):

RE_BYTE = re.compile(r''^ [\d] +( K | M)

Hi,

It seems to be strange that give me syntax error inside an eval statement.
I''m looking at it carefully but I can''t see any flaw.

Here it''s part of the code:

for nn in stn_items:
value= eval(''cp.%s'' %nn)
if value and (nn in ''log, trash, multithread, verbose, download''):
cfl[wchkey][nn]= chkbool(value)
continue
if value:
cnfg= ''cfl[wchkey][nn]= _%s(value)'' %nn
eval(cnfg)

And the output on pdb:

(Pdb) p cnfg
''cfl[wchkey][nn]=_append(value)''
(Pdb) p cfl[wchkey][nn]
False
(Pdb) eval(''cfl[wchkey][nn]= _append(value)'')
*** SyntaxError: invalid syntax (<string>, line 1)
(Pdb) p value
''230k''
(Pdb) p nn
''append''

Obviously I''ve an _append() function to convert into decimal the given value.

Other "eval" before this not issuing problems and also rather complicated,
but I''m not seeing the error here.
I''d like to study a class that might get a string and convert it into
function, once it''s found inside the program.

--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html

解决方案

On 14 juin, 10:31, TheSaint <fc14301...@icqmail.comwrote:

Hi,

It seems to be strange that give me syntax error inside an eval statement.
I''m looking at it carefully but I can''t see any flaw.

Here it''s part of the code:

for nn in stn_items:
value= eval(''cp.%s'' %nn)

Hem... Not obvious from this snippet, but what''s wrong with
getattr(cp, nn) ?


On 04:08, domenica 15 giugno 2008 br*****************@gmail.com wrote:

what''s wrong with getattr(cp, nn) ?

The learning curve to get into these programming ways.
Does gettattr run the snippet passed in?
Considering that nn is a name of function, which will be called and (cfl,
value) are the parameters to passed to that function.

I''ll spend some bit on getattr use.
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html


On 06:34, domenica 15 giugno 2008 Dennis Lee Bieber wrote:

> for nn in stn_items:

I already see a syntax error when viewing that in Agent... A missing
indent level under the "for"

The program don''t complain wrong indentation, I mostly sure a wrong
copy-paste error.
Error doesn''t come up there.

. You also don''t need the continue if you change the second if into elif.

My mistake, I thought that was improving the loop.

is it an if....elif....elif probing only the first matching case and drop the
remaining checks?

And what type of structure is "cfl"?

You got close, that''s a dictionary of dictionaries and I''m trying to updating
it.

wonder what this mysterious _append() function is supposed to be doing;

Append() is a conventional name regarding a file logging.
There would be an option to set a quota of bytes size.

Huh... I presume you mean to convert from a text decimal

it isn''t so, the function look at the string to see if ending by K or M,
which means Kbytes or Mbytes. It''ll return the decimal conversion.
If the value is set as boolean value, then it will do appending to the log
when it True or stop appending when there''s a quota.

def _append(c, v):
RE_BYTE= re.compile(r''^[\d]+(k|m)?


这篇关于很难理解'eval'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆