Skip to content

Instantly share code, notes, and snippets.

@mols3131d
Last active May 10, 2024 07:53
Show Gist options
  • Save mols3131d/52247ccd67e1fdc054b8ba44f59ec1fb to your computer and use it in GitHub Desktop.
Save mols3131d/52247ccd67e1fdc054b8ba44f59ec1fb to your computer and use it in GitHub Desktop.
python3
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
tqdm
black
isort
from math import floor
from typing import Any, Literal
def _floor(num, n):
return floor(num * 10 ** n) / 10 ** n
def convert_float_to_integer_or_raise_error(data):
if data % 1 == 0:
return int(data)
else:
raise ValueError("Decimal numbers are not allowed.")
def _auto_convert_memory_size(size, unit) -> tuple[float, str]:
"""
Automatically converts size and unit.
Parameters:
size (float): The size to convert.
unit (str): The initial unit. Valid units are ["B", "KB", "MB", "GB", "TB"].
Returns:
tuple: The converted size and unit (size, unit).
Raises:
ValueError: If an invalid unit or a negative size is provided.
Examples:
>>> auto_convert_size(1, "TB")
(1024.0, 'GB')
>>> auto_convert_size(1024, "MB")
(1.0, 'GB')
"""
valid_units = ["B", "KB", "MB", "GB", "TB"]
if unit not in valid_units:
raise ValueError(f"Invalid unit. Choose from {valid_units}.")
if size < 0:
raise ValueError("Size should be a positive number.")
while size >= 1024 and unit != "TB":
size /= 1024
unit = valid_units[valid_units.index(unit) + 1]
while size < 1 and unit != "B":
size *= 1024
unit = valid_units[valid_units.index(unit) - 1]
return size, unit
def convert_memory_size(
size: int | float,
from_unit: Literal["B", "KB", "MB", "GB", "TB"] = "B",
to_unit: Literal["auto", "B", "KB", "MB", "GB", "TB"] = "auto",
decimal_places: int = None,
) -> tuple[float | int, Literal["B", "KB", "MB", "GB", "TB"]]:
"""
Convert memory size from one unit to another.
Parameters:
size (int | float): The size to convert.
from_unit (Literal["B", "KB", "MB", "GB", "TB"]): The unit of the input size. Default is "B".
to_unit (Literal["auto", "B", "KB", "MB", "GB", "TB"]): The desired unit for the converted size. Default is "auto".
decimal_places (int): The number of decimal places to round the result. Default is None.
Returns:
tuple: The converted size and unit.
Raises:
ValueError: If an invalid unit is provided or if float values are given for unit "B".
Examples:
>>> convert_memory_size(1024, "KB", "MB")
(1.0, 'MB')
>>> convert_memory_size(16.1, "MB", "KB")
(16435.4, 'KB')
"""
if from_unit == "B" and isinstance(size, float):
if size % 1 == 0:
size = int(size)
else:
raise ValueError("Float values are not supported for B(byte).")
units = ["B", "KB", "MB", "GB", "TB"]
units = {u: 1024**i for i, u in enumerate(units)}
size_bytes = size * units[from_unit]
if to_unit == "auto":
size, to_unit = _auto_convert_memory_size(size, from_unit)
converted_size = size_bytes / units[to_unit]
if decimal_places:
converted_size = floor(converted_size * 10**decimal_places) / 10**decimal_places
converted_size = (
float(converted_size) if decimal_places >= 1 else int(converted_size)
)
return converted_size, to_unit
def order_dict(
original_dict: dict[str, Any],
desired_order: list[str],
drop: bool = False,
) -> dict[str, Any]:
"""
Orders the keys of a dictionary based on the desired order.
Parameters:
original_dict (dict): The original dictionary.
desired_order (list): The desired order of keys.
drop (bool): Whether to drop keys not in desired_order. Default is True.
Returns:
dict: The ordered dictionary.
Example:
>>> original_dict = {"d": 4, "c": 3, "b": 2, "a": 1}
>>> desired_order = ["a", "c", "b"]
>>> ordered_dict = order_dict(original_dict, desired_order)
>>> print(ordered_dict)
{'a': 1, 'c': 3, 'b': 2}
"""
ordered_dict = {}
if drop:
for key in desired_order:
if key in original_dict:
ordered_dict[key] = original_dict[key]
else:
for key in desired_order:
if key in original_dict:
ordered_dict[key] = original_dict[key]
# Append keys not in desired_order to the end
for key in original_dict:
if key not in ordered_dict:
ordered_dict[key] = original_dict[key]
return ordered_dict
if __name__ == "__main__":
# testing
import sys
sys.path.append("..")
sys.dont_write_bytecode = True
if __name__ == "__main__":
# testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment