Skip to content

Instantly share code, notes, and snippets.

View boris317's full-sized avatar

Shawn Adams boris317

View GitHub Profile
@boris317
boris317 / commafloat_field.py
Created September 4, 2012 19:51
Subclass of wtforms.core.FloatField that handles floats with comma delimiters.
"""
For reference wtforms.core.FloatField
-------------------------------------
class FloatField(Field):
widget = widgets.TextInput()
def __init__(self, label=None, validators=None, **kwargs):
super(FloatField, self).__init__(label, validators, **kwargs)
@boris317
boris317 / flask_tar.py
Created September 10, 2012 20:57
Stream a tar file with flask.
import os
from cStringIO import StringIO
import tarfile
from flask import Flask, Response
app = Flask(__name__)
def get_string_io_len(s):
existing_hash = {:foo=>"bar"}
other_hash.each_pair do |key, value|
if some_condition
existing_hash[key] = value
end
end
import os
class WSGIApp(object):
def __init__(self):
setup_logger(os.environ.get('PYCSW_CONFIG'))
def __call__(self, env, start_repsonse):
""" Do reqeust stuff here """
application = WSGIApp()
def api_url_for(endpoint, **values):
# probably handle missing api_app key in some
# special exception.
api_app = current_app().config["api_app"]
with api_app.app_context():
return url_for(end_point, **values)
@boris317
boris317 / flask_shell.py
Created September 25, 2012 17:00
IPython interactive shell + Flask App
from exampleapp import app
import IPython
app.testing = True
test_client = app.test_client()
welcome_message = """Welcome to your Flask CLI environment.
The following variables are available to use:
app -> Your Flask app instance.
#!/bin/bash
DIR=$( cd "$( dirname "$0" )" && pwd )
source $DIR/../env.sh
if [ ! X_OPENSHIFT_RUNNING_LOCAL ]; then
source $X_OPENSHIFT_VENV/bin/activate
fi
@boris317
boris317 / build.sh
Created October 1, 2012 15:42
OpenShift action hooks for python tornado deployment.
#!/bin/bash
#
# .openshift/action_hooks/build
#
DIR=$( cd "$( dirname "$0" )" && pwd )
source $DIR/../env.sh
if [ ! -d $X_OPENSHIFT_VENV ]; then
echo "[build] $(virtualenv --distribute $X_OPENSHIFT_VENV)"
@boris317
boris317 / base_converter.py
Last active December 25, 2015 18:09
Encode base 10 integer into other bases
class BaseConverter(object):
"""
Class for encoding/decoding base 10 values to/from another base
"""
def __init__(self, base, base_digits):
"""
:param base: (int) the base (e.g 2, 16, 64)
:param base_digits: (iterator) of base digits (e.g "01" for base 2, etc)
"""
self.base = base
@boris317
boris317 / gist:9675336
Last active August 29, 2015 13:57
Format a `JsonWeb` `ValidationError` stack
def format_validation_error(error, parent=None, indent=""):
def recurse(e, parent):
return format_validation_error(e, parent=p(parent),
indent=indent + " ")
def p(child):
if parent is None:
return child