Skip to content

Instantly share code, notes, and snippets.

View swateek's full-sized avatar
🎯
One Small Step at a Time

Swateek Jena swateek

🎯
One Small Step at a Time
View GitHub Profile
@swateek
swateek / bucket_sizes.js
Last active July 28, 2017 10:12
Freedman-Diaconis thumb rule for number of bins of a histogram
// metric = array of real numbers (like > 100 or something)
// IQR = inter-quaartile-range
function numBins(metric, defaultBins) {
var h = binWidth(metric), ulim = Math.max.apply(Math, metric), llim = Math.min.apply(Math, metric);
if (h <= (ulim - llim) / metric.length) {
return defaultBins || 10; // Fix num bins if binWidth yields too small a value.
}
return Math.ceil((ulim - llim) / h);
}
@swateek
swateek / bucket_sizes.py
Created July 28, 2017 10:12
Freedman-Diaconis thumb rule for number of bins of a histogram
import math
def numBins(metric, defaultBins):
h = binWidth(metric)
ulim = max(metric)
llim = min(metric)
if (h <= (ulim - llim) / len(metric)):
return defaultBins or 10
return int(math.ceil((ulim - llim) / h))
@swateek
swateek / histogram.py
Created July 28, 2017 10:17
Plotting a Histogram with bucket sizes
import numpy as np
import matplotlib.pyplot as plt
hist, bin_edges = np.histogram([1, 1, 2, 2, 2, 2, 3], bins = range(5))
plt.bar(bin_edges[:-1], hist, width = 1)
plt.xlim(min(bin_edges), max(bin_edges))
plt.show()
@swateek
swateek / export_to_csv_from_mongo
Last active September 7, 2017 17:37
Export Data from MongoDB to .csv
// export data to csv
print("ap_mac,tags");
db.managedaps.find({}).forEach(function(ap){
print(ap.mac+","+ap.tags);
});
// mongo dbname filename.js > out.csv
@swateek
swateek / interval_timer.py
Last active September 12, 2017 14:09
A python piece that can run some function at regular 'x' seconds interval
from datetime import datetime
import time
def task():
print "This is my task at - " + str(datetime.now())
while True:
task()
time.sleep(5)
@swateek
swateek / multiple_filter_criterias.js
Created September 14, 2017 06:21
Using multiple filter fields and filter data with REST
var params = {};
var filter = {};
params['filterField'] = "field1,field2";
params['filterData'] = "2,3;abc,cde";
if(params.hasOwnProperty('filterField') && params.hasOwnProperty('filterData')){
var filterFieldArr = params.filterField.split(',');
var filterDataArr = params.filterData.split(';');
@swateek
swateek / mongodb_collection_sizes.js
Created September 25, 2017 16:44
Use to calculate sizes of MongoDB collections.
use <dbname>;
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function(n){
stats.push(db[n].stats());
});
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
@swateek
swateek / Exporting collection size to csv.
Created September 26, 2017 08:12
collection_size_to_csv.js
use <dbname>;
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function(n){
stats.push(db[n].stats());
});
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
@swateek
swateek / nodejs_clusters.js
Last active October 11, 2017 06:34
NodeJS Clusters
'use strict';
const cluster = require('cluster');
const http = require('http');
const numCores = 3;
var workerIds = [];
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
@swateek
swateek / fresh-chrome-with-custom-tz.sh
Created February 15, 2018 03:31 — forked from prasadsilva/fresh-chrome-with-custom-tz.sh
Launch new instances of Google Chrome on OS X with isolated cache, cookies, user config and custom Timezone
#!/usr/bin/env bash
# fresh-chrome
#
# Use this script on OS X to launch a new instance of Google Chrome
# with its own empty cache, cookies, and user configuration.
#
# The first time you run this script, it will launch a new Google
# Chrome instance with a permanent user-data directory, which you can
# customize below. Perform any initial setup you want to keep on every