add convert data to nodes

This commit is contained in:
Павел
2025-01-11 08:08:08 +03:00
parent dfd02e3da5
commit 9377d23134
+10 -8
View File
@@ -1,8 +1,6 @@
import cProfile import cProfile
from collections import defaultdict
from copy import deepcopy Node = tuple[int, int, int]
from ipaddress import IPv4Address
from typing import List, Set, Tuple
def get_data(input_file): def get_data(input_file):
@@ -11,28 +9,32 @@ def get_data(input_file):
return data return data
def cidr4_to_node(cidr4: str) -> Tuple[int, int, int]: def cidr4_to_node(cidr4: str) -> Node:
ip, mask_len = cidr4.strip().split("/") ip, mask_len = cidr4.strip().split("/")
mask_len = int(mask_len) mask_len = int(mask_len)
added_ips_number = 0 added_ips_number = 0
a, b, c, d = ip.split(".") a, b, c, d = list(map(int, ip.split(".")))
a, b, c, d = int(a), int(b), int(c), int(d)
ip_value = a * 256**3 + b * 256**2 + c * 256**1 + d * 256**0 ip_value = a * 256**3 + b * 256**2 + c * 256**1 + d * 256**0
return ip_value, mask_len, added_ips_number return ip_value, mask_len, added_ips_number
def data_to_nodes(data: list[str]) -> list[Node]:
return sorted(map(cidr4_to_node, data))
def main(): def main():
file = "cidr4.txt" file = "cidr4.txt"
required_len = 15 required_len = 15
data = get_data(file) data = get_data(file)
nodes = sorted(map(cidr4_to_node, data)) nodes = data_to_nodes(data)
for n in nodes: for n in nodes:
print(n) print(n)
if __name__ == "__main__": if __name__ == "__main__":
assert cidr4_to_node("4.78.139.0/24") == (72256256, 24, 0) assert cidr4_to_node("4.78.139.0/24") == (72256256, 24, 0)
assert cidr4_to_node("0.0.0.0/32") == (0, 32, 0)
main() main()
# cProfile.run("main()") # cProfile.run("main()")