Skip to content

Instantly share code, notes, and snippets.

View guangningyu's full-sized avatar
🎯
Focusing

Guangning Yu guangningyu

🎯
Focusing
View GitHub Profile
// Filter object array based on attributes
var foo = [{'a':1, 'b':2}, {'a':2}, {'a':1}]
foo.find(o => o.a === 1) // return { a: 1, b: 2 }
@guangningyu
guangningyu / test_python_static_variable.py
Created October 12, 2018 03:00
test Python static variable
class Person(object):
number = 0 # <= define a static variable
def __init__(self, name):
self.name = name
def say_hello(self):
print("Hello, I'm {name}!".format(name = self.name))
def say_number(self):
@guangningyu
guangningyu / apply_yaml_filters_to_pyspark_dataframe.py
Last active October 9, 2018 10:42
Apply filters defined in yaml file to PySpark dataframe
#!/usr/bin/env python
import yaml
from pyspark import SparkContext
from pyspark.sql import SQLContext
sc = SparkContext()
sqlContext = SQLContext(sc)
# create dataframe
var bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
// Hash a password
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
console.log('> hash a password (generate a salt and hash on separate function calls)');
@guangningyu
guangningyu / redux_demo.html
Last active December 28, 2017 14:22
Handmade Redux Demo
<!DOCTYPE html>
<html>
<head>
<title>Handmade Redux Demo</title>
</head>
<body>
<div>
Counter:
<span id="counter"></span>
</div>
#!/bin/bash
mkdir -p src/{main,test}/{java,resources,scala}
mkdir lib project target
# create an initial build.sbt file
echo 'name := "MyProject"
version := "1.0"
scalaVersion := "2.10.0"' > build.sbt
object HelloWorld {
case class Text(content: String)
case class Prefix(text: String)
// step 2: capture the String conversion in implicit function;
// then find an implicit parameter and end up with finding "prefixLOL"
implicit def String2Text(content: String)(implicit prefix: Prefix) = {
// step 3: return instance of Text
Text(prefix.text + " " + content)
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Calculate the singular value decomposition using the power method.
'''
import numpy as np
from numpy.linalg import norm
from random import normalvariate
@guangningyu
guangningyu / neural_network.py
Created July 25, 2017 17:00
An implementation of Neural Network. Reference: [Implementing a Neural Network from Scratch in Python – An Introduction](http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/)
#!/usr/bin/env python
import numpy as np
import sklearn.datasets
import matplotlib.pyplot as plt
def load_data(n_samples=100, noise=None):
np.random.seed(0)
return sklearn.datasets.make_moons(n_samples=n_samples, noise=noise)
@guangningyu
guangningyu / keras-regression.py
Created July 19, 2017 04:14
Reference: [Regression Tutorial with the Keras Deep Learning Library in Python](http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/)
#!/usr/bin/env python
import urllib2
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler