Skip to content

Instantly share code, notes, and snippets.

@phalt
phalt / character.py
Created December 22, 2014 16:57
Get a character with a specific name from swapi.co
import swapi
get_character("name"):
for c in swapi.get_all("people"):
if c.name == name:
return c
# Change your settings file to this:
SECRET_KEY = os.environ.get('SECRET_KEY', 'MY_DEFAULT_KEY')
# Export the SECRET_KEY variable like in the terminal like so:
$ export SECRET_KEY=my_secret_key_here
@phalt
phalt / main.go
Created December 3, 2014 14:51
Simple HTTP API in Go
package main
import (
"github.com/gin-gonic/gin"
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"log"
"time"
"strconv"
def get_persistence(array_of_numbers):
for current_number in array_of_numbers:
if current_result is None:
current_result = current_number
else:
current_result = current_result * current_number
if len(current_result) == 1:
@phalt
phalt / merge_sort.py
Last active March 8, 2017 10:50
Binary tree / merge sort example in Python
# A merge sort algorithm that is probably bigger than it needs to be.
# Uses binary trees to sort integers.
# Runtime is probably O(NlogN)
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
@phalt
phalt / lrucache.py
Created November 9, 2014 19:58
LRU Cache
class LRUCache(object):
""" Implements a least-recently-used cache in Python
As the cache grows it will cap at some fixed size described by the max_size
property
When you attempt to add a new object into a full cache, it will discard the
Least Recently Used cached item.
Calling set('key', ...) or get('key') constitutes a "use" of 'key'
@phalt
phalt / linear.py
Created October 29, 2014 22:41
Linear / insertion sort in Python
haystack = [5,3,2,4,1]
for i in range(1, len(haystack)):
x = haystack[i]
c = i
while c > 0 and haystack[c-1] > x:
haystack[c] = haystack[c-1]
c = c - 1
haystack[c] = x
@phalt
phalt / server.py
Last active August 29, 2015 14:07
Twilio App Monitor Webhooks part 5
from flask import Flask, request
app = Flask(__name__)
from twilio.rest import TwilioRestClient
client = TwilioRestClient()
@app.route('/error_trigger', methods=['POST'])
def error_triggers():
@phalt
phalt / server.py
Created September 29, 2014 12:45
Twilio App Monitor Webhooks part 4
from flask import Flask, request
app = Flask(__name__)
@app.route('/error_trigger', methods=['POST'])
def error_triggers():
error_code = request.values.get('ErrorCode', None)
description = request.values.get('Description', None)
if error_code:
msg = 'An error on Twilio occurred! ' + description
@phalt
phalt / server.py
Last active August 29, 2015 14:07
Twilio App Monitor Webhooks part 3
from flask import Flask, request
app = Flask(__name__)
@app.route('/error_trigger', methods=['POST'])
def error_triggers():
error_code = request.values.get('ErrorCode', None)
return 'The error is' + error_code
if __name__ == "__main__":