forked from dxFeed/dxfeed-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_utils.py
60 lines (52 loc) · 1.94 KB
/
class_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from typing import Union, Iterable
import collections
from warnings import warn
from datetime import datetime
import pandas as pd
def to_iterable(value: Union[str, Iterable[str]]) -> Iterable[str]:
"""
Function to correctly wrap string into iterable object
Parameters
----------
value: str, iterable
String or iterable object
Returns
-------
: iterable
"""
if isinstance(value, str):
value = [value]
if not isinstance(value, collections.abc.Iterable):
raise TypeError(f'Expected string or iterable type, got {type(value)}')
return value
def handle_datetime(date_time: Union[str, datetime], fmt: str = '%Y-%m-%d %H:%M:%S.%f', exact_format: bool = True):
"""
Function to convert string of date and time to datetime object.
Parameters
----------
date_time: str, datetime.datetime
Datetime to convert
fmt: str
Format of expected datetime
exact_format: bool
If False no warning will be thrown in case of incomplete date_time parameter. Default - True
Returns
-------
date_time: datetime.datetime
date_time argument converted to datetime.datetime object.
"""
if isinstance(date_time, str):
try:
date_time = datetime.strptime(date_time, fmt)
except ValueError:
try:
date_time = pd.to_datetime(date_time, format=fmt, infer_datetime_format=True)
if exact_format:
warn(Warning(f'Datetime argument does not exactly match {fmt} format,' +
' date was parsed automatically as ' +
date_time.strftime(format=fmt)))
except ValueError:
raise ValueError(f'Datetime should use {fmt} format!')
if not isinstance(date_time, datetime):
raise TypeError(f'Datetime has incorrect type of {type(date_time)}, should be string or datetime')
return date_time