469,600 Members | 2,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Type of regular expression


Hello,

I wondered how I could test wether an argument was of type compiled
regexp:

from types import *

def do_something(arg):
if type(arg) is StringType:
print "%s is a string" % arg
elsif type(arg) is RegexpType: # <- This constant does not
# exist, it is only
# something I invented.
print "%s is a regexp" % arg
else
sys.exit("Argument must be *either* string or regular expression")

which is then called as:

do_something("a string")
do_something(re.compile("^F")

Regards

Joakim

--
/--------------------------------------------------------------------\
/ Joakim Hove / ho**@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
Jul 18 '05 #1
6 1543
Joakim Hove wrote:
I wondered how I could test wether an argument was of type compiled
regexp:


Both strings and compiled regular expressions can be compiled:
import re
r = re.compile("abc")
s = re.compile(r)
r is s True

Therefore I would not test beforehand, just rely on re.compile() to know
what it can deal with:
def do_something(s): .... try:
.... s = re.compile(s)
.... except TypeError:
.... sys.exit("Something went wrong")
.... # more stuff
.... do_something("abc")
do_something(r)
do_something(1)

Something went wrong

Peter

Jul 18 '05 #2

Peter Otten <__*******@web.de> writes:
Joakim Hove wrote:
I wondered how I could test wether an argument was of type compiled
regexp:
Both strings and compiled regular expressions can be compiled:


[...]
Therefore I would not test beforehand, just rely on re.compile() to know
what it can deal with:
def do_something(s):

... try:
... s = re.compile(s)
... except TypeError:
... sys.exit("Something went wrong")
... # more stuff
...


Thanks for answering, however I am afraid i posed the question
somewhat ambigously: The point is that i want the function to do
different things depending on the type of input:

def do_something(arg):
if type(arg) is RegexpType:
# Handle regular expression argument
elsif type(arg) is StringType:
# Handle string argument - which is something
# completely different.
else:
sys.exit("Wrong argument type")
Thanks - Joakim

--
/--------------------------------------------------------------------\
/ Joakim Hove / ho**@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
Jul 18 '05 #3
Joakim Hove wrote:

Peter Otten <__*******@web.de> writes:
Joakim Hove wrote:
I wondered how I could test wether an argument was of type compiled
regexp:


Both strings and compiled regular expressions can be compiled:


[...]
Therefore I would not test beforehand, just rely on re.compile() to know
what it can deal with:
> def do_something(s):

... try:
... s = re.compile(s)
... except TypeError:
... sys.exit("Something went wrong")
... # more stuff
...


Thanks for answering, however I am afraid i posed the question
somewhat ambigously: The point is that i want the function to do
different things depending on the type of input:


The easiest way to get the type of regular expressions:
import re
r = re.compile("")
RegexType = type(r)
del r
The types has examples where it's done in exactly the same way.
Now do the test:
isinstance(re.compile("abc"), RegexType) True isinstance("abc", RegexType) False

An alternative would be to test for the part of the interface you are
interested in (the match() method in the following example):
r = re.compile("abc")
if hasattr(r, "match"):

.... print "it's a regexp"
.... else:
.... print "it's a string"
....
it's a regexp

Peter
Jul 18 '05 #4
Peter Otten wrote:
The types has examples where it's done in exactly the same way.

The types module has examples where it's done in exactly the same way.
Jul 18 '05 #5

Peter Otten <__*******@web.de> writes:

The easiest way to get the type of regular expressions:
import re
r = re.compile("")
RegexType = type(r)
del r
The types has examples where it's done in exactly the same way.
Now do the test:
isinstance(re.compile("abc"), RegexType) True isinstance("abc", RegexType)

False


That was elegant - thanks a lot.
Joakim
--
/--------------------------------------------------------------------\
/ Joakim Hove / ho**@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
Jul 18 '05 #6
In article <4y************@skjellgran.ii.uib.no>,
Joakim Hove <ho**@bccs.no> wrote:

Thanks for answering, however I am afraid i posed the question
somewhat ambigously: The point is that i want the function to do
different things depending on the type of input:

def do_something(arg):
if type(arg) is RegexpType:
# Handle regular expression argument
elsif type(arg) is StringType:
# Handle string argument - which is something
# completely different.
else:
sys.exit("Wrong argument type")


That's a Bad Idea. What if someone gives you a Unicode string?
Generally speaking, type-based dispatch will run into problems sooner or
later. The better approach: you have to know at some point what kind of
data is being passed around -- when you create a target (usually a name)
for it. If you really need to know the type later on, create a
container that includes the necessary type information.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith, c.l.py
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Ron Adam | last post: by
2 posts views Thread by Bryce Budd | last post: by
4 posts views Thread by Buddy | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
18 posts views Thread by Q. John Chen | last post: by
7 posts views Thread by Billa | last post: by
2 posts views Thread by Helmut Jarausch | last post: by
25 posts views Thread by Mike | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.