Coverage for django_napse/utils/usefull_functions.py: 64%
53 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1import math
2from datetime import datetime, timedelta
4from pytz import UTC
7def calculate_mbp(value: str, current_value: float, order, currencies: dict) -> float:
8 ticker, price = value.split("|")
9 price = float(price)
11 current_amount = currencies.get(ticker, {"amount": 0})["amount"]
12 current_value = current_value if current_value is not None else 0
13 received_quote = order.debited_amount - order.exit_amount_quote
14 return (current_amount * current_value + received_quote) / (received_quote / price + current_amount)
17def process_value_from_type(value, target_type, **kwargs) -> any:
18 """Convert a value to a specific type."""
19 target_type = target_type.lower()
20 if value == "None":
21 return None
22 if target_type == "int":
23 return int(value)
24 if target_type == "float":
25 return float(value)
26 if target_type == "bool":
27 match value:
28 case "True" | "true":
29 return True
30 case "False" | "false":
31 return False
32 case _:
33 return value
34 elif target_type == "str":
35 return str(value)
36 elif target_type == "datetime":
37 return datetime.strptime(value, "%Y-%m-%d %H:%M:%S%z").astimezone(UTC)
38 elif target_type == "date":
39 return datetime.strptime(value, "%Y-%m-%d").astimezone(UTC).date()
40 elif target_type == "time":
41 return datetime.strptime(value, "%H:%M:%S").astimezone(UTC).time()
42 elif target_type == "timedelta":
43 split = value.split(", ")
44 days = split[0] if len(split) == 2 else "0 days"
45 timestamp = split[1] if len(split) == 2 else split[0]
46 days = int(days.split(" ")[0])
47 t = datetime.strptime(timestamp, "%H:%M:%S").astimezone()
48 return timedelta(days=days, hours=t.hour, minutes=t.minute, seconds=t.second)
49 elif target_type == "None":
50 return None
51 elif target_type == "plugin_mbp":
52 return calculate_mbp(
53 value=value,
54 current_value=kwargs["current_value"],
55 order=kwargs["order"],
56 currencies=kwargs["currencies"],
57 )
58 else:
59 error_message = f"Unknown target_type: {target_type}"
60 raise ValueError(error_message)
63def round_up(number: float, decimals: int = 0) -> float:
64 """Round a number up to a given number of decimals.
66 Args:
67 ----
68 number (float): The number to round up.
69 decimals (int, optional): The number of decimals to round up to. Defaults to 0.
71 Returns:
72 -------
73 float: The rounded number.
74 """
75 multiplier = 10**decimals
76 return math.ceil(number * multiplier) / multiplier
79def round_down(number: float, decimals: int = 0) -> float:
80 """Round a number down to a given number of decimals.
82 Args:
83 ----
84 number (float): The number to round down.
85 decimals (int, optional): The number of decimals to round down to. Defaults to 0.
87 Returns:
88 -------
89 float: The rounded number.
90 """
91 multiplier = 10**decimals
92 return math.floor(number * multiplier) / multiplier