By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

how to pass a custom object to re.search?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
<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

P: n/a
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

P: n/a
<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

P: n/a
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.