Skip to content

Instantly share code, notes, and snippets.

@mattupstate
mattupstate / uuid_url64.py
Last active July 2, 2020 13:46
Generate unique, URL friendly ID's based on UUID with Python
import re
import uuid
import base64
def uuid_url64():
"""Returns a unique, 16 byte, URL safe ID by combining UUID and Base64
"""
rv = base64.b64encode(uuid.uuid4().bytes).decode('utf-8')
return re.sub(r'[\=\+\/]', lambda m: {'+': '-', '/': '_', '=': ''}[m.group(0)], rv)
@mattupstate
mattupstate / gist:7010498
Created October 16, 2013 16:11
Flask-Mail Log Handler
import logging
from flask_mail import Message
class FlaskMailLogHandler(logging.Handler):
def __init__(self, mail, sender, recipients, subject, *args, **kwargs):
super(FlaskMailLogHandler, self).__init__(*args, **kwargs)
declare module 'humio' {
interface Options {
ssl?: boolean;
host: string;
port: number;
basePath?: string;
dataspaceId?: string;
sessionId?: string;
includeClientMetadata?: boolean;
includeSessionId?: boolean;
@mattupstate
mattupstate / Scale to Fit.jsx
Created August 22, 2012 00:30
Photoshop script to scale an image to fill a specified rectangle and preserve aspect ratio. Save this in <Photoshop Install Folder>/Presets/Scripts and restart Photoshop.
#target photoshop
main ();
function cloneRectangle(rect) {
return { x:rect.x, y:rect.y, width:rect.width, height:rect.height };
}
function rectangle(x, y, width, height) {
return { x:x, y:y, width:width, height:height };
@mattupstate
mattupstate / as_blueprint.py
Last active August 27, 2019 02:55
An `as_blueprint` method for Flask MethodView classes
from flask import Flask, Blueprint
from flask.views import MethodView
class ApiResource(MethodView):
endpoint = None
url_prefix = None
url_rules = {}
@classmethod
@mattupstate
mattupstate / url62.py
Created December 10, 2014 21:29
python url62 implementation
import string
import uuid
alphabet = string.digits + string.ascii_letters
def base62_encode(n):
ret = ''
while n > 0:
ret = alphabet[n % 62] + ret
n /= 62
@mattupstate
mattupstate / supervisord.sh
Created April 19, 2012 18:50
Super simple supervisor init.d script
# Supervisord auto-start
#
# description: Auto-starts supervisord
# processname: supervisord
# pidfile: /var/run/supervisord.pid
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=supervisord
DESC="supervisod is a system for controlling process state"
SUPERVISORD=/usr/local/bin/supervisord
@mattupstate
mattupstate / app.py
Created January 22, 2014 19:45
Odd behavior regarding ordering_list and association_proxy with SQLAlchemy
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.orderinglist import ordering_list
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
db = SQLAlchemy(app)
@mattupstate
mattupstate / helpers.py
Last active January 4, 2016 03:09
A helper function for grabbing pseudo-namespaced configuration options from a Flask application
def namespaced_config_options(app, prefix):
"""Returns a dictionary of configuration options built from
the specified Flask application and string prefix. Keys in the
resulting dictionary will be lowercase.
:param app: a Flask application
:param prefix: a configuration namespace prefix. e.g. `IMAGE_STORE`
"""
rv = {}
for key, value in app.config.iteritems():
@mattupstate
mattupstate / module.sublime-snippet
Created July 19, 2013 13:55
A simple Python module header snippet for Sublime Text
<snippet>
<tabTrigger>module</tabTrigger>
<scope>source.python</scope>
<description>Module</description>
<content><![CDATA[# -*- coding: utf-8 -*-
"""
${1:name}
${1/./~/g}