Skip to content

Instantly share code, notes, and snippets.

@viperscape
viperscape / ec2-power.py
Last active June 8, 2016 16:55
script to power on/off ec2 instances based on block times, reassociates eip if needed and saves cache of eips when rerun
import boto.ec2
import sys
import datetime
from time import gmtime, strftime, sleep
import toml
# expects ec2 instance tag: power-off
# values should be in 24hr clock from:to
# eg 2400:0400
# would result in midnight to 4am
@viperscape
viperscape / ec2-snapshots.py
Last active June 8, 2016 16:54
ec2 snapshot script, max snaps sets revolving snapshots to expire when rerun; expects volume to have tag: snapshot, some-unique-name
import boto.ec2
import sys
def get_snaps (desc, vol,conn):
snapshots = conn.get_all_snapshots(owner='id...',
filters={'description':'*'+desc+'*'})
return snapshots
def op_snaps (vols,max_snaps,conn):
@viperscape
viperscape / handler_clients.clj
Created January 20, 2014 21:22
http-kit connection timeout handling, client tracking. ping-clients should probably be named differently. assumes traffic is json.
(def clients (atom {}))
(defn remove-client [ch] (close ch)(swap! clients dissoc ch))
(defn purge-clients! []
(doseq [c @clients]
(if (> (- (.getTime (java.util.Date.)) (.getTime (:last-seen (val c)))) 60000)
(remove-client (key c)))))
// .. //
static VERT_SRC: &'static str = r"
#version 140
in vec3 pos;
in vec3 norm;
uniform mat4 transform;
uniform vec3 size;
uniform vec3 color;
@viperscape
viperscape / peek-drain.clj
Last active December 28, 2015 08:39
Quickly look at a core.async channel with only momentary blocking, pulls the data if it exists and thus alters the channel.Drain! will repeatedly call peek! until a maximum number of elements is pulled or until the channel is empty.
;;uses core.async and refers chan, >!, go, alts!! and timeout functions
(def c (chan))
(go (>! c {:msg "hi"}))
(defn peek! [ch & args]
"quickly looks at a channel for new incoming data, pulls it if it exists, quits otherwise;
returns seq if specifying max elements to pull"
(if-not (nil? args)
@viperscape
viperscape / matrix operations for 3d graphics.clj
Last active December 25, 2015 23:09
using core.matrix, perform some 3d graphics-related functions lookat, perspective projection, and building translation and rotation matrices; 'left' in the function name specifies left-handed coordinates
;; (ns somenamespace
;; (:refer-clojure :exclude [* - + == /])
;; (:use clojure.core.matrix)
;; (:use clojure.core.matrix.operators)
;; (:gen-class))
(defn inv [n] "returns the inverse, (* -1 n)" (* -1 n))
(defn projection [l r b t, n f, aspect fov]
@viperscape
viperscape / config.rs
Last active December 21, 2015 19:05
toml parser for key bindings, example
/// toml parser and config for keybindings, etc.
use toml::{Parser,Value};
use std::fs::File;
use std::io::Read;
use glium::glutin::VirtualKeyCode;
use std::collections::{HashMap,BTreeMap};
#[derive(Debug)]
pub struct Bindings(HashMap<String,VirtualKeyCode>);
@viperscape
viperscape / maps defentity's to the table listing in sql
Last active December 20, 2015 12:59
information schema to defentity in korma
(defentity INFORMATION_SCHEMA.tables)
(def tables (map #(get % :TABLE_NAME)
(select 'INFORMATION_SCHEMA.tables (fields :TABLE_NAME)
(where {:TABLE_TYPE "BASE TABLE"})))) ;notice the quote
(prn tables)
;(map #(def %) tables) ;additional concept
(map #(defentity %) tables) ;not sure this is required anymore
(select (first tables))
@viperscape
viperscape / night-mode.el
Created September 18, 2015 13:14
flips between solarized dark/light on keypress
(set 'nitemode nil)
(defun toggle-nitemode ()
"toggles nitemode"
(interactive)
(cond ((not nitemode)
(load-theme 'solarized-dark t)
(set 'nitemode 1))
((eq 1 nitemode)
(load-theme 'solarized-light t)
(set 'nitemode nil))))
@viperscape
viperscape / rng.rs
Created February 25, 2015 19:18
range in rust, without the need to think about range direction
#![feature(core)]
extern crate core;
use self::core::num::Int;
fn rng<T:PartialEq+Int, F:FnMut(T)> (d:T,dt:T,mut f:F) {
if d>dt { for n in (dt..d).rev() { f(n); } }
else { for n in (d..dt) { f(n); } }
}
fn main () {