Create your own modules#
Module is a file with
.py extension and Python code.
Example of creating your own modules and importing a function from one module to another.
import ipaddress def check_ip(ip): try: ipaddress.ip_address(ip) return True except ValueError as err: return False ip1 = '10.1.1.1' ip2 = '10.1.1' print('Checking IP...') print(ip1, check_ip(ip1)) print(ip2, check_ip(ip2))
File check_ip_function.py has created check_ip function which checks that argument is an IP address. This is done by using ipaddress module which will be discussed in the next section.
ipaddress.ip_address checks the correctness of IP
address and generates ValueError exception if address is not validated.
check_ip returns True if address is valid and False if not.
If you run check_ip_function.py script, the output is:
$ python check_ip_function.py Checking IP... 10.1.1.1 True 10.1.1 False
The second script imports
check_ip function and uses it to select from address
list only those that passed the check (get_correct_ip.py file):
from check_ip_function import check_ip def return_correct_ip(ip_addresses): correct =  for ip in ip_addresses: if check_ip(ip): correct.append(ip) return correct print('Cheking list of IP addresses') ip_list = ['10.1.1.1', '184.108.40.206', '2.2.2'] correct = return_correct_ip(ip_list) print(correct)
First line imports check_ip function from check_ip_function.py module.
Result of script execution:
$ python get_correct_ip.py Cheking IP... 10.1.1.1 True 10.1.1 False Cheking list of IP addresses ['10.1.1.1', '220.127.116.11']
Note that not only information from get_correct_ip.py script is displayed,
but also information from check_ip_function.py. This is because any type of
import executes the entire script. That is, even when import looks like
from check_ip_function import check_ip, entire check_ip_function.py script
is executed, not just check_ip function. As a result, all messages of imported script will be displayed.
Messages from imported script are not scary, they are just confusing. Worse when script performed some kind of connection to equipment and when importing a function from it, we will have to wait for connection to take place.
Python can specify that some strings should not be executed when importing. This is discussed in the following subsection.
return_correct_ip can be replaced by a
filter or a list comprehension.
Above is used the longer but most likely more understandable option:
In : list(filter(check_ip, ip_list)) Out: ['10.1.1.1', '18.104.22.168'] In : [ip for ip in ip_list if check_ip(ip)] Out: ['10.1.1.1', '22.214.171.124'] In : def return_correct_ip(ip_addresses): ...: return [ip for ip in ip_addresses if check_ip(ip)] ...: In : return_correct_ip(ip_list) Out: ['10.1.1.1', '126.96.36.199']