testdata/src/utils.py
2024-04-12 17:05:27 +02:00

46 lines
1.1 KiB
Python

import json
def convert_to_bytes(size: int | str) -> int:
try:
return int(size)
except ValueError: # treat as string
units = {
'TB': 1000 ** 4, 'TiB': 1024 ** 4,
'GB': 1000 ** 3, 'GiB': 1024 ** 3,
'MB': 1000 ** 2, 'MiB': 1024 ** 2,
'KB': 1000, 'KiB': 1024,
'B': 1
}
for unit in units:
if size.endswith(unit):
return int(float(size.removesuffix(unit)) * units[unit])
break
raise ValueError
def generate_data(size: int, buffer_size: int = 4 * 1024) -> bytes:
size_left = size
while size_left > buffer_size:
size_left -= buffer_size
yield b'\0' * buffer_size
else:
yield b'\0' * size_left
def check_policies(ip: str) -> None:
network = ipaddress.ip_network(ip)
print(network)
def load_database(path: str) -> dict:
with open(path, 'r') as file:
return json.load(file)
def save_database(path: str, database: dict) -> None:
with open(path, 'w') as file:
json.dump(database, file, indent=2)