Skip to content

Instantly share code, notes, and snippets.

@chadgh
chadgh / simulation.py
Created March 31, 2017 15:18
Simulation of the Monty Hall / Game show problem
from abc import ABCMeta, abstractmethod
from random import choice
def game(player):
# determine winning and loosing doors for this round
doors = [1, 2, 3]
winning_door = choice(doors)
loosing_doors = [d for d in doors if d != winning_door]
def calc_sale(price, realtor=True, closing=0.03, mortgage=148000):
if realtor:
realtor = 0.05
else:
realtor = 0.00
cost = price * (closing + realtor) + 450
rtn = price - cost - mortgage
return rtn
from os import environ
from fabric.api import run, env
import requests
env.hosts.append('mydomain.com')
LINODE_API_KEY = 'mylinodeapikey' # noqa
DOMAIN_ID = 'mylinodedomainid' # for mydomain.com
IPv4 = 'mylinodeipv4address'
IPv6 = 'mylinodeipv6address'
@chadgh
chadgh / interpolation.py
Last active September 1, 2015 19:40
Function to create an interpolate function.
def make_interpolation(from_range, to_range):
from_min, from_max = from_range
to_min, to_max = to_range
scale = float(to_max - to_min) / float(from_max - from_min)
def interpolate(value):
return to_min + (value - from_min) * scale
return interpolate
change = make_interpolation((0, 100), (0, 200))
print(change(50)) # will print 100.0
from flask import Flask, request
app = Flask(__name__)
app.debug = False
@app.route('/')
def get_ip_address():
style = """
<style>
body { text-align: center; font-size: 8em; color: rgb(176, 204, 105); font-family: Courier; letter-spacing:-0.05em; margin-top: 25px; font-weight: bold; background: rgb(58, 58, 58); }
@chadgh
chadgh / README.md
Last active August 29, 2015 14:16
Trello to Django

What?!?!

Sometimes it is easier to design and talk about models in a more abstract easy to change way. After that design phase is finished, it is nice to have something to take your designs and give you boilerplate code. This does that for Django when models are specified in a specific style in Trello. An example board https://trello.com/b/iPHi7eQz/my-django-models.

Assumptions

  • py-trello==0.1.6 is installed
    • py-trello has a bug that is easily fixed, edit the env/lib/python2.X/site-packages/trello/__init__.py line 577. Change it from self.due = json_obj.get('due', '')[:10] to if json_obj.get('due', ''): self.due = json_obj.get('due', '')[:10]
  • Environment variables are set; TRELLO_KEY, TRELLO_SECRET, TRELLO_TOKEN

Trello Syntax

  • Each list that starts with "Model:" will be turned into a Django model. All other syntax is based on the cards in those lists.
​def make_power(power):
def pow(number):
return number ** power
return pow
cube = make_power(3)
print(cube(3)) # should print '27'
@chadgh
chadgh / debugging_decorators.py
Last active February 15, 2022 04:32
python decorators for; debugging, threading
def timeit(function):
'''Decorator used for debugging. Prints the call and how long it took.'''
def timed(*args, **kwargs):
ts = time.time()
result = function(*args, **kwargs)
te = time.time()
print("{0} ({1}, {2}) {3:.2} sec"
.format(function.__name__, args, kwargs, te - ts))
@chadgh
chadgh / squid.py
Last active December 22, 2015 00:49
The squid module provides a nice easy way to use threading effectively for I/O bound tasks that you want the results back to do something with.
import threading
def tentacle(f, daemon=False):
"""Decorator adopted from http://stackoverflow.com/a/14331755/18992 (thanks bj0)"""
import Queue
def wrapped_f(q, *args, **kwargs):
"""Call decorated function and puts the result in a queue."""
ret = f(*args, **kwargs)
@chadgh
chadgh / chadbot.js
Last active October 13, 2015 18:18
Bot for botswana
var Chad = function() {};
Chad.prototype = new Bot();
Chad.prototype.setup = function() {
this.timer = 0;
this.movements = ['strafe-right', 'strafe-left', 'forward', 'backward'];
this.formation = this.id % 4;
};