Skip to content

Instantly share code, notes, and snippets.

View alexwright's full-sized avatar

Alex alexwright

View GitHub Profile
@alexwright
alexwright / gist:1853977
Created February 17, 2012 15:15
Get enum values from a MySQL field (PHP, PDO).
/**
* Don't let user input anywhere near this!
* Since the PDO::bind* methods escape a STR with 's I couldn't use them. :(
*/
protected function get_enum ($table_name, $field_name)
{
$sql = "desc {$table_name} {$field_name}";
$st = $this->db->prepare($sql);
if ($st->execute())
@alexwright
alexwright / loader.js
Created February 24, 2012 15:00
Async loader to load the yepnope.js loader
/* I heard you like async loading..
So I wrote an async loader blob so you can async load and async loader */
(function(d) {
var b = document, a = b.createElement("script"), c = !1, b = b.head || b.getElementsByTagName("head");
a.onload = a.onreadystatechange = function() {
if(!(a.readyState && "complete" !== a.readyState && "loaded" !== a.readyState || c)) {
a.onload = a.onreadystatechange = null, c = !0, yepnope(d)
}
};
a.src = "/static/js/yepnope.js";
@alexwright
alexwright / ASN.php
Created May 5, 2012 19:18
PHP class(es) to parse and generate some pretty basic ASN.1 stuff.
<?php
class ASN {
public static function decode ($binary_string)
{
$parser = new ASN_Parser($binary_string);
return $parser->parse();
}
public static function encode ($asn_data)
function relativeTime(milliseconds, withoutSuffix, lang) {
var seconds = round(Math.abs(milliseconds) / 1000),
minutes = round(seconds / 60),
hours = round(minutes / 60),
days = round(hours / 24),
years = round(days / 365),
args = seconds < 45 && ['s', seconds] ||
minutes === 1 && ['m'] ||
minutes < 45 && ['mm', minutes] ||
hours === 1 && ['h'] ||
def get_commit_activity(self, project):
from datetime import date, timedelta
from pygit2 import Repository
from pygit2 import GIT_SORT_TIME
repo = Repository(project.gitrepo)
weeks = self.get_weeks()
for commit in repo.walk(repo.head.oid, GIT_SORT_TIME):
commit_time = date.fromtimestamp(commit.commit_time)
commit_week = commit_time - timedelta(days=commit_time.weekday())
<!doctype html>
<html>
<head>
</head>
<body>
<h1>Hi</h1>
<div class="widget" style="padding: 2em;">
<div class="some-menu-whatever">
<button data-color="red" class="btn-change-color">red</button>
<button data-color="blue" class="btn-change-color">blue</button>
@alexwright
alexwright / sort.py
Created February 14, 2013 19:14
Quick and dirty script to sort pictures into a directory structure based on EXIF DateTaken date/times.
import os
import EXIF
import datetime
work_dir = '../pics'
exif_date_format = '%Y:%m:%d %H:%M:%S'
for dirname, dirnames, filenames in os.walk(work_dir):
for filename in filenames:
old_file_path = os.path.join(work_dir, filename)
@alexwright
alexwright / gist:5526721
Created May 6, 2013 17:43
Traverse an object based on dot notation.
a = {foo:{bar:{baz:42}}};
"foo.bar.baz".split('.').reduce(function(o, i) { return o[i]; }, a);
@alexwright
alexwright / reagent.sh
Created February 2, 2014 16:41
Find the most recently created ssh-agent socket and export it to the current session.
#!/bin/bash
export SSH_AUTH_SOCK=$(find /tmp/ssh-* -user `whoami` -name agent\* -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' | head -n 1);

Keybase proof

I hereby claim:

  • I am alexwright on github.
  • I am alexw (https://keybase.io/alexw) on keybase.
  • I have a public key whose fingerprint is 56B0 74A6 B6EA 8A96 D951 F54D F8A8 9EB0 2E92 4159

To claim this, I am signing this object: