2016-09-10 6 views
-1

私は、簡略化のアドバイスが必要なPythonプログラムを開発しました。Python - コードを短縮する方法

これは私のコードの一部です:

このコードで
import wx 
import sys 
import socket 

def error_handler(c): 
    if c == 'canceled': 
     sys.exit('User canceled configuration.') 
    elif c == 'empty': 
     sys.exit('Empty value.') 


def hostname(): 
    dlg = wx.TextEntryDialog(None, 
          'What is your default Hostname?', 
          'Hostname', 
          socket.gethostname()) 

    if dlg.ShowModal() == wx.ID_CANCEL: 
     error_handler('canceled') 
    else: 
     if dlg.GetValue() == "": 
      error_handler('empty') 
     else: 
      HOSTNAME = dlg.GetValue() 
      return HOSTNAME 


def random_hostname(): 
    dlg = wx.SingleChoiceDialog(None, 
           'Do you want to randomize your Hostname', 
           'Randomize', 
           ['Yes', 'No', 'Disable'], 
           wx.CHOICEDLG_STYLE) 

    if dlg.ShowModal() == wx.ID_CANCEL: 
     error_handler('canceled') 
    else: 
     RANDOM_HOSTNAME = dlg.GetStringSelection() 
     return RANDOM_HOSTNAME 


def nameserver(): 
    dlg = wx.TextEntryDialog(None, 
          'Nameserver IP\n', 
          'Nameserver', 
          '127.0.0.1') 

    if dlg.ShowModal() == wx.ID_CANCEL: 
     error_handler('canceled') 
    else: 
     if dlg.GetValue() == "": 
      error_handler('empty') 
     else: 
      NAMESERVER = dlg.GetValue() 
      return NAMESERVER 


def main(): 
    app = wx.App() 
    print 'HOSTNAME =', hostname() 
    print 'RANDOM_HOSTNAME =', random_hostname() 
    print 'NAMESERVER =', nameserver() 
    app.MainLoop() 


if __name__ == '__main__': 
    main() 

私はホスト名、ランダムホスト名とネームサーバのための機能をするが、すべての3機能で、私はほぼ同じコードを繰り返す必要があります。

if dlg.ShowModal() == wx.ID_CANCEL: 
    error_handler('canceled') 
else: 
    if dlg.GetValue() == "": 
     error_handler('empty') 
    else: 
     HOSTNAME = dlg.GetValue() 
     return HOSTNAME 

しかし、私はいくつかの値をチェックするために20以上の関数を作りたいと思います。 すべての機能を短縮するには、いくつかのより良い方法がありますか?私はこれで私を助ける皆に感謝したい

import wx 
import sys 
import socket 

def error_handler(c): 
    if c == 'canceled': 
     sys.exit('User canceled configuration.') 
    elif c == 'empty': 
     sys.exit('Empty value.') 
    else 
     return dialog value 


def hostname(): 
    dlg = wx.TextEntryDialog(None, 
          'What is your default Hostname?', 
          'Hostname', 
          socket.gethostname()) 

    error_handler(dlg) 


def random_hostname(): 
    dlg = wx.SingleChoiceDialog(None, 
           'Do you want to randomize your Hostname', 
           'Randomize', 
           ['Yes', 'No', 'Disable'], 
           wx.CHOICEDLG_STYLE) 

    error_handler(dlg) 


def nameserver(): 
    dlg = wx.TextEntryDialog(None, 
          'Nameserver IP\n', 
          'Nameserver', 
          '127.0.0.1') 

    error_handler(dlg) 


def main(): 
    app = wx.App() 
    print 'HOSTNAME =', hostname() 
    print 'RANDOM_HOSTNAME =', random_hostname() 
    print 'NAMESERVER =', nameserver() 
    app.MainLoop() 


if __name__ == '__main__': 
    main() 

は、私はこのような何かをしたいです。

+0

これは、codeview.stackexchage:http://codereview.stackexchange.com/でうまくいく可能性があります。私はそこに尋ねることを提案する。 –

+0

oh。私が間違ったセクションに投稿したことを知らなかった。摂取のためのthx –

答えて

0

もう一度別の機能を作成すると、回答が返ってきているようです。繰り返し作業を行うにはerror_handler!あなたはこの線に沿って何かを見てしまうtry/exceptブロックであなたのエラーハンドラをラップすることができます:

def hostname(): 
    try: 
     dlg = wx.TextEntryDialog(None, 
        'What is your default Hostname?', 
        'Hostname', 
         socket.gethostname()) 
    except: 
     error_handler(dlg) 
0
def funct(dlg, wx, funcCheck): 
    if dlg.ShowModal() == wx.ID_CANCEL: 
     error_handler('canceled') 
    else: 
     if dlg.GetValue() == "": 
      error_handler('empty') 
     else: 
      value = funcCheck() 
      return value 

、あなたはこのよう

funct(dlg, wx, dlg.GetValue) 

がに条件を追加し続け、それを呼び出すことができますあなたが望むものに応じて機能する

関連する問題