-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
executable file
·68 lines (58 loc) · 2.22 KB
/
utilities.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
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 16:36:44 2020
@author: ryancrisanti
"""
import pandas as pd
import pickle
import os
def format_money_value(value):
ret = f'${abs(value):,.2f}'
if value < 0:
ret = f'({ret})'
return ret
def format_name(name):
return name.strip().title().replace(' ', '')
def is_unique(obj, objs, idfy='name'):
return not any([getattr(obj, idfy)==getattr(o, idfy) for o in objs])
def month_range(start, end):
rng = pd.date_range(start-pd.offsets.MonthBegin(), end=end, freq='MS')
ret = (rng + pd.offsets.Day(start.day-1)).to_series()
ret.loc[ret.dt.month > rng.month] -= pd.offsets.MonthEnd(1)
# return pd.DatetimeIndex(ret)
# made this adjustment to account for inconsistencies
return pd.DatetimeIndex(ret.loc[start:end])
def save(obj, filepath, **kwargs):
filepath = _check_filepath_ext(filepath, expected='.pickle')
with open(filepath, 'wb') as f:
pickle.dump(obj, f, **kwargs)
def load(filepath, **kwargs):
filepath = _check_filepath_ext(filepath, expected='.pickle')
with open(filepath, 'rb') as f:
obj = pickle.load(f, **kwargs)
return obj
def _check_filepath_ext(filepath, expected='.pickle'):
filepath = os.path.abspath(filepath)
fname, ext = os.path.splitext(filepath)
if ext == expected:
pass
elif ext == '':
filepath += expected
else:
raise ValueError(f'File cannot have extension other than "{expected}'\
f'", but got extension "{ext}".')
return filepath
def check_delta_inputs(value, dates, name):
msg = 'Expected `{exp}` to be of type "{exptype}", but got "{got}" '\
'of type "{gottype}" instead'
if not isinstance(value, (float, int)):
raise TypeError(msg.format(exp='value', exptype='float/int',
got=value, gottype=type(value)))
if not isinstance(dates, pd.DatetimeIndex):
raise TypeError(msg.format(
exp='dates', exptype='pd.DatetimeIndex',
got=dates, gottype=type(dates)))
if not isinstance(name, str):
raise TypeError(msg.format(exp='name', exptype='str',
got=name, gottype=type(name)))