This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = pd.read_csv(mappings['Filepath']) | |
# lowercase and strip whitespace from all column headers | |
data.rename({col: col.strip().lower() for col in data.columns}, | |
axis=1, inplace=True) | |
# we must also do the same for inv_mappings | |
# also add snake_case formatting to internal columns (optional) | |
inv_mappings = { | |
mappings[key].strip().lower(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
empty = 0 # initialize our empty cell count | |
while empty < 2: | |
# increment the row number (which begins at the row we found 'Internal' | |
row += 1 | |
# assign the value we find in the 'Internal' column to internal | |
internal = ws[f'B{row}'].value | |
if internal is None: | |
empty += 1 # if we get blank row, increment empty counter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define function to search a given column in an openpyxl worksheet object | |
def search_col(sheet, column, value, limit=100): | |
# loop through each row upto the limit, beginning from 1 | |
for row in range(1, limit+1): | |
if sheet[f"column{row}"].value == value: | |
# if we find the value we are searching for, return col + row | |
return (col, row) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# first we set the path to Excel file | |
path = r".\documents\tool_setup.xlsx" | |
# open the file as a workbook object | |
wb = openpyxl.load_workbook(path, data_only=True) | |
# find the index of 'Mapping' sheet | |
idx = [i for i, name in wb.sheetnames if name == 'Mapping'][0] | |
# set the mapping sheet as active |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpyxl | |
# loan the workbook object using 'sheet.xlsx' | |
wb = openpyxl.load_workbook("sheet.xlsx", data_only=True) | |
# create a worksheet object with the active sheet | |
ws = wb.active | |
# get the value in cell 'E4' | |
value = ws['E4'].value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mappings = {'loan identifier': 'loan_id', | |
'amt': 'amount', | |
... | |
'init fees': 'initial_fees'} | |
data.rename(mappings, axis=1, inplace=True) | |
# now, for the remainder of the code we use the data's Python given column names, 'loan_id', 'loan_amount', etc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from email.mime.text import MIMEText | |
from email.mime.image import MIMEImage | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
def message(subject="Python Notification", text="", img=None, attachment=None): | |
# build message contents | |
msg = MIMEMultipart() | |
msg['Subject'] = subject # add in the subject |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
end = datetime.datetime.now() # get the ending datetime | |
# get the total runtime in hours:minutes:seconds | |
hours, rem = divmod((end - start).seconds, 3600) | |
mins, secs = divmod(rem, 60) | |
runtime = '{:02d}:{:02d}:{:02d}'.format(hours, mins, secs) | |
# now built our message | |
notify.msg( | |
subject="Cashflow Model Completion", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import notify | |
from data import Sql # see https://jamescalam.github.io/pysqlplus/lib/data/sql.html | |
dt = Sql('database123', 'server001') # setup the connection to SQL Server | |
for i, file in enumerate(os.listdir('../data/new')): | |
dt.push_raw(f'../data/new/{file}') # push a file to SQL Server | |
# once the upload is complete, send a notification |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import notify | |
START = datetime.now() # this line would be placed before model training begins | |
MODELNAME = "Synthwave GAN" # giving us our model name | |
NOTIFY = 100 # so we send an update notification every 100 epochs | |
# for each epoch e, we would include the following code | |
if e % notify_epoch == 0 and e != 0: | |
# here we create the email body message | |
txt = (f"{MODELNAME} update as of " |
NewerOlder