模拟按引用调用 [英] Simulating call-by-reference

查看:65
本文介绍了模拟按引用调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一些代码。基本上,代码在文本上运行一堆

regexp-searching(> 10)并将匹配存储在另一个变量中。


喜欢这个:


re1 = r''..(。*)..''

re2 = r''....''

re3 = r''。(。*)..''

...

m = re.search(re1,data)

如果m:

myclass.bar = m.group(1)


m = re.search(re2,data)

如果m:

myclass.foo = m.group(1)


m = re.search(re3,data)

如果m:

myclass.baz = m.group(1)

虽然这段代码有效,但看起来并不是很好看。 />

我想要的是把它重写成这样的东西:


l = [(re1,myclass.bar),

(re2,myclass.foo),

(re3,myclass.baz),

]


for(x ,y)在l:

m = re.search(x,y)

如果m:

y = m.group(1)


但是因为Python不起作用那个想法注定要失败。我正在寻找的是
寻找其他(更好)的方法或指针来完成

清理这个任务。

-

此致,| http://bos.hack.org/cv/

Rikard Bosnjakovic |代码厨师 - 将为食物做饭

----------------------------------- -------------------------------------

解决方案



Rikard Bosnjakovic写道:

我正在整理一些代码。基本上,代码在文本上运行一堆
regexp-searching(> 10)并将匹配存储在不同的变量中。

像这样:
re1 = r''..(。*)..''
re2 = r''....''
re3 = r''。(。*)..''< br => ...
m = re.search(re1,data)
如果m:
myclass.bar = m.group(1)

m = re.search(re2,data)
如果m:
myclass.foo = m.group(1)

m = re.search(re3,data)
如果m:
myclass.baz = m.group(1)

虽然这段代码有效,但看起来并不是很好看。

什么我想把它改写成这样的东西:

