Skip to content

Instantly share code, notes, and snippets.

View lppier's full-sized avatar

Pier lppier

View GitHub Profile
source ~/shrink_venv/bin/activate
cd $VIRTUAL_ENV/lib/python3.6/site-packages
zip -r9 ~/CreateThumbnail.zip *
cd ~
zip -g CreateThumbnail.zip CreateThumbnail.py
aws lambda update-function-code --function-name CreateThumbnail --zip-file fileb:///home/ec2-user/CreateThumbnail.zip
@lppier
lppier / last_mon_of_month.py
Last active December 26, 2018 03:39
Get the last monday of the month of the date specified (Python)
from datetime import date
from dateutil.relativedelta import relativedelta, MO
# Relative delta replaces the day in the date you specify by day = 31. From this new date, weekday=MO(-1) specifies the last monday.
def get_last_mon_of_mth(dt):
print (dt + relativedelta(day=31, weekday=MO(-1)))
get_last_mon_of_mth(date(2018, 12, 30))
get_last_mon_of_mth(date(2019, 6, 4))
@lppier
lppier / first_monday_of_month.py
Last active December 26, 2018 03:38
first monday of month
from datetime import date
from dateutil.relativedelta import relativedelta, MO
# Relative delta replaces the day in the date you specify by day = 1. From this new date, weekday=MO(+1) specifies the 1st Monday.
def get_first_mon_of_mth(dt):
print (dt + relativedelta(day=1, weekday=MO(+1)))
get_first_mon_of_mth(date(2018, 12, 30))
get_first_mon_of_mth(date(2019, 6, 4))
@lppier
lppier / redis_dataframe_example.py
Last active December 27, 2018 06:41
Example of using redis with a dataframe for caching : Checks if a day has passed. If yes, retrieve from SQL server, else retrieve from Redis
import pandas as pd
import numpy as np
import redis
import pyodbc
from time import time
from datetime import date, timedelta, datetime
r = redis.Redis(host='localhost', port=6379, db=0)
dt_last = datetime.strptime(r.get("last_update_time").decode(), '%Y-%m-%d %H:%M:%S')
dt_now = datetime.now()
@lppier
lppier / redis_python_dict.py
Created January 2, 2019 01:27
Redis Retrieving and Putting a Python Dictionary in Redis
import redis
import json
# Retrieve Redis Stored Values, if any for faster processing
anc_dict_320 = {}
r = redis.Redis(host='localhost', port=6379, db=0)
json_str_320 = r.get("anc_dict_320")
if json_str_320 is not None:
anc_dict_320 = json.loads(json_str_320)
print(anc_dict_320)
import pyodbc
import pandas as pd
# https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017
conn = pyodbc.connect(
r'DRIVER={ODBC Driver 17 for SQL Server};'
r'SERVER=mydbinstance.cazdl38iyoz0.ap-southeast-1.rds.amazonaws.com;'
r'DATABASE=testdb;'
r'UID=pier;'
@lppier
lppier / pretty_seaborn_bar_chart.py
Created January 24, 2019 02:26
Pretty seaborn bar chart, differentiate data by color
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.style as style
style.use('ggplot')
f, ax1 = plt.subplots(1, 1, figsize=(20, 7), sharex=True)
df_mth_plot = pd.DataFrame(list(monthly_growth_dict.items()), columns=['Market', 'Average Monthly Growth Rate %'])
df_mth_plot = df_mth_plot.sort_values('Average Monthly Growth Rate %', ascending=False)
@lppier
lppier / datetime_conversions.py
Last active February 21, 2019 02:59
Converting datetime to day of week
import calendar
df_weekly_season['DOW_ENG'] = weekly_season['ds'].apply(lambda x : calendar.day_name[pd.to_datetime(x).weekday()])
df_weekly_season['DOW'] = weekly_season['ds'].apply(lambda x : int(pd.to_datetime(x).strftime('%w'))) # SUN == 0
df_weekly_season['DOW'] = weekly_season['ds'].apply(lambda x : int(pd.to_datetime(x).weekday()) # MON == 0
@lppier
lppier / cloudera_python36_install
Created March 5, 2019 08:14
Steps to install Python 3.6 on Cloudera VM
> yum install centos-release-scl
> yum info rh-python36
> scl enable rh-python36 bash
> python --version
Run PySpark2
> pyspark2
Python 3.6.3 (default, Apr 26 2018, 13:16:02)
@lppier
lppier / avoid_copywithsetting_error.py
Last active March 6, 2019 02:59
Avoiding pandas copywithsetting error
#SettingWithCopyWarning:
#A value is trying to be set on a copy of a slice from a DataFrame.
#Try using .loc[row_indexer,col_indexer] = value instead
# assign was introduced in pandas 0.16 to deal with this false positive
# Instead of
df_weekly_season.loc[:, 'market'] = market
# or
df_weekly_season['market'] = market