Coverage for src/chainalysis/utils/stringify.py: 100%
28 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-20 16:53 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-20 16:53 -0400
1import datetime
2import decimal
4from chainalysis.util_functions.check_list_type import get_list_type
5from chainalysis.util_functions.exceptions import BadRequest
8class Stringify:
9 """
10 This class contains functions to transform python objects
11 into formatted string params so they can be used to query
12 Data Solutions' databases without needing to be transformed
13 manually by the user.
14 """
16 def lists(self, _list: list) -> str:
17 """
18 Convert a list to a formatted string param.
20 :param list: The list to be converted.
21 :type list: list
23 :raises Exception: Raises an exception if a list type is not submitted.
24 :raises Exception: If lists do not contain the right types.
25 :return: The converted list.
26 :rtype: str
27 """
29 if not isinstance(_list, list):
30 raise BadRequest(
31 "Incorrect type. Supply a list.",
32 )
34 type = get_list_type(_list)
36 if type == int or type == bool or type == float or type == decimal:
37 return f"({', '.join(map(str, _list))})"
39 if type == str:
40 result = "("
42 for i, element in enumerate(_list):
43 if i > 0:
44 result += ", "
45 result += f"'{element}'"
47 result += ")"
48 return result
50 def columns(self, columns: list) -> str:
51 """
52 Convert a column select list to a formatted string param.
54 :param list: The column select list object to be converted.
55 :type list: list
57 :return: The converted column select list.
58 :rtype: str
59 """
60 type = get_list_type(columns)
61 if type != str:
62 raise BadRequest("Columns must be a string list")
63 return ", ".join(columns)
65 def datetimes(self, _datetime: datetime.datetime) -> str:
66 """
67 Convert a datetime object to a formatted string param.
69 :param list: The datetime object to be converted.
70 :type list: list
72 :return: The converted datetime object.
73 :rtype: str
74 """
76 if not isinstance(_datetime, datetime.datetime):
77 raise BadRequest("Incorrect type. Supply a datetime.datetime object.")
79 return f"'{_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')}'"