簡體   English   中英

如何簡化if語句中的布爾邏輯?

[英]how to simplify boolean logic in if else statement?

我有4個變量,其中一些將為True和False,對於每種組合,我將不得不調用一個或幾個函數。 我目前正在為每種情況使用if else語句,我想知道是否存在更好的方法來通過字典或其他方法獲得相同的結果。

謝謝

這是我的代碼:

    if (self.cpe_ip and self.cpe_passwd) and self.phone and self.pppoe:
        print "launch ISP portal, modem and radius"
        if self.isp() == "east":
            self.launchbell()
        else:
            self.launchtelus()
        print 'check modem...'
        self.modemstatus()
        radius = sgp_radius.Radius(self.pppoe)
        print 'check radius logs...'
        self.data = radius.sgp()

        self.radius_save()

        #exit(0)
    elif (self.cpe_ip and self.cpe_passwd) and not self.phone and not self.pppoe:
        print "launch modem test only"
        self.modemstatus()


        #exit(0)
    elif not(self.cpe_ip and self.cpe_passwd) and self.phone and not self.pppoe:
        #print "only  Bell portal"
        if self.isp() == "east":
            self.launchbell()
        else:
            self.launchtelus()

    elif (self.cpe_ip and self.cpe_passwd) and not self.phone and self.pppoe:
        #print "launch modem and radius test."
        self.modemstatus()

        radius = sgp_radius.Radius(self.pppoe)
        print 'check radius logs...'
        self.data = radius.sgp()
        self.radius_save()

    elif not(self.cpe_ip and self.cpe_passwd) and not self.phone and self.pppoe:
        #print "only radius tests."
        radius = sgp_radius.Radius(self.pppoe)
        self.data = radius.sgp()

        self.radius_save()

    elif not(self.cpe_ip and self.cpe_passwd) and self.phone and self.pppoe:
        print "bell and radius tests."
        if self.isp() == "east":
            self.launchbell()
        else:
            self.launchtelus()

        radius = sgp_radius.Radius(self.pppoe)
        print 'check radius logs...'
        self.data = radius.sgp()
        self.radius_save()

    elif (self.cpe_ip and self.cpe_passwd) and self.phone and not self.pppoe:
        #print "launch modem and bell tests."
        if self.isp() == "east":
            self.launchbell()
        else:
            self.launchtelus()
        self.modemstatus()

    else:
        #print "test bell only"
        #launchbell(self.phone)
        exit(0)

一種簡化方法是識別並排除重復:

if self.phone:
    if self.isp() == "east":
        self.launchbell()
    else:
        self.launchtelus()

if self.cpe_ip and self.cpe_passwd:
    print 'check modem...'
    self.modemstatus()

if self.pppoe:
    radius = sgp_radius.Radius(self.pppoe)
    print 'check radius logs...'
    self.data = radius.sgp()
    self.radius_save()

您在問題中暗示,某種邏輯查找表可能是要走的路,並且有很多方法可以實現類似的目的。

如您所說,一種方法是使用字典,如下所示。

與您的特定示例的“簡化和分解重復”解決方案相比,這種方法產生的代碼看起來更復雜,但是這種方法更容易擴展到針對不同情況具有更多不同邏輯的情況。我不是說它“更好” ...只是一個不同的選擇!

def launch_isp_portal_and_radius():
   print "launch ISP portal, modem and radius"
   # etc

# etc

DecisionTable = {
 # cpe_ip cpe_passwd phone ppoe
   (True,  True,  True,  True ) : launch_isp_portal_and_radius,
   (True,  True,  False, False) : launch_modem_test_only,
   (False, False, True,  False) : only_Bell_portal,
   (False, True,  True,  False) : only_Bell_portal,
   (True,  False, True,  False) : only_Bell_portal,
   (True,  True,  False, True ) : launch_modem_and_radius_test,
   (False, False, False, False) : only_radius_tests,
   (False, True,  False, False) : only_radius_tests,
   (True,  False, False, False) : only_radius_tests,
   (False, False, True,  True ) : bell_and_radius_tests
   (False, True,  True,  True ) : bell_and_radius_tests,
   (True,  False, True,  True ) : bell_and_radius_tests,
   (True,  True,  True,  False) : launch_modem_and_bell_tests
}

action = DecisionTable.get((self.cpe_ip, self.cpe_passwd, self.phone, self.ppoe))

if action:
   action()
else:
   raise StandardError("Whoa, internal logic error!")

(當然,還有其他方法可以創建決策表-數組是另一個明顯的選擇。數組的問題在於使它的靜態聲明對於它所表示的邏輯可讀,...)

(編輯:根據喬恩斯的建議,將字典放在元組上索引)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM