473,378 Members | 1,413 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,378 software developers and data experts.

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 1523
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael ALbanese | last post by:
I am developing a telephone directory for my company. I have Fist Name, Last Name, Department and Phone Number as fields. A user can search by entering data into any one, or combination of these...
3
by: Steve | last post by:
All -- This occasionally happens, and there's no set pattern on why this is... perhaps it happens a few times every few months or so. I have a custom ATL COM object that handles data from...
1
by: Srini | last post by:
Hi, I am working on a project and a portion of which involves receiving xml files on internet, extract values to build a string and pass that string to legacy system. I am planning on using...
3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
6
by: Whoever | last post by:
Here's what I have A custom collection: public class aUser { public string UserName { get {...} set {...} } public class UserList : System.Collection.CollectionBase { public void Add(aUser...
3
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on...
7
by: klynn | last post by:
I'm wondering what the best way to do the following: I have a page with a table. I built a custom class that creates a System.Web.UI.WebControls table object and return it to the webpage as a...
2
by: Glenn | last post by:
Hello Is it possible to pass arguements to a .net service once it is in a running state. If this is not possible , are they alternative ways in which to achive the same thing? Glenn
4
by: Val | last post by:
I have a complex object that I need to serialize. Rather than rely on a standard routine, which is called during the serialization/deserialization, I would like to be able to use my own functions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.