Type of regular expression

J

Joakim Hove

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 / (e-mail address removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
P

Peter Otten

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

Both strings and compiled regular expressions can be compiled:
True

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

Peter
 
J

Joakim Hove

Peter Otten said:
Joakim said:
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:
... 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 / (e-mail address removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
P

Peter Otten

Joakim said:
Peter Otten said:
Joakim said:
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:

The types has examples where it's done in exactly the same way.
Now do the test:
False

An alternative would be to test for the part of the interface you are
interested in (the match() method in the following example):
.... print "it's a regexp"
.... else:
.... print "it's a string"
....
it's a regexp

Peter
 
P

Peter Otten

Peter said:
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.
 
J

Joakim Hove

The easiest way to get the type of regular expressions:


The types has examples where it's done in exactly the same way.
Now do the test:

False

That was elegant - thanks a lot.


Joakim


--
/--------------------------------------------------------------------\
/ Joakim Hove / (e-mail address removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
A

Aahz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top