From d4fc2e920f72b751a64e848a9c1c5cda1a5bda33 Mon Sep 17 00:00:00 2001 From: Nikolay Tatarinov Date: Thu, 12 Feb 2026 20:19:30 +0300 Subject: [PATCH] Refactor code formatting for improved readability in flavor and math filter utilities - 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. --- dashboard/openstack_utils/flavor.py | 42 ++++----- dashboard/templatetags/mathfilters.py | 120 +++++++++++++------------- 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/dashboard/openstack_utils/flavor.py b/dashboard/openstack_utils/flavor.py index 5eb56bd..0b0240b 100644 --- a/dashboard/openstack_utils/flavor.py +++ b/dashboard/openstack_utils/flavor.py @@ -1,21 +1,21 @@ -from collections import Counter - -from openstack.connection import Connection - - -def get_flavor_list(connection: Connection) -> dict: - servers = list(connection.compute.servers(all_projects=True)) - flavor_ids = [s.flavor["id"] for s in servers if "id" in s.flavor] - flavor_count = Counter(flavor_ids).most_common() - - flavors = list(flavor_count) - - result = {} - placeholder = {"name": "—", "count": 0} - for idx, prefix in [(0, "first"), (1, "second"), (2, "third")]: - if len(flavors) > idx: - result[f"{prefix}_common_flavor"] = {"name": flavors[idx][0], "count": flavors[idx][1]} - else: - result[f"{prefix}_common_flavor"] = placeholder - - return result +from collections import Counter + +from openstack.connection import Connection + + +def get_flavor_list(connection: Connection) -> dict: + servers = list(connection.compute.servers(all_projects=True)) + flavor_ids = [s.flavor["id"] for s in servers if "id" in s.flavor] + flavor_count = Counter(flavor_ids).most_common() + + flavors = list(flavor_count) + + result = {} + placeholder = {"name": "—", "count": 0} + for idx, prefix in [(0, "first"), (1, "second"), (2, "third")]: + if len(flavors) > idx: + result[f"{prefix}_common_flavor"] = {"name": flavors[idx][0], "count": flavors[idx][1]} + else: + result[f"{prefix}_common_flavor"] = placeholder + + return result diff --git a/dashboard/templatetags/mathfilters.py b/dashboard/templatetags/mathfilters.py index 7d602d9..22dafa6 100644 --- a/dashboard/templatetags/mathfilters.py +++ b/dashboard/templatetags/mathfilters.py @@ -1,60 +1,60 @@ -from django import template - -register = template.Library() - - -@register.filter -def div(a, b): - try: - return float(a) / float(b) - except (TypeError, ValueError, ZeroDivisionError): - return 0 - - -@register.filter -def mul(a, b): - try: - return float(a) * float(b) - except (TypeError, ValueError): - return 0 - - -@register.filter -def sub(a, b): - try: - return float(a) - float(b) - except (TypeError, ValueError): - return 0 - - -@register.filter -def convert_bytes(bytes_value, target_unit="GB"): - """ - Convert bytes to specific unit - - Args: - bytes_value: Size in bytes - target_unit: Target unit ('B', 'KB', 'MB', 'GB', 'TB') - precision: Number of decimal places - - Returns: - Float value in target unit - """ - try: - bytes_value = float(bytes_value) - except (ValueError, TypeError): - return 0.0 - conversion_factors = { - "B": 1, - "KB": 1024, - "MB": 1024 * 1024, - "GB": 1024 * 1024 * 1024, - "TB": 1024 * 1024 * 1024 * 1024, - } - - target_unit = target_unit.upper() - if target_unit not in conversion_factors: - target_unit = "MB" - - result = bytes_value / conversion_factors[target_unit] - return round(result, 1) +from django import template + +register = template.Library() + + +@register.filter +def div(a, b): + try: + return float(a) / float(b) + except (TypeError, ValueError, ZeroDivisionError): + return 0 + + +@register.filter +def mul(a, b): + try: + return float(a) * float(b) + except (TypeError, ValueError): + return 0 + + +@register.filter +def sub(a, b): + try: + return float(a) - float(b) + except (TypeError, ValueError): + return 0 + + +@register.filter +def convert_bytes(bytes_value, target_unit="GB"): + """ + Convert bytes to specific unit + + Args: + bytes_value: Size in bytes + target_unit: Target unit ('B', 'KB', 'MB', 'GB', 'TB') + precision: Number of decimal places + + Returns: + Float value in target unit + """ + try: + bytes_value = float(bytes_value) + except (ValueError, TypeError): + return 0.0 + conversion_factors = { + "B": 1, + "KB": 1024, + "MB": 1024 * 1024, + "GB": 1024 * 1024 * 1024, + "TB": 1024 * 1024 * 1024 * 1024, + } + + target_unit = target_unit.upper() + if target_unit not in conversion_factors: + target_unit = "MB" + + result = bytes_value / conversion_factors[target_unit] + return round(result, 1)