l = [(re1,myclass.bar),
(re2,myclass.foo),
(re3,myclass .b,(

对于(x,y)中的l:
m = re.search(x,y)
如果m:
y = m.group(1)

但是因为Python不起作用方式,这个想法注定要失败。我正在寻找的其他(更好的)方法或指针来完成
清理任务。
---------------- -




我相信你可以使用setattr / getattr致电


l = [(re1,myclass," bar")]


代表x,y,z in l:

m = re.search(x,getattr(y,z))

如果m:setattr(y,z,m.group(1))




Rikard Bosnjakovic写道:

我正在整理一些代码。基本上,代码在文本上运行一堆
regexp-searching(> 10)并将匹配存储在不同的变量中。

像这样:
re1 = r''..(。*)..''
re2 = r''....''
re3 = r''。(。*)..''< br => ...
m = re.search(re1,data)
如果m:
myclass.bar = m.group(1)

m = re.search(re2,data)
如果m:
myclass.foo = m.group(1)

m = re.search(re3,data)
如果m:
myclass.baz = m.group(1)

虽然这段代码有效,但看起来并不是很好看。

什么我想把它改写成这样的东西:

l = [(re1,myclass.bar),
(re2,myclass.foo),
(re3,myclass .b,(

对于(x,y)中的l:
m = re.search(x,y)
如果m:
y = m.group(1)

但是因为Python不起作用方式,这个想法注定要失败。我正在寻找的其他(更好的)方法或指针来完成
清理任务。
---------------- -




我相信你可以使用setattr / getattr致电


l = [(re1,myclass," bar")]


代表x,y,z in l:

m = re.search(x,getattr(y,z))

如果m:setattr(y,z,m.group(1))


2005年11月17日星期四,格林威治标准时间10:03:50,
Rikard Bosnjakovic< bo*@REMOVETHIShack.org>写道:

我想要的是将其重写为:
l = [(re1,myclass.bar),
(re2,myclass.foo) ),
(re3,myclass.baz),
]
for(x,y)in l:
m = re.search(x,y)
如果m:
y = m.group(1)
但是由于Python没有这样做,这个想法注定要失败。我正在寻找的其他(更好的)方法或指针来完成这个清理任务。




将结果放入一本字典(未经测试的代码如下!):


l = [(re1,''bar''),

(re2,''foo'') ,

(re3,''baz''),

]

results = {}

for( regexp,key)in l:

m = re.search(regexp,data)

如果m:

结果[key] = m。 group(1)


现在你可以将结果作为结果[''foo'']等来访问。或者查看

Borg模式ASPN食谱,你可以访问结果

results.foo等。


问候,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>


I''m tidying up some code. Basically, the code runs a bunch of
regexp-searches (> 10) on a text and stores the match in a different variable.

Like this:

re1 = r'' ..(.*).. ''
re2 = r'' .... ''
re3 = r'' .(.*).. ''
...
m = re.search(re1, data)
if m:
myclass.bar = m.group(1)

m = re.search(re2, data)
if m:
myclass.foo = m.group(1)

m = re.search(re3, data)
if m:
myclass.baz = m.group(1)
While this code works, it''s not very good looking.

What I want is to rewrite it to something like this:

l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
]

for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1)

But since Python doesn''t work that way, that idea is doomed. What I''m
looking for are other (better) ways or pointers to accomplish this task of
cleanup.
--
Sincerely, | http://bos.hack.org/cv/
Rikard Bosnjakovic | Code chef - will cook for food
------------------------------------------------------------------------

解决方案


Rikard Bosnjakovic wrote:

I''m tidying up some code. Basically, the code runs a bunch of
regexp-searches (> 10) on a text and stores the match in a different variable.

Like this:

re1 = r'' ..(.*).. ''
re2 = r'' .... ''
re3 = r'' .(.*).. ''
...
m = re.search(re1, data)
if m:
myclass.bar = m.group(1)

m = re.search(re2, data)
if m:
myclass.foo = m.group(1)

m = re.search(re3, data)
if m:
myclass.baz = m.group(1)
While this code works, it''s not very good looking.

What I want is to rewrite it to something like this:

l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
]

for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1)

But since Python doesn''t work that way, that idea is doomed. What I''m
looking for are other (better) ways or pointers to accomplish this task of
cleanup.
-----------------



I believe you can use the "setattr/getattr" call

l = [ (re1, myclass, "bar") ]

for x,y,z in l:
m = re.search(x,getattr(y,z))
if m: setattr(y,z,m.group(1))



Rikard Bosnjakovic wrote:

I''m tidying up some code. Basically, the code runs a bunch of
regexp-searches (> 10) on a text and stores the match in a different variable.

Like this:

re1 = r'' ..(.*).. ''
re2 = r'' .... ''
re3 = r'' .(.*).. ''
...
m = re.search(re1, data)
if m:
myclass.bar = m.group(1)

m = re.search(re2, data)
if m:
myclass.foo = m.group(1)

m = re.search(re3, data)
if m:
myclass.baz = m.group(1)
While this code works, it''s not very good looking.

What I want is to rewrite it to something like this:

l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
]

for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1)

But since Python doesn''t work that way, that idea is doomed. What I''m
looking for are other (better) ways or pointers to accomplish this task of
cleanup.
-----------------



I believe you can use the "setattr/getattr" call

l = [ (re1, myclass, "bar") ]

for x,y,z in l:
m = re.search(x,getattr(y,z))
if m: setattr(y,z,m.group(1))


On Thu, 17 Nov 2005 10:03:50 GMT,
Rikard Bosnjakovic <bo*@REMOVETHIShack.org> wrote:

What I want is to rewrite it to something like this: l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
] for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1) But since Python doesn''t work that way, that idea is doomed. What I''m
looking for are other (better) ways or pointers to accomplish this
task of cleanup.



Put the results into a dictionary (untested code follows!):

l = [ (re1, ''bar''),
(re2, ''foo''),
(re3, ''baz''),
]
results = {}
for (regexp, key) in l:
m = re.search(regexp, data)
if m:
results[key] = m.group(1)

Now you can access the results as results[''foo''], etc. Or look up the
Borg pattern in the ASPN cookbook and you can access the results as
results.foo, etc.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>


这篇关于模拟按引用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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