-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
70 lines (60 loc) · 2.63 KB
/
utilities.py
File metadata and controls
70 lines (60 loc) · 2.63 KB
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
69
70
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 26 09:36:30 2016
@author: moleary
"""
import os
import time
import urllib
import sqlalchemy as sql
import pandas as pd
from datetime import date,timedelta
from calendar import monthrange
from pandas.tseries.holiday import USFederalHolidayCalendar as fedHoliCal
def __getstate__(self):
"""Anywhere logger is defined in __init__ this function overwriting __getstate__ is needed.
Try/Excepts will be needed around all logging stuff for debug to work properly. Can't pickle logging."""
return dict((k, v) for (k, v) in self.__dict__.iteritems() if k != 'logger')
def search(fname, directory):
for dirpath, dirnames, filenames in os.walk(directory):
if fname in filenames:
return os.path.join(dirpath,fname)
def currTime():
"""generates Current Date/time in clean format"""
return str(time.strftime("%a %d-%b-%Y, %I:%M:%S%p"))
def sqlConn(serverName,dbName,userName,password):
params = urllib.parse.quote_plus('''DRIVER={SQL Server};SERVER='''+serverName+''';PORT=1433;
DATABASE='''+dbName+''';UID='''+userName+''';PWD='''+password)
engine = sql.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
return engine
#Find n-th work Day for specific month
#Default is current month, month needs to be numeric 1-12
def businessDay(month='', day=1):
if month not in list(range(1,13)):
return "ERROR: Month value outside of range"
if month == '':
begMon = date.today().replace(day=1)
endMon = date.today().replace(day=monthrange(date.today().year,date.today().month)[1])
cal = fedHoliCal()
count = 0
if day > monthrange(date.today().year,date.today().month)[1]:
return "ERROR: Day outside of month range"
else:
drange = pd.bdate_range(start=begMon,periods=day).to_pydatetime()
for day in drange:
if day in cal.holidays(begMon,endMon):
count+=1
return drange[-1]+timedelta(days=count)
else:
begMon = date(date.today().year,month,1)
endMon = begMon.replace(day=monthrange(begMon.year,month)[1])
cal = fedHoliCal()
count = 0
if day > monthrange(begMon.year,month)[1]:
return "ERROR: Day outside of month range"
else:
drange = pd.bdate_range(start=begMon,periods=day).to_pydatetime()
for day in drange:
if day in cal.holidays(begMon,endMon):
count+=1
return drange[-1]+timedelta(days=count)