Refactor code formatting for improved readability in flavor and math filter utilities
All checks were successful
CI / ci (push) Successful in 17s
CI / ci (pull_request) Successful in 17s

- Standardized code formatting in `flavor.py` and `mathfilters.py` for better visual clarity and consistency.
- Ensured proper indentation and spacing to enhance maintainability and readability of the utility functions.
This commit is contained in:
2026-02-12 20:19:30 +03:00
parent 656a6bfac4
commit d4fc2e920f
2 changed files with 81 additions and 81 deletions

View File

@@ -1,21 +1,21 @@
from collections import Counter from collections import Counter
from openstack.connection import Connection from openstack.connection import Connection
def get_flavor_list(connection: Connection) -> dict: def get_flavor_list(connection: Connection) -> dict:
servers = list(connection.compute.servers(all_projects=True)) servers = list(connection.compute.servers(all_projects=True))
flavor_ids = [s.flavor["id"] for s in servers if "id" in s.flavor] flavor_ids = [s.flavor["id"] for s in servers if "id" in s.flavor]
flavor_count = Counter(flavor_ids).most_common() flavor_count = Counter(flavor_ids).most_common()
flavors = list(flavor_count) flavors = list(flavor_count)
result = {} result = {}
placeholder = {"name": "", "count": 0} placeholder = {"name": "", "count": 0}
for idx, prefix in [(0, "first"), (1, "second"), (2, "third")]: for idx, prefix in [(0, "first"), (1, "second"), (2, "third")]:
if len(flavors) > idx: if len(flavors) > idx:
result[f"{prefix}_common_flavor"] = {"name": flavors[idx][0], "count": flavors[idx][1]} result[f"{prefix}_common_flavor"] = {"name": flavors[idx][0], "count": flavors[idx][1]}
else: else:
result[f"{prefix}_common_flavor"] = placeholder result[f"{prefix}_common_flavor"] = placeholder
return result return result

View File

@@ -1,60 +1,60 @@
from django import template from django import template
register = template.Library() register = template.Library()
@register.filter @register.filter
def div(a, b): def div(a, b):
try: try:
return float(a) / float(b) return float(a) / float(b)
except (TypeError, ValueError, ZeroDivisionError): except (TypeError, ValueError, ZeroDivisionError):
return 0 return 0
@register.filter @register.filter
def mul(a, b): def mul(a, b):
try: try:
return float(a) * float(b) return float(a) * float(b)
except (TypeError, ValueError): except (TypeError, ValueError):
return 0 return 0
@register.filter @register.filter
def sub(a, b): def sub(a, b):
try: try:
return float(a) - float(b) return float(a) - float(b)
except (TypeError, ValueError): except (TypeError, ValueError):
return 0 return 0
@register.filter @register.filter
def convert_bytes(bytes_value, target_unit="GB"): def convert_bytes(bytes_value, target_unit="GB"):
""" """
Convert bytes to specific unit Convert bytes to specific unit
Args: Args:
bytes_value: Size in bytes bytes_value: Size in bytes
target_unit: Target unit ('B', 'KB', 'MB', 'GB', 'TB') target_unit: Target unit ('B', 'KB', 'MB', 'GB', 'TB')
precision: Number of decimal places precision: Number of decimal places
Returns: Returns:
Float value in target unit Float value in target unit
""" """
try: try:
bytes_value = float(bytes_value) bytes_value = float(bytes_value)
except (ValueError, TypeError): except (ValueError, TypeError):
return 0.0 return 0.0
conversion_factors = { conversion_factors = {
"B": 1, "B": 1,
"KB": 1024, "KB": 1024,
"MB": 1024 * 1024, "MB": 1024 * 1024,
"GB": 1024 * 1024 * 1024, "GB": 1024 * 1024 * 1024,
"TB": 1024 * 1024 * 1024 * 1024, "TB": 1024 * 1024 * 1024 * 1024,
} }
target_unit = target_unit.upper() target_unit = target_unit.upper()
if target_unit not in conversion_factors: if target_unit not in conversion_factors:
target_unit = "MB" target_unit = "MB"
result = bytes_value / conversion_factors[target_unit] result = bytes_value / conversion_factors[target_unit]
return round(result, 1) return round(result, 1)