diff --git a/m01_basics/l_06_comparisons.py b/m01_basics/l_06_comparisons.py index 5101498e..c4d128f4 100644 --- a/m01_basics/l_06_comparisons.py +++ b/m01_basics/l_06_comparisons.py @@ -26,10 +26,10 @@ for index, device_a in enumerate(devices): print(f"Found match! {device_a['name']} for both {device_a['ip']} and {device_b['ip']}") print("----- Comparison of device names completed") -print("\n----- Create table of arbitrary 'standard' versions for each vendor:os --------------------") +print("\n----- Create table of arbitrary 'standard' versions for each vendor:os - Resumen de versiones --------------------") standard_versions = dict() for device in devices: - vendor_os = device["vendor"] + ":" + device["os"] + vendor_os = device["vendor"] + ":" + device["os"] if vendor_os not in standard_versions: standard_versions[vendor_os] = device["version"] pprint(standard_versions) diff --git a/m01b_intermediate/l_13_normalize_data.py b/m01b_intermediate/l_13_normalize_data.py index f173f6e4..6d80889f 100644 --- a/m01b_intermediate/l_13_normalize_data.py +++ b/m01b_intermediate/l_13_normalize_data.py @@ -1,6 +1,9 @@ from ipaddress import IPv4Address -# REMOVE CAPITALIZATION ISSUES +print("\nNormalization tests\n") + +# ----- STRING NORMALIZATION -------------------- + device_1 = { "name": "sbx-n9kv-ao", "vendor": "cisco", @@ -25,11 +28,21 @@ if ( and device_1["model"].lower() == device_2["model"].lower() and device_1["os"].lower() == device_2["os"].lower() ): - print("--\nString lower() normalization works") + print("--String lower() normalization works") else: - print("--\nString lower() normalization failed") + print("--String lower() normalization failed") -# MAC ADDRESS NORMALIZATION +if ( + device_1["name"].casefold() == device_2["name"].casefold() + and device_1["vendor"].casefold() == device_2["vendor"].casefold() + and device_1["model"].casefold() == device_2["model"].casefold() + and device_1["os"].casefold() == device_2["os"].casefold() +): + print("--String casefold() normalization works") +else: + print("--String casefold() normalization failed") + +# ----- MAC ADDRESS NORMALIZATION -------------------- mac_addr_colons = "a0:b1:c2:d3:e4:f5" mac_addr_caps = "A0:B1:C2:D3:E4:F5" @@ -45,12 +58,6 @@ def normalize(mac): return mac.lower().replace(":", "").replace(".", "").replace("-", "") -# print("MAC address with colons: ", normalize(mac_addr_colons)) -# print("MAC address with caps: ", normalize(mac_addr_caps)) -# print("MAC address with dots: ", normalize(mac_addr_dots)) -# print("MAC address with hyphens: ", normalize(mac_addr_hyphens)) -# print("MAC address with wacky: ", normalize(mac_addr_wacky)) - if ( normalize(mac_addr_colons) == normalize(mac_addr_caps) @@ -59,16 +66,20 @@ if ( == normalize(mac_addr_wacky) == mac_addr_norm ): - print("--\nMAC address normalization works") + print("--MAC address normalization works") else: - print("--\nMAC address normalization failed") + print("--MAC address normalization failed") -# IP ADDRESS NORMALIZATION +# ----- IP ADDRESS NORMALIZATION -------------------- ip_addr_1 = "10.0.1.1" ip_addr_2 = "10.000.001.001" +ip_addr_3 = "010.00.01.001" -if IPv4Address(ip_addr_1) == IPv4Address(ip_addr_2): - print("--\nIP address normalization works") +if IPv4Address(ip_addr_1) == IPv4Address(ip_addr_2) == IPv4Address(ip_addr_3): + print("--IP address normalization works") else: - print("--\nIP address normalization failed") + print("--IP address normalization failed") + +# ----- DEVICE DATA NORMALIZATION -------------------- +# (see NAPALM examples) \ No newline at end of file diff --git a/m01b_intermediate/l_14_tuples.py b/m01b_intermediate/l_14_tuples.py index 90b3e153..2d2bded7 100644 --- a/m01b_intermediate/l_14_tuples.py +++ b/m01b_intermediate/l_14_tuples.py @@ -1,21 +1,49 @@ from util.create_utils import create_devices -from tabulate import tabulate -from operator import itemgetter from pprint import pprint +from collections import namedtuple # --- Main program -------------------------------------------- if __name__ == '__main__': - t = (1, 2, 3) - t += (4, 5) - print(t) - - devices = create_devices(num_devices=4, num_subnets=1) - devices_tuple = tuple(devices) - - print("\n----- LIST OF DEVICES --------------------") - pprint(devices) + devices = tuple(create_devices(num_devices=4, num_subnets=1)) print("\n----- TUPLE OF DEVICES --------------------") - pprint(devices_tuple) + pprint(devices) + print("\n----- DEVICE AS TUPLE --------------------") + device = ("sbx-n9kv-ao", "cisco", "Nexus9000 C9300v Chassis", "nxos", "10.0.1.1") + + print(" name:", device[0]) + print("vendor:", device[1]) + print(" model:", device[2]) + print(" os:", device[3]) + print(" ip:", device[4]) + + print("\n----- DEVICE AS NAMED TUPLE --------------------") + Device = namedtuple('Device', ['name', 'vendor', 'model', 'os', 'ip']) + device = Device("sbx-n9kv-ao", "cisco", "Nexus9000 C9300v Chassis", "nxos", "10.0.1.1") + + print(" name:", device.name) + print("vendor:", device.vendor) + print(" model:", device.model) + print(" os:", device.os) + print(" ip:", device.ip) + + print("\n----- PPRINT OF DEVICE NAMED TUPLE --------------------") + pprint(device) + + print("\n----- CONVERT DEVICES TO NAMED TUPLES --------------------") + devices = create_devices(num_devices=10, num_subnets=2, random_ip=True) + devices_as_namedtuples = list() + for device in devices: + Device = namedtuple("Device", device.keys()) + devices_as_namedtuples.append(Device(**device)) + + print("\n----- PPRINT VERSION OF DEVICES AS NAMED TUPLES --------------------") + pprint(devices_as_namedtuples) + + print("\n----- NICELY FORMATTED --------------------\n") + print(" NAME VENDOR : OS IP ADDRESS") + print(" ----- ------- ----- --------------") + for device in devices_as_namedtuples: + print(f'{device.name:>7} {device.vendor:>10} : {device.os:<6} {device.ip:<15}') \ No newline at end of file diff --git a/m01b_intermediate/l_15_sets.py b/m01b_intermediate/l_15_sets.py index e69de29b..19a7c0bc 100644 --- a/m01b_intermediate/l_15_sets.py +++ b/m01b_intermediate/l_15_sets.py @@ -0,0 +1,46 @@ +available_ips = set() +used_ips = set() + + +def print_ips(): + + available_ips_list = list(available_ips) + used_ips_list = list(used_ips) + + if len(available_ips_list) > len(used_ips_list): + for _ in range(0, len(available_ips_list) - len(used_ips_list)): + used_ips_list.append("") + elif len(available_ips_list) < len(used_ips_list): + for _ in range(0, len(used_ips_list) - len(available_ips_list)): + available_ips_list.append("") + + print() + print(" available used") + print(" ---------------- -----------------") + for available_ip, used_ip in zip(available_ips_list, used_ips_list): + print(f" {available_ip:>16} {used_ip:<16}") + + +for index in range(180, 200): + available_ips.add("10.0.1." + str(index)) + +while True: + print_ips() + ip_address = input("\nEnter IP address to allocate: ") + if not ip_address: + print("\nExiting 'sets' application") + exit() + + if ip_address in available_ips: + + print(f"-- allocated IP address: {ip_address}") + available_ips.remove(ip_address) + used_ips.add(ip_address) + + print_ips() + + if len(available_ips.intersection(used_ips)) > 0: + print("\n-- ERROR! one or more IPs in both sets") + + else: + print("-- IP address not found in available IPs\n") \ No newline at end of file