473,598 Members | 3,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.BufferTyp e 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 1531
<br************ ***@gmail.comwr ote:
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(c ustom_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(c ustom_object))
I've done that (and added __unicode__ too). I only didn't want to, I
want to do:

re.search(custo m_object)

so, code that worked before as:

re.search(paren tobj.custom_obj ect)

don't have to be changed for:

re.search(str(p arentobj.custom _object))

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

Aug 17 '07 #3
<br************ ***@gmail.comwr ote:
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(custo m_object)

so, code that worked before as:

re.search(paren tobj.custom_obj ect)

don't have to be changed for:

re.search(str(p arentobj.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_compi le = re._compile

def _wrapped_compil e(*key):
try:
custom_compile = key[0].__compile__
except AttributeError:
return _original_compi le(*key)
else:
return custom_compile( *key[1:])

re._compile = _wrapped_compil e
class Aaa(object):
def __compile__(sel f, *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
4062
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 fields. I have been trying to write a custom validation control that will test each of the textboxes and reject the request if they are ALL blank.
3
9284
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 multiple pages and sites on my server. Occasionally, I'll get the following errors: ASP_0241|CreateObject_Exception|-|ASP_0241|CreateObject_Exception|42|ASP_0177_:_c0000005|Server.CreateObject_Failed 500
1
2331
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 XMLReader to read passed xml file and extract values that need to be mapped to string buffer. However, in this method I am not able to get the full path (ie., path information from root to this node)
3
25256
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 user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
6
1854
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 user) {...} public aUser this {...}
3
2805
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 servicing a number of different web service operations from a single application, and because it's all XML, I've been scoping everything to a specific namespace targeting a specific application (these are all InfoPath forms). It seems that the...
7
1449
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 property. But its not reading the table correctly. It doesnt see the tablerows and tablecell objects. First, I'm not sure why its not reading the table properly? Second, I wonder if there's a better way to do this? Maybe a custom control? I haven't...
2
2761
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
6442
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 that would convert this object into a string and would then write that string to a file. Upon deserialization, I need to be able to call another custom function to recreate the object. I have looked and both implementing the ISerializable...
0
8395
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6719
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.