Skip to content

Instantly share code, notes, and snippets.

@tehasdf
tehasdf / LICENSE
Last active July 21, 2020 21:47
A quick hack to create "forms" using urwid.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
from flask import Flask, Blueprint, current_app, request
from collections import namedtuple
class ConfigBlueprint(Blueprint):
def register(self, app, options, first_registration):
self.options = options
super(ConfigBlueprint, self).register(app, options, first_registration)
<!doctype html>
<html>
<head>
<title>asd</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script>
@tehasdf
tehasdf / gist:4749354
Last active December 12, 2015 09:09
throwing errors from within errorhandlers with flask
from flask import Flask, abort
app = Flask(__name__)
@app.route('/')
def foo():
abort(403)
return 'this wont ever be returned'
@app.errorhandler(403)
from twisted.web.resource import Resource
from twisted.web import server
from flask import Flask, render_template_string
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
import json
INDEX_TEMPLATE = """
from klein import Klein
app = Klein()
@app.route('/')
def index(request):
return 'foo'
# expose a 'resource' name for use with twistd web
resource = app.resource
from twisted.internet.protocol import Factory
class MyFactory(Factory):
protocol = MyProtocol
def __init__(self, queue):
self._queue = queue
def buildProtocol(self, addr):
return self.protocol(self._queue)
# coding: utf-8
"""
Example of how to deal with user objects that can implement either __unicode__
or __str__, and we don't know which - eg. exceptions, into which the user
might've passed a bytes message or a unicode message.
One function shows how to convert both cases to bytes (str), the other function
shows how to convert both cases to unicode - depending what we need.
BTW note that it is much better to always know if we're dealing with bytes or
class ConnectionPool(txpostgres.ConnectionPool):
"""A txpostgres ConnectionPool that renders sqlalchemy queries
This is so you can pass a sqlalchemy Query object to .runQuery
and .runOperation. It will render it using the postgresql sqlalchemy
dialect.
"""
def __init__(self, **kwargs):
kwargs.setdefault('cursor_factory', NamedTupleCursor)
self._sqla_dialect = dialect()
from cloudify.decorators import workflow, operation
from cloudify.workflows import ctx as workflow_ctx
@operation
def sometask(ctx):
ctx.logger.info('ok {0}'.format(ctx)) # ctx is a CloudifyContext, not a WorkflowContext