Last active
May 10, 2024 07:53
-
-
Save mols3131d/52247ccd67e1fdc054b8ba44f59ec1fb to your computer and use it in GitHub Desktop.
python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pprint import pprint" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def update_nested_dict(org: dict, new: dict):\n", | |
" for key, value in new.items():\n", | |
" if isinstance(value, dict):\n", | |
" if key not in org:\n", | |
" org[key] = {}\n", | |
" update_nested_dict(org[key], value)\n", | |
" else:\n", | |
" org[key] = value\n", | |
" return org" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"original_dict\n", | |
"{'user_id': 12345,\n", | |
" 'username': 'foo',\n", | |
" 'email': 'foo@example.com',\n", | |
" 'profile': {'name': 'Foo', 'age': 33, 'interests': ['Python', 'Programming']},\n", | |
" 'settings': {'theme': 'light', 'language': 'eng'}}\n", | |
"\n", | |
"new_dict\n", | |
"{'username': 'goo',\n", | |
" 'profile': {'age': 34, 'interests': ['Python', 'Web Development']},\n", | |
" 'settings': {'theme': 'dark'}}\n", | |
"\n", | |
"updated_dict\n", | |
"{'user_id': 12345,\n", | |
" 'username': 'goo',\n", | |
" 'email': 'foo@example.com',\n", | |
" 'profile': {'name': 'Foo',\n", | |
" 'age': 34,\n", | |
" 'interests': ['Python', 'Web Development']},\n", | |
" 'settings': {'theme': 'dark', 'language': 'eng'}}\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"original_dict = {\n", | |
" \"user_id\": 12345,\n", | |
" \"username\": \"foo\",\n", | |
" \"email\": \"foo@example.com\",\n", | |
" \"profile\": {\n", | |
" \"name\": \"Foo\",\n", | |
" \"age\": 33,\n", | |
" \"interests\": [\"Python\", \"Programming\"],\n", | |
" },\n", | |
" \"settings\": {\n", | |
" \"theme\": \"light\",\n", | |
" \"language\": \"eng\",\n", | |
" },\n", | |
"}\n", | |
"\n", | |
"\n", | |
"new_dict = {\n", | |
" \"username\": \"goo\",\n", | |
" \"profile\": {\n", | |
" \"age\": 34,\n", | |
" \"interests\": [\"Python\", \"Web Development\"],\n", | |
" },\n", | |
" \"settings\": {\n", | |
" \"theme\": \"dark\",\n", | |
" },\n", | |
"}\n", | |
"\n", | |
"print(\"original_dict\")\n", | |
"pprint(original_dict, sort_dicts=False)\n", | |
"print()\n", | |
"\n", | |
"print(\"new_dict\")\n", | |
"pprint(new_dict, sort_dicts=False)\n", | |
"print()\n", | |
"\n", | |
"updated_dict = update_nested_dict(original_dict.copy(), new_dict)\n", | |
"print(\"updated_dict\")\n", | |
"pprint(updated_dict, sort_dicts=False)\n", | |
"print()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "crawler", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tqdm | |
black | |
isort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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