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

2010-06-17 Thread MRAB

dmtr wrote:

On Jun 17, 3:35 pm, MRAB  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)
--
http://mail.python.org/mailman/listinfo/python-list


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

2010-06-17 Thread dmtr
On Jun 17, 3:35 pm, MRAB  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.

-- Cheers, Dmitry
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-06-17 Thread MRAB

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2010-06-17 Thread dmtr
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
-- 
http://mail.python.org/mailman/listinfo/python-list