f



How to print SRE_Pattern (regexp object) text for debugging purposes?

I need to print the regexp pattern text (SRE_Pattern object ) for
debugging purposes, is there any way to do it gracefully? I've came up
with the following hack, but it is rather crude... Is there an
official way to get the regexp pattern text?

>>> import re, pickle
>>> r = re.compile('^abc$', re.I)
>>> r
<_sre.SRE_Pattern object at 0xb7e6a330>

>>> ds = pickle.dumps(r)
>>> ds
"cre\n_compile\np0\n(S'^abc$'\np1\nI2\ntp2\nRp3\n."

>>> re.search("\n\(S'(.*)'\n", ds).group(1)
'^abc$'
>>>

-- Cheers, Dmitry
0
dmtr
6/17/2010 9:42:50 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

3 Replies
773 Views

Similar Articles

[PageSpeed] 9

dmtr wrote:
> I need to print the regexp pattern text (SRE_Pattern object ) for
> debugging purposes, is there any way to do it gracefully? I've came up
> with the following hack, but it is rather crude... Is there an
> official way to get the regexp pattern text?
> 
>>>> import re, pickle
>>>> r = re.compile('^abc$', re.I)
>>>> r
> <_sre.SRE_Pattern object at 0xb7e6a330>
> 
>>>> ds = pickle.dumps(r)
>>>> ds
> "cre\n_compile\np0\n(S'^abc$'\np1\nI2\ntp2\nRp3\n."
> 
>>>> re.search("\n\(S'(.*)'\n", ds).group(1)
> '^abc$'
> 

 >>> import re
 >>> r = re.compile('^abc$', re.I)
 >>> r.pattern
'^abc$'
 >>> r.flags
2

0
MRAB
6/17/2010 10:35:17 PM
On Jun 17, 3:35=A0pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
>
> =A0>>> import re
> =A0>>> r =3D re.compile('^abc$', re.I)
> =A0>>> r.pattern
> '^abc$'
> =A0>>> r.flags
> 2


Hey, thanks. It works.

Couldn't find it in a reference somehow.
And it's not in the inspect.getmembers(r).
Must be doing something wrong.....

-- Cheers, Dmitry
0
dmtr
6/17/2010 10:49:45 PM
dmtr wrote:
> On Jun 17, 3:35 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
>>  >>> import re
>>  >>> r = re.compile('^abc$', re.I)
>>  >>> r.pattern
>> '^abc$'
>>  >>> r.flags
>> 2
> 
> Hey, thanks. It works.
> 
> Couldn't find it in a reference somehow.
> And it's not in the inspect.getmembers(r).
> Must be doing something wrong.....
> 
Occasionally you'll find classes whose instances don't reveal all their
attributes to dir() and so forth, so it's always a good idea to
double-check the documentation.

The documentation mentions the .pattern and .flags attributes at:

     http://docs.python.org/library/re.html
     Section 7.2.4. Regular Expression Objects

(Python 2.6)
0
MRAB
6/17/2010 11:16:17 PM
Reply:

Similar Artilces:

[compilation error] debugging this error message "Object() in java.lang.Object cannot be applied to"
Hi,I have a class called Device that instantiates another class DbMoneyin this manner:=== return new DbMoney( money, hardware.getName() );===1) Both are in the same package, i.e. my.IMoney & my.Device2) When I compile them, javac complains with the error:Object() in java.lang.Object cannot be applied to (my.IMoney,java.lang.String) return new DbMoney( money, hardware.getName() ) ^Q) My arguments to the DbMoney constructor match that of its declaredmethod parameters. So that can't be the problem. What am I doingwrong ?Gavin On Mar 28, 3:25 pm, "Gavin&...

[Howto] Compiling debug Python extensions for non-debug Python
Every few months I get to working on a Python C extension (built with distutils) and discover a pointer error or the like where I'd like to be able to step into my DLL and see where the extension is going wonky. Now, the "proper" way to debug a DLL is apparently to build Python in debug mode (i.e. with the _d form dll), then build each and every extension I depend on (in debug mode), and then build each of my own extensions in debug mode. That takes forever when there's 8 or 9 complex-to-build extensions involved in the system generating the error, and is pointless ...

Printing a text file using Python
I have been trying to print a text file to my printer but the most I have been able to do is print one line. I have been using the following commands. dc = win32ui.CreateDC() dc.CreatePrinterDC() dc.SetMapMode(4) # This is UI_MM_LOENGLISH # With this map mode, 12 points is 12*100/72 units = 16 font = win32ui.CreateFont({'name' : 'Arial', 'height' : 16}) dc.SelectObject(font) f=open("./Reports/Report.txt","r") memory=f.read() f.close memory.split('\n') ...

printing line numbers for debugging purpose
Hi, All of the methods from my program return None on error (i.e; I do not want to assert and have the program exit). Is it possible to print the current source file name/line number ? ex: in C/C++ I would use the macros __FILE__ and __LINE__. Regards, Philippe -- *************************** Philippe C. Martin SnakeCard LLC www.snakecard.com *************************** "Philippe C. Martin" <philippecmartin@sbcglobal.net> writes: > All of the methods from my program return None on error (i.e; I do not > want to assert and have the program exit). > > Is it...

