Skip to content

Instantly share code, notes, and snippets.

View lucascheung's full-sized avatar
🎯
Focusing

Lucas Cheung lucascheung

🎯
Focusing
View GitHub Profile
@lucascheung
lucascheung / fuzzy.py
Created September 1, 2020 12:32
Autocorrect misspelled words from a master list.
from fuzzywuzzy import process
def find_best_match(misspelled, correct_names):
closest, ratio = process.extractOne(misspelled, correct_names)
return closest
print(find_best_match('noo yurk', ['New York', 'Boston', 'Washington']))
@lucascheung
lucascheung / all_files_in_a_directory_to_list.py
Created July 16, 2020 14:02
all files in a directory to list
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
@lucascheung
lucascheung / check_empty_object.js
Created May 14, 2020 09:26
Check if a JavaScript object is empty
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
@lucascheung
lucascheung / pandas_groupby.py
Created April 8, 2020 15:22
pandas groupby method with other columns
df = df.groupby(['client_id','data_source'],as_index=False).agg({
'client': 'first',
'currency': 'first',
'total_revenue': 'sum',
'total_accrual': 'sum',
'revenue': 'sum',
'media_cost': 'sum'
})
@lucascheung
lucascheung / last_x_days.py
Created April 8, 2020 14:46
get dates for the last x days.
from datetime import datetime, timedelta
def last_x_days(days):
"""
INPUT: int, date range, number of days
OUTPUT: dict, of the date range
"""
t = datetime.today()
y = t - timedelta(days=1)
m = y - timedelta(days=days)
y_day = y.strftime("%Y%m%d")
@lucascheung
lucascheung / df_to_gsheet.py
Last active March 30, 2024 03:13
Upload pandas dataframe Google Sheet
import json
from datetime import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g
def upload_to_sheet(df,sheet_id, sheet_name):
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
@lucascheung
lucascheung / df_to_outputs.py
Created April 6, 2020 11:40
pandas dataframe to various output
from bigquery_schema_generator.generate_schema import SchemaGenerator
import json
from datetime import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g
def report_output_helper(report_name, df, output):
report_name = report_name.lower().replace(" ", "")
date = datetime.today().strftime('%Y%m%d')
@lucascheung
lucascheung / dictionary_to_outputs.py
Last active April 6, 2020 11:40
Python dictionary to various output
from bigquery_schema_generator.generate_schema import SchemaGenerator
import json
from datetime import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g
def output_helper(data, output):
level = level.lower()
date = datetime.today().strftime('%Y%m%d')
@lucascheung
lucascheung / flatten.py
Last active June 6, 2020 10:13
Flatten any nested objects on Python
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)