Skip to content

Instantly share code, notes, and snippets.

class OptimisticDict(dict):
def __init__(self, factory_func):
self.factory_func = factory_func
super(OptimisticDict, self).__init__()
def __missing__(self, key):
self[key] = self.factory_func(key)
return self.factory_func(key)
@aman-tiwari
aman-tiwari / ofApp.cpp
Last active December 17, 2015 08:57
UArm control from Openframeworks
//--------------------------------------------------------------
void ofApp::setup() {
uarm.setup("/dev/tty.usbserial-A600CRMU", 9600);
}
//--------------------------------------------------------------
// x, y, z in cylindrical coordinates, handRot in degrees
void ofApp::setArmTo(int x, int y, int z, int handRot, bool gripper) {
// UArm expects 1 for an open gripper and 2 for a closed gripper
gripper = (int)gripper + 1;
@aman-tiwari
aman-tiwari / of_dir_search.cpp
Created February 19, 2016 23:20
Recursive directory search in openframeworks
const string allowed_ext[] = {"jpg", "png", "gif", "jpeg"}; //File types detected by the search
vector<ofFile> files;
void ofApp::scan_dir(ofDirectory dir){
ofDirectory new_dir;
int size = dir.listDir();
for (int i = 0; i < size; i++){
if (dir.getFile(i).isDirectory()){
new_dir = ofDirectory(dir.getFile(i).getAbsolutePath());
new_dir.listDir();
def unpool(x, repeat=False):
s = x.shape
if repeat:
r = 1
else:
r = 0
kernel_x = np.tile([1, r] + [0,0] * (s[1]), s[1])[:s[1]*s[1]*2].reshape(s[1], (s[1]*2))
kernel_y = np.tile([1, r] + [0,0] * (s[0]), s[0])[:s[0]*s[0]*2].reshape(s[0], s[0]*2)
return np.matmul(np.matmul(x, kernel_x).transpose(), kernel_y).transpose()
@aman-tiwari
aman-tiwari / nearest square grid.cpp
Last active March 16, 2016 22:12
Finds best (closest to square) grid sides for a number of square tiles
int n_tiles; //number of tiles
int grid_x = 0;
int grid_y = 0;
for(int n = 1; n < ceil(sqrt(n_tiles) + 1); n++) {
if(n_tiles%n == 0) {
grid_y = n;
grid_x = n_tiles/n;
}
}
// Parses a url query (key=val&key_2=val_2&key_3=val_3) into a map
std::map<std::string, std::string> query_to_map(const std::string query) {
std::map<std::string, std::string> res;
std::ostringstream curr_key;
std::ostringstream curr_val;
bool in_key = true;
for(const auto& s : query) {
if(s == '=') {
in_key = false;
@aman-tiwari
aman-tiwari / serv.js
Last active June 17, 2016 03:53
node server for talking to XY Plotter
var express = require('express')
var bodyparser = require('body-parser')
var sp = require('serialport')
var SerialPort = sp.SerialPort
var app = express()
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session");
next();
@aman-tiwari
aman-tiwari / https-sticky.md
Created August 1, 2016 21:37 — forked from bprashanth/https-sticky.md
https sticky sessions

Create a backend service that simply serves the pod name, and a frontend haproxy instance that balances based on client cookies.

# This is the backend service
apiVersion: v1
kind: Service
metadata:
  name: hostname
  annotations:
    # Enable sticky-ness on "SERVERID"
    serviceloadbalancer/lb.cookie-sticky-session: "true"
@aman-tiwari
aman-tiwari / url_reader.py
Created November 1, 2016 20:45
read remote image files in tensorflow // url file reader
import tensorflow as tf
from skimage import io
import time
N_THREADS = 64
BATCH_SIZE = 32
N_IMGS = 1000
random_img = 'url to load'
@aman-tiwari
aman-tiwari / particle_system.py
Last active May 25, 2017 06:05
A little particle system for your terminal : )
from itertools import chain
from math import sqrt
from random import random
import drawille
def wrap(x, low, high):
if x > high:
return low