473,394 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simulating call-by-reference

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
------------------------------------------------------------------------
Nov 22 '05 #1
10 1455

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))

Nov 22 '05 #2

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))

Nov 22 '05 #3
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/>
Nov 22 '05 #4
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/>
Nov 22 '05 #5
Dan Sommers <me@privacy.net> wrote:
...
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.


I think you mean the Bunch idiom, rather than the Borg one (which has to
do with having instances of the same class share state).

Personally, I would rather pass myvar as well as the attribute names,
and set them with setattr, as I see some others already suggested.
Alex
Nov 22 '05 #6
On Thu, 17 Nov 2005 12:31:08 -0800,
al***@mail.comcast.net (Alex Martelli) wrote:
Dan Sommers <me@privacy.net> wrote:
...
Put the results into a dictionary (untested code follows!):
[ example code snipped ]
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.
I think you mean the Bunch idiom, rather than the Borg one (which has to
do with having instances of the same class share state).
Oops. <sheepish grin>

You're right.

Sorry.
Personally, I would rather pass myvar as well as the attribute names,
and set them with setattr, as I see some others already suggested.


I lose track of things too easily that way. YMMV.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 22 '05 #7
Dan Sommers <me@privacy.net> wrote:
...
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.


I think you mean the Bunch idiom, rather than the Borg one (which has to
do with having instances of the same class share state).

Personally, I would rather pass myvar as well as the attribute names,
and set them with setattr, as I see some others already suggested.
Alex
Nov 22 '05 #8
On Thu, 17 Nov 2005 12:31:08 -0800,
al***@mail.comcast.net (Alex Martelli) wrote:
Dan Sommers <me@privacy.net> wrote:
...
Put the results into a dictionary (untested code follows!):
[ example code snipped ]
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.
I think you mean the Bunch idiom, rather than the Borg one (which has to
do with having instances of the same class share state).
Oops. <sheepish grin>

You're right.

Sorry.
Personally, I would rather pass myvar as well as the attribute names,
and set them with setattr, as I see some others already suggested.


I lose track of things too easily that way. YMMV.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 22 '05 #9
On Thu, 17 Nov 2005 10:03:50 GMT, Rikard Bosnjakovic <bo*@REMOVETHIShack.org> 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.

You could tag your regexs with the foo bar baz names, and pre-compile them.
Then you could do something like
import re
re1 = re.compile(r'(?P<bar>\d+)') # find an int
re2 = re.compile(r'(?P<foo>[A-Z]+)') # find a cap seq
re3 = re.compile(r'(?P<baz>[a-z]+)') # find a lower case seq

data = 'abc12 34CAPS lowercase'

class myclass(object): pass # ?? ... class myotherclass(object): pass # ??? ... L = [ (re1, myclass), ... (re2, myclass),
... (re3, myotherclass),
... ] for (rx, cls) in L: ... m = rx.search(data)
... if m:
... setattr(cls, *m.groupdict().items()[0])
... myclass.bar '12' myclass.foo 'CAPS' myotherclass.baz 'abc'

Of course, this is only finding a single group, so this specific code
might not work for other searches you might like to do. Also, if you don't
need an alternate myotherclass, DRY says don't repeat it in L. I.e., you
could write (spelling myclass more conventionally)
class MyClass(object): pass # ?? ... L = (re1, re2, re3)
for (rx) in L: ... m = rx.search(data)
... if m: setattr(MyClass, *m.groupdict().items()[0])
... for k,v in MyClass.__dict__.items(): print '%15s: %r'%(k,v) ...
__module__: '__main__'
bar: '12'
baz: 'abc'
__dict__: <attribute '__dict__' of 'MyClass' objects>
foo: 'CAPS'
__weakref__: <attribute '__weakref__' of 'MyClass' objects>
__doc__: None for it in (it for it in MyClass.__dict__.items() if not it[0].startswith('_')): print '%15s: %r'%it

...
bar: '12'
baz: 'abc'
foo: 'CAPS'

The
setattr(MyClass, *m.groupdict().items()[0])