How to make this Python code print blocks of text?
This code prints output in rows like this: ****** ****** ****** How to make print in blocks like this? ***** ***** ****** ***** ***** ****** ***** ***** ***** start= int (raw_input("StartTable?")) upperlimit= int (raw_input ("FinalTable?")) cycle= start while cycle <= upperlimit: ........table= cycle ........counter= 0 ........while counter < 10: ...............counter= counter + 1 ...............print table, "X", counter, "=", counter * table ........cycle= cycle + 1 The program prints multyply tables like this 4 X1 = 4 4 X2 = 8 5 X 1= 5 5 X 2...

Special-purpose extension of Python -- new kinds of objects
Hello, I have an idea, thus far half-baked, to create a language called Ephemeral, for "flexible modeling & reasoning language". I'd like some advice about how to go about implementing it. Ephemeral would be Python plus additional constructs to support decision analysis -- what's needed are essentially specifications of probability and utility functions. Let's not worry about whether such a language would be usefult to anyone. The goal here is to merge the description of the probability and utility stuff with Python in general. I would like for objects of these ne...

Facing issue with Python loggin logger for printing object value
--14dae934117ba3987104d1ea76ab Content-Type: text/plain; charset=ISO-8859-1 Hi Team, i am new to python and i am using python loggin for log the value of the object. Below is my code : class field: field_name = "" length = 0 type = 0 value = "" def __init__(self, field_name, length, type, value): self.field_name = field_name self.length = length self.type = type self.value = value def toString(self): if self.type == 2: return self.value.zfill(self.length) else: ...

Re: Facing issue with Python loggin logger for printing object value
--14dae9340669d2b13704d1fd1162 Content-Type: text/plain; charset=ISO-8859-1 Hi Dave, Thanks for reply. I will really appreciate if you reply to my mail id and keep python list in cc, since everytime you reply my query i need to search the reply in the forwarding message of python list. Using logger.setLevel(logging.DEBUG) will log only debug message in the log file and discard other message from log file. So that's not solving the issue of my problem. I am looking into code to find out the issue. Thanks again for your effort.... --14dae9340669d2b13704d1fd1162 Content-Typ...

Finding values (text,numbers, regexps) in objects (arrays or key/value pairs)
I have an array of objects and I want to retrieve specific elements that match certain criteria, i.e. find value A in an object B The problem is that I cannot know what is A (text, object, reg exp, array) and what is B (text, object, array). Also, I must assume that B may be an array or a key/value object and that A may be anywhere (including 'children' objects). This is usually the case when I have a JSON response from an external source and want to find something in there but without hard-coding any specific methods or search cases. I think the best way to achieve this is to have...

Strange problem with xml.dom.minidom Text object (Python 2.3)
[Not sure if this message made it, so I'm resending it... apologies if it appears multiple times.] Howdy y'all, The following works in Python 2.2.2: Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from xml.dom.minidom import Text >>> t = Text("foo") >>> t <DOM Text node "foo"> So far, so good. We have production code that uses the Text class by initializing it with a string, and it work...

Text, search -regexp, elidden text
Hi. It has been a long time, but I'll spare you from tales how my Easter went, and stuff. No, I can't do that. You can skip starting from <here> all the way down there to next \n\n unless you are really interested. That I doubt. My Easter grass didn't grow and as a bonus even the seeds got mold. Dang. But otherwise, great time. Now to my point. I have been drooling for a while at {expand} but none seemed to be interested in wrapping Tcl alphas into Debian packages, and with very humble SparcStation 5 I was not that eager to compile it all by myself. However, I did, jus...

Printing text, image, text, image, ...
I have bunch of text files (about 20 lines) and bunch of image files. Is there a clean way to print text, then image, then text, then image, ...., as though there are in single continuous document? -- William Park <opengeometry@yahoo.ca>, Toronto, Canada ThinFlash: Linux thin-client on USB key (flash) drive http://home.eol.ca/~parkw/thinflash.html BashDiff: Super Bash shell http://freshmeat.net/projects/bashdiff/ ["Followup-To:" header set to comp.os.linux.misc.] On Fri, 23 Jun 2006 20:01:01 -0400, William Park staggered into the Black Sun and said: > I have [a] bu...

Show colored text on a text object
I have a chat program. The messages sent from users will be shown in different colors. What is the suitable object for that. The object should be scrollable. lrantisi wrote: > I have a chat program. > The messages sent from users will be shown in different colors. > What is the suitable object for that. Questions usually end in a question mark (e.g. '?') please use question marks in future. Most Swing components can render basic HTML, so that is one way to get colored text. For a more serious approach, it is probably better to use a JTextPane/JEditorPane and use a styled ...

conversion of Python object to perl object
Hi,=0AI have to call a perl method which takes a hash as its=A0argument fro= m a python module. Is there a way to convert python dictionary to perl hash= ( not hash ref)?=0A=0AThanks,=0ASrini=0A=0A=0A See the Web&#39;s brea= king stories, chosen by people like you. Check out Yahoo! Buzz. http://in.b= uzz.yahoo.com/ ...

Web resources about - How to print SRE_Pattern (regexp object) text for debugging purposes? - comp.lang.python

Resources last updated: 2/12/2016 11:57:35 AM