Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Type of regular expression

Reply
Thread Tools

Type of regular expression

 
 
Joakim Hove
Guest
Posts: n/a
 
      07-01-2004



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 / http://www.velocityreviews.com/forums/(E-Mail Removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-01-2004
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

 
Reply With Quote
 
 
 
 
Joakim Hove
Guest
Posts: n/a
 
      07-02-2004

Peter Otten <(E-Mail Removed)> 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 / (E-Mail Removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-02-2004
Joakim Hove wrote:

>
> Peter Otten <(E-Mail Removed)> 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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-02-2004
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.
 
Reply With Quote
 
Joakim Hove
Guest
Posts: n/a
 
      07-02-2004

Peter Otten <(E-Mail Removed)> 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 / (E-Mail Removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      07-06-2004
In article <(E-Mail Removed)>,
Joakim Hove <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith, c.l.py
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments