This def has too many IF, i don't know how to optimize this code for more simplier and professional.

Please take a look.

def cfg_wlan(self, wlan_cfg):


    Configure a new wlan for Zone Director.


    Input: a dictionary, supplied in

    Output: none



    if wlan_cfg['auth'] == "PSK":

        auth = "open"


        auth = wlan_cfg['auth']


    auth_server = ""

    vlan_id = ""

    acl_name = ""

    uplink_rate_limit = ""

    downlink_rate_limit = ""

    use_web_auth = False

    use_hide_ssid = False

    use_guest_access = False

    use_client_isolation = False

    use_zero_it = False

    use_dynamic_psk = False

    do_tunnel = False


    if auth == "EAP":

        if wlan_cfg['use_radius']:

            auth_server = wlan_cfg['ras_addr']



        if wlan_cfg.has_key('use_web_auth'):

            if wlan_cfg['use_web_auth']:

                use_web_auth = wlan_cfg['use_web_auth']

                if wlan_cfg['ras_addr']:

                    auth_server = wlan_cfg['ras_addr']

                elif wlan_cfg['ad_addr']:

                    auth_server = wlan_cfg['ad_addr']


    if wlan_cfg.has_key('use_guest_access'):

        use_guest_access = wlan_cfg['use_guest_access']


    if wlan_cfg.has_key('use_client_isolation'):

        use_client_isolation = wlan_cfg['use_client_isolation']


    if wlan_cfg.has_key('acl_name'):

        acl_name = wlan_cfg['acl_name']


    if wlan_cfg.has_key('use_hide_ssid'):

        use_hide_ssid = wlan_cfg['use_hide_ssid']


    if wlan_cfg.has_key('vlan_id'):

        vlan_id = wlan_cfg['vlan_id']


    if wlan_cfg.has_key('uplink_rate_limit'):

        uplink_rate_limit = wlan_cfg['uplink_rate_limit']

    if wlan_cfg.has_key('downlink_rate_limit'):

        downlink_rate_limit = wlan_cfg['downlink_rate_limit']


    if wlan_cfg.has_key('use_zero_it'):

        use_zero_it = wlan_cfg['use_zero_it']

        if wlan_cfg.has_key('use_dynamic_psk'):

            use_dynamic_psk = wlan_cfg['use_dynamic_psk']


    if wlan_cfg.has_key('do_tunnel'):

        do_tunnel = wlan_cfg['do_tunnel']



        self._create_wlan(wlan_cfg['ssid'], auth, wlan_cfg['encryption'],

                          wlan_cfg['wpa_ver'], wlan_cfg['key_string'],

                          wlan_cfg['key_index'], auth_server,

                          use_web_auth, use_guest_access, acl_name, use_hide_ssid,

                          vlan_id, uplink_rate_limit, downlink_rate_limit,

                          use_client_isolation, use_zero_it,

                          use_dynamic_psk, do_tunnel)



I suggest this simpler code. Check that it works as you expect

def cfg_wlan(self, wlan_cfg):

    Configure a new wlan for Zone Director.

    Input: a dictionary, supplied in

    Output: none

    defaults = [
        ('ssid', None), ('auth', None),
        ('encryption', None), ('wpa_ver', None),
        ('key_string', None), ('key_index', None),
        ('auth_server', ""), ('use_web_auth', False),
        ('use_guest_access', False), ('acl_name', ""),
        ('use_hide_ssid', False), ('vlan_id', ""),
        ('uplink_rate_limit', ""), ('downlink_rate_limit', ""),
        ('use_client_isolation', False), ('use_zero_it', False),
        ('use_dynamic_psk', False), ('do_tunnel', False)
    argdict = dict([(k, wlan_cfg.get(k, v)) for (k, v) in defaults])

    if not 'use_zero_it' in wlan_cfg:
        argdict['use_dynamic_psk'] = False

    if argdict['auth'] == "PSK":
        argdict['auth'] = "open"
    if argdict['auth'] == "EAP":
        if wlan_cfg['use_radius']:
            argdict['auth_server'] = wlan_cfg['ras_addr']
    elif argdict['use_web_auth']:
        argdict['auth_server'] = wlan_cfg['ras_addr'] or wlan_cfg['ad_addr'] or ""
    args = [argdict[k] for (k, v) in defaults]

Edited 6 Years Ago by Gribouillis: n/a

This article has been dead for over six months. Start a new discussion instead.