469,588 Members | 2,879 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

how to pass a custom object to re.search?

Hi,

sorry if this is a faq. I've searched and got no result. I'm willing
to pass a custom object to re.search method, but I'm getting the
error:

TypeError: expected string or buffer

I don't want to make my object to inherit from basestring (nor I know
how to do it...). Then I was left with 'buffer'. I tried to make my
object inherit from types.BufferType and got the error:

TypeError: Error when calling the metaclass bases
type 'buffer' is not an acceptable base type

If I call the function buffer passing an instance of my object I get:

TypeError: buffer object expected

Is it possible to make what I want (to pass a custom object to
re.search)?

regards,
Bruno

Aug 17 '07 #1
4 1448
<br***************@gmail.comwrote:
Is it possible to make what I want (to pass a custom object to
re.search)?
Try to implement __str__() for your object and provide a string
representation for it.

re.search(str(custom_object))

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Aug 17 '07 #2
Try to implement __str__() for your object and provide a string
representation for it.

re.search(str(custom_object))
I've done that (and added __unicode__ too). I only didn't want to, I
want to do:

re.search(custom_object)

so, code that worked before as:

re.search(parentobj.custom_object)

don't have to be changed for:

re.search(str(parentobj.custom_object))

and I'm also curious to know if it is possible to do that... :-)

Aug 17 '07 #3
<br***************@gmail.comwrote:
and I'm also curious to know if it is possible to do that... :-)
Only if re.search() doesn't check for the type of the argument, which it
seems it does.

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Aug 17 '07 #4
br***************@gmail.com wrote:
I've done that (and added __unicode__ too). I only didn't want to, I
want to do:

re.search(custom_object)

so, code that worked before as:

re.search(parentobj.custom_object)

don't have to be changed for:

re.search(str(parentobj.custom_object))

and I'm also curious to know if it is possible to do that... :-)
Not without monkeypatching the re module:

import re

_original_compile = re._compile

def _wrapped_compile(*key):
try:
custom_compile = key[0].__compile__
except AttributeError:
return _original_compile(*key)
else:
return custom_compile(*key[1:])

re._compile = _wrapped_compile
class Aaa(object):
def __compile__(self, *args):
return re.compile("[Aa]+")

print re.findall(Aaa(), "a yadda so whaaaat")

Peter
Aug 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael ALbanese | last post: by
6 posts views Thread by Whoever | last post: by
7 posts views Thread by klynn | last post: by
4 posts views Thread by Val | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.