Python - How to shorten code
<p>I've developed a Python program that I need advice on simplifying.</p>

<p>This is part of my code:</p>

<pre><code>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()
</code></pre>

<p>In this code I make function for Hostname, Random hostname and nameserver, but in all 3 function I have to repeat almost same code:</p>

<pre><code>if dlg.ShowModal() == wx.ID_CANCEL:
    error_handler('canceled')
else:
    if dlg.GetValue() == "":
        error_handler('empty')
    else:
        HOSTNAME = dlg.GetValue()
        return HOSTNAME
</code></pre>

<p>But I want to make more than 20 functions for checking some values.
Is there (and I know that there is) some better trick to shorten every function?</p>

<p>I want something like this:</p>

<pre><code>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()
</code></pre>

<p>I wish to thank everybody who will help me with this.</p>

<p>It looks you already know the answer with creating another function, <code>error_handler</code> to do the repeated work! You could wrap your error handler in a <a href="https://docs.python.org/2/tutorial/errors.html#handling-exceptions" rel="nofollow">try/except</a> block that would look something along the lines of this:</p>

<pre><code>def hostname():
    try:
        dlg = wx.TextEntryDialog(None,
                     'What is your default Hostname?',
                     'Hostname',
                      socket.gethostname())
    except:
        error_handler(dlg)
</code></pre>

python function