When I used to program I wrote functions for tasks I did on a regular basis and created a library for them. I have been playing around with python and found an interesting ITPro.TV video on creating one in python.
I created a runbatch text file to execute the commands for testing. I store the commands in commands.py. They are called into cli.py.
I created it so I could add, subtract, multiply, divide, quadratic formula I created them with 3 integers
Finally finding my hostname, IPv4 and IPv6 addresses.
python3 cli.py add 1 2 3
python3 cli.py subtract 1 2 3
python3 cli.py multiply 1 2 3
python3 cli.py division 1 2 3
python3 cli.py quadratic 1 2 3
python3 cli.py quadratic 1 -2 1
python3 cli.py quadratic 1 7 12
python3 cli.py interface_nic
runbatch.txt
cli.py
#!/usr/bin/env python3 import argparse import commands import math import socket import netifaces as ni import ipaddress def main_math(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest=”command”) # first command add = subparsers.add_parser(commands.ADD) add.add_argument(“numbers”, nargs=’+’, type=int) # second command sub = subparsers.add_parser(commands.SUBTRACT) sub.add_argument(“numbers”, nargs=’+’, type=int) # third command multiply = subparsers.add_parser(commands.MULTIPLY) multiply.add_argument(“numbers”, nargs=’+’, type=int) # fourth command division = subparsers.add_parser(commands.DIVISION) division.add_argument(“numbers”, nargs=’+’, type=int) # fifth command quadratic = subparsers.add_parser(commands.QUADRATIC) quadratic.add_argument(“numbers”, nargs=’+’, type=int) # find interface command interface_nic = subparsers.add_parser(commands.INTERFACE_NIC) args = parser.parse_args() if args.command == commands.ADD: result = sum(args.numbers) print(f”The sum of {args.numbers} is {result}”) print(f””) elif args.command == commands.SUBTRACT: first, *rest = args.numbers result = first – sum(rest) print(f”The subtraction of {args.numbers} is {result}”) print(f””) elif args.command == commands.MULTIPLY: result = 1 for i in args.numbers: result *= i print(f”The multiplication of {args.numbers} is {result}”) print(f””) elif args.command == commands.DIVISION: set_first = 0 for i in args.numbers: if set_first == 1: result /= i # sets first number to results elif set_first == 0: result = i set_first = 1 print(f”The division of {args.numbers} is {result}”) print(f””) elif args.command == commands.QUADRATIC: a = args.numbers[0] b = args.numbers[1] c = args.numbers[2] dis = b * b – 4 * a * c sqrt_val = math.sqrt(abs(dis)) # checking condition for discriminant if dis > 0: print(f”The roots of {a} {b} {c} are”) print(” real and different roots “) print((-b + sqrt_val) / (2 * a)) print((-b – sqrt_val) / (2 * a)) print(f””) elif dis == 0: print(f”The roots of {a} {b} {c} are”) print(” real and same roots”) print(-b / (2 * a)) print(f””) # when discriminant is less than 0 else: print(f”The roots of {a} {b} {c} are”) print(“Complex Roots”) print(- b / (2 * a), ” + i”, “{:.4f}”.format(sqrt_val)) print(- b / (2 * a), ” – i”, “{:.4f}”.format(sqrt_val)) print(f””) elif args.command == commands.INTERFACE_NIC: # Find network interfaces print(“What is my ip address”) host_name = socket.gethostname() print(f”my host name is {host_name}”) a = socket.getaddrinfo(host_name, 80) addrs = socket.if_nameindex() for i in addrs: if i[0] != 1: int_nic = ”.join(i[1]) ni.ifaddresses(int_nic) ip = ni.ifaddresses(int_nic)[ni.AF_INET][0][‘addr’] print(f”IP Address of interface {int_nic} is {ip}”) # IPv6 address addrInfo = socket.getaddrinfo(host_name, None, socket.AF_INET6, socket.SOCK_RAW, 0) str_addInfo = str(addrInfo) str_addInfo_split = str_addInfo.split(“,”,5) i = 0 while i < len(str_addInfo_split): if i == 4: int_ipv6 = str_addInfo_split[i] i += 1 len_int_ipv6 = len(int_ipv6) print(f"IPv6 Address of interface {int_nic} is {int_ipv6[3:(len_int_ipv6-1)]}") else: parser.print_help() if __name__ == "__main__": main_math()
Commands.py
ADD = 'add'
SUBTRACT = 'subtract'
MULTIPLY = 'multiply'
DIVISION = 'division'
QUADRATIC = 'quadratic'
INTERFACE_NIC = 'interface_nic'
The sum of [1, 2, 3] is 6
The subtraction of [1, 2, 3] is -4
The multiplication of [1, 2, 3] is 6
The division of [1, 2, 3] is 0.16666666666666666
The roots of 1 2 3 are
Complex Roots
-1.0 + i 2.8284
-1.0 - i 2.8284
The roots of 1 -2 1 are
real and same roots
1.0
The roots of 1 7 12 are
real and different roots
-3.0
-4.0
What is my ip address
my host name is programmer2004
IP Address of interface ens32 is 192.168.1.30
IPv6 Address of interface ens32 is 2600:1702:980:25ef:e192:168:1:30