Skip to content

Instantly share code, notes, and snippets.

View alan-ho's full-sized avatar

Alan Ho alan-ho

View GitHub Profile
@alan-ho
alan-ho / Find Key Value Path.py
Last active July 22, 2023 07:15
Find value of key in a nested dictionary in Python
def find_key_in_nested_dict(d, target_key, path=None):
if path is None:
path = []
for key, value in d.items():
current_path = path + [key]
if key == target_key:
return current_path
@alan-ho
alan-ho / print_filter.js
Created January 4, 2018 01:17
Crossfilter & dc filter printer source: codeproject
function print_filter(filter) {
var f=eval(filter);
if (typeof(f.length) != "undefined") {}else{}
if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
}
@alan-ho
alan-ho / Postman current timestamp for UTC as ISO 8601.md
Created January 4, 2018 01:16
Postman current timestamp for UTC as ISO 8601

I wanted to know: How can I get the current timestamp for UTC in ISO 8601 format to appear in the body data of a Postman request?

After reading some of the Postman documentation and online comments, this is the solution (using Postman v5.0.1 for Chrome 58.0.3029.110 on macOS 10.12.5) I used:

  1. In the Builder, while editing a request, click the "Pre-request Script" heading below the URL field.

  2. In the editor field that appears, enter this single line of JavaScript:

    postman.setGlobalVariable('timestampUtcIso8601', (new Date()).toISOString());
@alan-ho
alan-ho / detect.py
Created January 4, 2018 01:12
Flask browser detection
browser = request.user_agent.browser
version = request.user_agent.version and int(request.user_agent.version.split('.')[0])
platform = request.user_agent.platform
uas = request.user_agent.string
if browser and version:
if (browser == 'msie' and version < 9) \
or (browser == 'firefox' and version < 4) \
or (platform == 'android' and browser == 'safari' and version < 534) \
or (platform == 'iphone' and browser == 'safari' and version < 7000) \
@alan-ho
alan-ho / nea_json.py
Last active January 4, 2018 01:27
NEA Weather JSON objects
weather_mapping = {
"CL": "Cloudy",
"DR": "Drizzle",
"FA": "Fair (Day)",
"FG": "Fog",
"FN": "Fair (Night)",
"FW": "Fair & Warm",
"HG": "Heavy Thundery Showers with Gusty Winds",
"HR": "Heavy Rain",
"HS": "Heavy Showers",
@alan-ho
alan-ho / BeautifulSoup.py
Last active January 4, 2018 01:27
Creating scrapers and using them
from urllib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import re
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html, 'html.parser')
@alan-ho
alan-ho / DataFrames.py
Last active January 4, 2018 01:27
Creating Pandas DataFrames
#Create DataFrame from Dictionary
import pandas as pd
data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012], 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],'wins': [11, 8, 10, 15, 11, 6, 10, 4], 'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
football = pd.DataFrame(data)
print (football.dtypes)
print (football.describe())
@alan-ho
alan-ho / bokeh_scatter_shapes.py
Last active May 5, 2018 10:44
A scatter plot with different shapes
# Create the figure: p
p = figure(x_axis_label='fertility (children per woman)', y_axis_label='female_literacy (% population)')
# Add a circle glyph to the figure p
p.circle(fertility_latinamerica, female_literacy_latinamerica)
# Add an x glyph to the figure p
p.x(fertility_africa, female_literacy_africa)
# Specify the name of the file
@alan-ho
alan-ho / bokeh_scatter_plot
Last active January 4, 2018 01:28
Bokeh a simple scatter plot
# Import figure from bokeh.plotting
from bokeh.plotting import figure
# Import output_file and show from bokeh.io
from bokeh.io import output_file, show
# Create the figure: p
p = figure(x_axis_label='fertility (children per woman)', y_axis_label='female_literacy (% population)')
# Add a circle glyph to the figure p
@alan-ho
alan-ho / Anaconda Commands
Last active January 4, 2018 01:28
List of Anaconda commands
#Create virtual environments
$ conda create --name <ENVIRONMENT_NAME> anaconda
#To activate this environment, use:
$ source activate <ENVIRONMENT_NAME>
#To deactivate this environment, use:
$ source deactivate
#Remove an environment: