Skip to content

Instantly share code, notes, and snippets.

@jchysk
jchysk / require at least one.py
Created August 26, 2015 22:40
Formencode validator that will require a minimum of one field
from formencode import Schema, validators, Invalid
from formencode.validators import FormValidator
class RequireAtLeastOne(FormValidator):
choices = []
__unpackargs__ = ('choices',)
def _convert_to_python(self, value_dict, state):
for each in self.choices:
if value_dict.get(each) is not None:
@jchysk
jchysk / totp to QR.py
Created August 26, 2015 23:20
Secret to QR Code
"""
Function that takes two values to generate a QR Code that can be scanned with LaunchKey or TOTP holder
@param identity: Name and place. E.g. username/Gmail
@param secret: The actual TOTP code
Saves the image locally as a png
Requires PIL (pillow) and qrcode
"""
def convert(self, identity, secret):
import re
@jchysk
jchysk / gist:2ea832c30f4a2d475079
Created May 27, 2014 03:08
Secretary problem
#Original secretary problem solution based on 100 secretaries and just on the ranking of secretaries from 1-100
#want to figure out both average ranking chosen and how often the best candidate is chosen
#function which randomizes a list
def randomize_rankings(sec):
import random
random.shuffle(sec)
return sec
@jchysk
jchysk / gist:7582c7618f9a35b9b5bd
Created May 27, 2014 03:09
Secretary problem; Daut version towards optimizing average instead
#Daut's edited secretary problem
#function which randomizes a list
def randomize_rankings(sec):
import random
random.shuffle(sec)
return sec
#function which returns a secretary based on Daut's algorithm
def chooseSecretary(sec):
@jchysk
jchysk / fabfile.py
Last active August 29, 2015 14:04
Fabfile that can interact with Dockers from any OS
# Docker controller
# example usage
# fab local_os restart_env
from fabric.api import *
def _vagrant():
env.user = "vagrant"
@jchysk
jchysk / email_verifier.py
Created February 9, 2015 23:57
Check that domain of email addresses can be resolved
#!/usr/bin/python
"""
Takes a csv of email addresses and gives back a csv of the email addresses that have a resolvable domain.
"""
domains = list()
checked = []
checked_bad = []
import socket
@jchysk
jchysk / keybase.md
Created February 13, 2015 08:07
Keybase Proof

Keybase proof

I hereby claim:

  • I am jchysk on github.
  • I am jchysk (https://keybase.io/jchysk) on keybase.
  • I have a public key whose fingerprint is 4697 5E88 C723 4E13 57E0 E88D BC7F C851 B93C C464

To claim this, I am signing this object:

@jchysk
jchysk / pick_winner.py
Created April 7, 2015 19:34
Winner Picker using Blockchain
"""
Pick a random winner
"""
if __name__ == '__main__':
import sys
entries = sys.argv[1]
import requests
# Grab the latest hash
latest_hash = requests.get("https://blockchain.info/q/latesthash")
@jchysk
jchysk / shuffler.py
Created October 19, 2013 04:19
Simulate the shuffling of cards
SUITS = ["clubs", "diamonds", "hearts", "spades"]
class Card(object):
one_to_fifty_two = None
number = None
suit = None
positions = None
def __init__(self, number, suit, value):
@jchysk
jchysk / Javascript CSS Loader
Created November 23, 2013 03:33
Javascript CSS Loader
/**
* Framework independent function to load CSS
* @param path To the CSS file
* @returns {HTMLElement}
*/
function loadStyleSheet(path) {
var head = document.getElementsByTagName( 'head' )[0], // reference to document.head for appending/ removing link nodes
link = document.createElement( 'link' ); // create the link node
link.setAttribute( 'id', 'cssLKLoad'); // set ID to check if already loaded
link.setAttribute( 'href', path );