Skip to content

Instantly share code, notes, and snippets.

View bchan01's full-sized avatar

Brian Chan bchan01

View GitHub Profile
@bchan01
bchan01 / zip_directory.py
Created August 12, 2020 22:41
Python - ZIP file in a directory
import glob
import zipfile
# Assume there's a directory 'test-dir' with few .txt files in it
files = glob.glob("test-dir/*.txt")
zip_file = zipfile.ZipFile('test.zip', 'w')
with zip_file:
# writing each file one by one
for file in files:
zip_file.write(file)
@bchan01
bchan01 / zip_directory.py
Created August 12, 2020 22:41
Python - ZIP file in a directory
import glob
import zipfile
# Assume there's a directory 'test-dir' with few .txt files in it
files = glob.glob("test-dir/*.txt")
zip_file = zipfile.ZipFile('test.zip', 'w')
with zip_file:
# writing each file one by one
for file in files:
zip_file.write(file)
@bchan01
bchan01 / zip_directory.py
Created August 12, 2020 22:41
Python - ZIP file in a directory
import glob
import zipfile
# Assume there's a directory 'test-dir' with few .txt files in it
files = glob.glob("test-dir/*.txt")
zip_file = zipfile.ZipFile('test.zip', 'w')
with zip_file:
# writing each file one by one
for file in files:
zip_file.write(file)
@bchan01
bchan01 / aws-lambda-sqs-consumer.js
Created April 20, 2020 03:12
AWS Lambda - SQS Consumer
exports.handler = async (event, context) => {
let count = 0;
event.Records.forEach(record => {
console.log('RECEIVE RECORD: ' + JSON.stringify(record, null, 4));
count++;
});
return {'records' : count};
}
@bchan01
bchan01 / presidio_analyzer.py
Created April 12, 2020 23:44
Presidio Data Analyzer
from presidio_analyzer import AnalyzerEngine
import json
engine = AnalyzerEngine()
inputText = "Yo! Brandon is your SSN 100-11-1111? I want to charge $10.0 to your card 5555555555554444. Call me at 215-327-8888. I will pay you back at 36036545555"
# https://microsoft.github.io/presidio/field_types.html
response = engine.analyze(correlation_id=0,
text=inputText,
@bchan01
bchan01 / covid-19_folium_map.py
Last active April 11, 2020 02:08
COVID-19 PA - Folium Map
import folium as folium
# Function to color code categories for number of positive cases
def get_icon_color(cases):
if cases <= 10:
return 'green'
elif cases > 10 and cases < 100:
return 'orange'
else:
return 'red'
# Plot lines
ax = df_diff.plot(colormap='jet', marker='.', markersize=10,
title='Daily Changes in Cases', figsize=(20,10))
# set labels for X and Y axes
ax.set_xlabel("Count")
ax.set_ylabel("Date")
@bchan01
bchan01 / covid-19_daily_change_dataset.py
Created April 11, 2020 01:41
COVID-19 PA - Calculate Daily Changes in Cases
# Reload DataFrame and sort by Date
df_summary = pd.read_csv("pa_summary.csv")
# convert column to Integer for calculation
df_summary = df_summary.astype({"Positive": int, "Negative": int, "Deaths" : int})
df_summary['Tested'] = df_summary['Positive'] + df_summary['Negative']
# Set Index and sort by Date
df_summary.set_index('Date', inplace=True)
df_summary = df_summary.sort_values('Date', ascending = True)
@bchan01
bchan01 / covid-19_case_percent_bar_chart.py
Created April 11, 2020 01:33
COVID-19 PA - Case Percentage Bar Chart
# Setup DataFrame for plotting
df_percent = pd.DataFrame(df, columns=['Date', 'Positive_Percent','Negative_Percent'])
df_percent.set_index("Date", inplace = True)
# Plot bar charts
style.use('ggplot')
bar_chart = df_percent.plot.bar(stacked=False, width=0.8, figsize=(20,15), title="Percentage of Positive and Negative Cases")
plt.xlabel('Date')
plt.ylabel('Percentage')
bar_chart.legend(loc=2)
@bchan01
bchan01 / covid-19_case_line_plot.py
Created April 11, 2020 01:27
COVID-19 PA Analysis
# Plot PA COVID-19 cases to date
style.use('ggplot')
ax = plt.gca()
df.plot(kind='line',x='Date',y='Positive', color='green', ax=ax, figsize=(20,10))
df.plot(kind='line',x='Date',y='Negative', color='blue', ax=ax)
df.plot(kind='line',x='Date',y='Deaths', color='red', ax=ax)
df.plot(kind='line',x='Date',y='Tested', color='black', ax=ax)
plt.title('Number of Cases and Deaths by Date')
plt.xlabel('Date')
plt.ylabel('Count')