如何在Python中组合switch case和regex

2024-10-01 11:38:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过将字符串与正则表达式序列匹配来处理它。当我试图避免嵌套if-then时,我想到了switch case。如何用Python编写以下结构?谢谢你

switch str:
   case match(regex1):
       # do something
   case match(regex2):
       # do sth else

我知道Perl允许这样做。是Python吗?


Tags: 字符串ifmatch序列结构doelsesomething
3条回答

你在找pyswitch(免责声明:我是作者)。使用它,您可以执行以下操作,这与您在问题中给出的示例非常接近:

from pyswitch import Switch

mySwitch = Switch()

@myswitch.caseRegEx(regex1)
def doSomething(matchObj, *args, **kwargs):
    # Do Something
    return 1

@myswitch.caseRegEx(regex2)
def doSomethingElse(matchObj, *args, **kwargs):
    # Do Something Else
    return 2

rval = myswitch(stringYouWantToSwitchOn)

在我链接的URL上有一个更全面的例子。pyswitch不仅限于打开正则表达式。在内部,pyswitch使用一个调度系统,类似于上面其他人给出的示例。我只是厌倦了每次需要这种调度系统时都要一遍又一遍地重新编写相同的代码框架,所以我编写了pyswitch。

首先考虑why there is no case statement in Python。所以让你的大脑复位,忘掉它们。

可以使用对象类、函数装饰器或函数字典来获得相同或更好的结果。

下面是一个简单的例子:

#!/usr/bin/env python
import re

def hat(found):
    if found: print "found a hat"
    else: print "no hat"

def cat(found):
    if found: print "found a cat"
    else: print "no cat"

def dog(found):    
    if found: print "found a dog"
    else: print "no dog"

st="""
Here is the target string
with a hat and a cat
no d o g 
end
"""

patterns=['hat', 'cat', 'dog']
functions=[hat,cat,dog]

for pattern,case in zip(patterns,functions):
    print "pattern=",pattern
    case(re.search(pattern,st))

C样式的case/switch语句也“失败”,例如:

   switch(c) {
       case 'a':
       case 'b':
       case 'c':  do_abc();
                  break;
       ... other cases...
   }

使用元组和可调用列表,可以获得类似的行为:

st="rat kitten snake puppy bug child"

def proc1(st): print "cuddle the %s" % st
def proc2(st): print "kill the %s" % st
def proc3(st): print "pick-up the %s" % st
def proc4(st): print "wear the %s" % st
def proc5(st): print "dispose of the %s" %st
def default(st): print "%s not found" % st

dproc={ ('puppy','kitten','child'): 
            [proc3, proc1], 
        ('hat','gloves'): 
            [proc3, proc4], 
        ('rat','snake','bug'): 
            [proc2, proc3, proc5]}

for patterns,cases in dproc.iteritems():
    for pattern in patterns:
        if re.search(pattern,st):
            for case in cases: case(pattern)
        else: default(pattern)    
        print

这得到了找到的物品的正确顺序:1)抱起孩子,拥抱孩子;2)杀死老鼠,抱起老鼠。。。用可理解的语法对C switch语句执行同样的操作是很困难的。

还有很多其他方法可以模拟C开关语句。这里有一个(用于整数)使用函数装饰符:

case = {}

def switch_on(*values):
    def case_func(f):
        case.update((v, f) for v in values)
        return f
    return case_func

@switch_on(0, 3, 5)
def case_a(): print "case A"

@switch_on(1,2,4)
def case_b(): print "case B"

def default(): print "default"

for i in (0,2,3,5,22):
    print "Case: %i" % i
    try: 
        case[i]()
    except KeyError:
        default()

套用Larry Wall、Tom Christiansen、Jon Orwant在Programming Perl中关于理解Perl中的上下文的话:

You will be miserable programming Python until you use the idioms that are native to the language...

快速搜索会显示前面询问的similar question以及多个工作区。我最喜欢的解决办法是米扎德

import re

class Re(object):
  def __init__(self):
    self.last_match = None
  def match(self,pattern,text):
    self.last_match = re.match(pattern,text)
    return self.last_match
  def search(self,pattern,text):
    self.last_match = re.search(pattern,text)
    return self.last_match

gre = Re()
if gre.match(r'foo',text):
  # do something with gre.last_match
elif gre.match(r'bar',text):
  # do something with gre.last_match
else:
  # do something else

相关问题 更多 >