Type of regular expression

Discussion in 'Python' started by Joakim Hove, Jul 1, 2004.

  1. Joakim Hove

    Joakim Hove Guest

    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 / / (55 5) 84076 | \
    | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
    | CMU | 5231 Paradis |
    \ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
    \--------------------------------------------------------------------/
    Joakim Hove, Jul 1, 2004
    #1
    1. Advertising

  2. Joakim Hove

    Peter Otten Guest

    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
    Peter Otten, Jul 1, 2004
    #2
    1. Advertising

  3. Joakim Hove

    Joakim Hove Guest

    Peter Otten <> 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 / / (55 5) 84076 | \
    | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
    | CMU | 5231 Paradis |
    \ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
    \--------------------------------------------------------------------/
    Joakim Hove, Jul 2, 2004
    #3
  4. Joakim Hove

    Peter Otten Guest

    Joakim Hove wrote:

    >
    > Peter Otten <> 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
    Peter Otten, Jul 2, 2004
    #4
  5. Joakim Hove

    Peter Otten Guest

    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.
    Peter Otten, Jul 2, 2004
    #5
  6. Joakim Hove

    Joakim Hove Guest

    Peter Otten <> 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 / / (55 5) 84076 | \
    | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
    | CMU | 5231 Paradis |
    \ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
    \--------------------------------------------------------------------/
    Joakim Hove, Jul 2, 2004
    #6
  7. Joakim Hove

    Aahz Guest

    In article <>,
    Joakim Hove <> 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 () <*> http://www.pythoncraft.com/

    "Typing is cheap. Thinking is expensive." --Roy Smith, c.l.py
    Aahz, Jul 6, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,271
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    831
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,921
    vforvikash
    Dec 29, 2008
  4. Joakim Hove

    Type of regular expression

    Joakim Hove, Jul 1, 2004, in forum: Python
    Replies:
    0
    Views:
    285
    Joakim Hove
    Jul 1, 2004
  5. Diego UTN-FRP
    Replies:
    4
    Views:
    4,475
    Diego UTN-FRP
    Nov 29, 2009
Loading...

Share This Page