Skip to content

Instantly share code, notes, and snippets.

View cgrinaldi's full-sized avatar

Chris Rinaldi cgrinaldi

View GitHub Profile
@cgrinaldi
cgrinaldi / timer_decorator.py
Created December 12, 2018 16:42
Timer decorator for function timing
import functools
import time
def timer(func):
"""Print the runtime of a decorated function."""
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter()
value = func(*args, **kwargs)
end_time = time.perf_counter()
@cgrinaldi
cgrinaldi / 0_reuse_code.js
Created November 29, 2016 21:48
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@cgrinaldi
cgrinaldi / basic-webpack-config.js
Created November 21, 2015 18:26
A basic Webpack configuration
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'webpack-dev-server/client?http://localhost:8080',
'./src/js/app',
'./src/style.scss'
],
@cgrinaldi
cgrinaldi / js-example.jsx
Last active November 21, 2015 17:57
An example of transpiling JSX into JavaScript
var App = (
<Form>
<Row>
<Label />
<Input />
</Row>
</Form>
);
@cgrinaldi
cgrinaldi / promise-mistake-after.js
Created June 14, 2015 23:18
An example of properly using promises (no deferred!)
var getConfig = function() {
var readFile = Q.nfbind(fs.readFile);
return readFile(CONFIG_DIR + '/aFile.conf', 'utf-8').then(function(data) {
var configJSON = helpers.configStr2JSON(data, true);
return configJSON;
});
};
@cgrinaldi
cgrinaldi / promise-mistake-before.js
Created June 14, 2015 23:17
An example where I was using promises all wrong...
var fs = require('fs');
var getConfig = function() {
// Using promises because fs.readFile() is async
var deferred = Q.defer();
fs.readFile(__dirname + '/../../aFile.conf', 'utf-8', function(err, data) {
if (err) {
deferred.reject(err);
}
var configJSON = helpers.config2JSON(data, true); // true indicates we are parsing the input section
deferred.resolve(configJSON);
a()
.then(b)
.then(c)
.then(d);
@cgrinaldi
cgrinaldi / callback-pyramid.js
Created June 14, 2015 23:05
An example of the callback pyramid of doom
a (function (data1) {
b (function (data2) {
c (function (data3) {
d (function (data4) {
e (function (data5) {
f (function (data6) {
// The Egyptions would be jealous of this pyramid!
})
}
})
@cgrinaldi
cgrinaldi / hoisting-func-expr-I.js
Last active August 29, 2015 14:17
A simple example of hoisting with function expressions
sayHello(); // will error out (see below for reason)
var sayHello = function() {
console.log('Hello, world!');
};
@cgrinaldi
cgrinaldi / hoisting-func-decl-I.js
Last active August 29, 2015 14:17
A simple example of hosting with a function declaration
sayHello(); // logs 'Hello, world!'
function sayHello() {
console.log('Hello, world!');
}