[Python] conditional statement is not working when function returns "None" in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    import pandas as pd
    import ipaddress


    def get_destination_host_from_csv():

    df = pd.read_csv('dc_interface.csv')
    for i in df['destination']:
    if ipaddress.ip_network(ip_add2,strict=False).subnet_of(ipaddress.ip_network(i,strict=False)) is True:
    target_value = i
    break
    else:
    target_value = None

    if target_value is not None:
    index_list = []
    column_list = []
    for column in df.columns:
    for index in df.index:
    if df.loc[index, column] == target_value:
    index_list.append(index)
    column_list.append(column)
    df.iloc[index_list[0]]['hostname']
    return df.iloc[index_list[0]]['hostname'],df.iloc[index_list[0]]['virtual-router']
    else:
    return None

    def component_to_dest_fw():
    if get_destination_host_from_csv() is None:
    dest_fw = firewall.Firewall(get_destination_host_from_csv()[0],api_username= username, api_password= password)
    source_route_xml = dest_fw.op(f'test routing fib-lookup virtual-router "trust-vr" ip "{ip_add2}"', xml=True)
    source_interface = ET.fromstring(source_route_xml)
    source_interface_finder = source_interface.findall(".//result/interface")[0].text
    return source_interface_finder
    else:
    print("no need to check destination side firewall")


    my first function get_destination_host_from_csv() is returning None when I run the function, but when I match the condition of the first function with returned value of 2nd function component_to_dest_fw() and try to print it then it gets me below output:


    <function component_to_dest_fw at 0x7ff10c061bc0>
    ​

    instead of :


    no need to check destination side firewall
    ​

    Continue reading...

Compartilhe esta Página