just makes an assignment of whatever comes out to be the first of name-tagged
matches (of which there has to at least one here also). If you want several name-tagged
matches in a single regex, you could do that and do a setattr for each item in m.groubdict().items().

What else you can do is only limited by your imagination ;-)

Regards,
Bengt Richter
Nov 22 '05 #10
On Thu, 17 Nov 2005 10:03:50 GMT, Rikard Bosnjakovic <bo*@REMOVETHIShack.org> 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.

You could tag your regexs with the foo bar baz names, and pre-compile them.
Then you could do something like
import re
re1 = re.compile(r'(?P<bar>\d+)') # find an int
re2 = re.compile(r'(?P<foo>[A-Z]+)') # find a cap seq
re3 = re.compile(r'(?P<baz>[a-z]+)') # find a lower case seq

data = 'abc12 34CAPS lowercase'

class myclass(object): pass # ?? ... class myotherclass(object): pass # ??? ... L = [ (re1, myclass), ... (re2, myclass),
... (re3, myotherclass),
... ] for (rx, cls) in L: ... m = rx.search(data)
... if m:
... setattr(cls, *m.groupdict().items()[0])
... myclass.bar '12' myclass.foo 'CAPS' myotherclass.baz 'abc'

Of course, this is only finding a single group, so this specific code
might not work for other searches you might like to do. Also, if you don't
need an alternate myotherclass, DRY says don't repeat it in L. I.e., you
could write (spelling myclass more conventionally)
class MyClass(object): pass # ?? ... L = (re1, re2, re3)
for (rx) in L: ... m = rx.search(data)
... if m: setattr(MyClass, *m.groupdict().items()[0])
... for k,v in MyClass.__dict__.items(): print '%15s: %r'%(k,v) ...
__module__: '__main__'
bar: '12'
baz: 'abc'
__dict__: <attribute '__dict__' of 'MyClass' objects>
foo: 'CAPS'
__weakref__: <attribute '__weakref__' of 'MyClass' objects>
__doc__: None for it in (it for it in MyClass.__dict__.items() if not it[0].startswith('_')): print '%15s: %r'%it

...
bar: '12'
baz: 'abc'
foo: 'CAPS'

The
setattr(MyClass, *m.groupdict().items()[0])

just makes an assignment of whatever comes out to be the first of name-tagged
matches (of which there has to at least one here also). If you want several name-tagged
matches in a single regex, you could do that and do a setattr for each item in m.groubdict().items().

What else you can do is only limited by your imagination ;-)

Regards,
Bengt Richter
Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jimif_fr | last post by:
How is it possible to simulate the keyboard entries (and the mouse events) of one program, from another program. The first program is not written for this purpose and is a not aware of the...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
47
by: Lauren Quantrell | last post by:
I have constructed the following code that simulates the common rollover effect when moving the mouse over a label (this example makes the label bold.) I'm wondering if anyone has come up with...
3
by: Doug Eleveld | last post by:
Hi Everyone, I have been playing aroung with 'object-oriented' C for a while now and I have come up with an interesting way to simulate C++ inheritance and non-member functions in C in a 100%...
6
by: bwahahahaha | last post by:
How can I inject the MouseMove events to move the mouse programatically in C#, or is this not possible and I have to drop to Win32 for this?
5
by: YR | last post by:
Hello, I am tasked to write an application for a travel agency, that should be able to get a price for airline tickets from airline's website. Generally, airlines don't provide any web services...
6
by: Timo | last post by:
The IT Manager where I'm working prizes "no touch" deployment above all else. The instructions I've been given are basically this: if it's possible in ASP.NET, write it in ASP.NET; use WinForms...
8
by: John Dann | last post by:
I've either completely forgotten how to do this or it's different in VB.Net from VB6, but how do I call the routine for a menu click programmatically? (ie I want to simulate and call...
0
by: Andrew Teece | last post by:
HELP! I am calling a webservice as a result of an event in an infragistics control (well, various controls). Problem is that if a user clicks around the GUI alot, then my code crashes (because...
4
by: Bill Pursell | last post by:
I've been thinking of doing things like the following: $ cat foo.h struct foo{ int a; void (*init)(struct foo *, int); }; void new_foo(struct foo *self);
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.