Skip to content

Instantly share code, notes, and snippets.

View dmitric's full-sized avatar

Dmitri Cherniak dmitric

View GitHub Profile
@dmitric
dmitric / MapBackedBeanUsage.groovy
Created September 29, 2011 07:46
MapBackedBeanUsage
def mappings = [
[csvProperty: "item name", classProperty: "name"],
[csvProperty: "cost in $", classProperty: "cost", conversion: CSVBeanBuilder.STRING_TO_DOUBLE],
[csvProperty: "count", classProperty: "itemCount", conversion: CSVBeanBuilder.STRING_TO_INT],
[csvProperty: "date", classProperty: "purchaseDate", conversion: CSVBeanBuilder.STRING_TO_DATE],
]
CSVBeanBuilder.loadAs(MapBackedBean.class, csvText, mappings).each {
transaction ->
assert(transaction.name == transaction.getName() && transaction.getName() == transaction["name"] )
#add this to your base handler class
def write_error(self, status_code, **kwargs):
error_trace = None
if self.settings.get("debug") and "exc_info" in kwargs:
import traceback
error_trace= ""
for line in traceback.format_exception(*kwargs["exc_info"]):
error_trace += line
self.render("base_error.html", status_code=status_code, error_trace=error_trace)
@dmitric
dmitric / base_wepay.html
Created January 8, 2012 00:42
WePay Asynchronous Client on top of tornado
{% extends "base.html" %}
{% block page_title %} - Buy Goods {% end %}
{% block head %} <script type="text/javascript" src="https://stage.wepay.com/js/iframe.wepay.js"></script> {% end %}
{% block page_content %}
<div id="checkout"></div>
{% end %}
{% block js %}
WePay.iframe_checkout("checkout", "{{checkout["checkout_uri"]}}");
@dmitric
dmitric / backend.py
Created March 24, 2012 18:06
SqlAlchemy usage with tornado backend
class Backend(object):
def __init__(self):
engine = create_engine("mysql://{0}:{1}@{2}/{3}".format(options.mysql_user, options.mysql_pass, options.mysql_host, options.mysql_db)
, pool_size = options.mysql_poolsize
, pool_recycle = 3600
, echo=options.debug
, echo_pool=options.debug)
self._session = sessionmaker(bind=engine)
@classmethod
@dmitric
dmitric / blurb.m
Created August 29, 2012 22:02
Native like iOS scrolling in WebView
self.webView.scrollView.decelerationRate = 0.994f;
@dmitric
dmitric / p.py
Created October 3, 2012 16:56
Template
Traceback (most recent call last):
File "/Users/DLC/.virtualenvs/b/lib/python2.7/site-packages/tornado/web.py", line 1021, in _stack_context_handle_exception
raise_exc_info((type, value, traceback))
File "/Users/DLC/.virtualenvs/b/lib/python2.7/site-packages/tornado/stack_context.py", line 258, in _nested
yield vars
File "/Users/DLC/.virtualenvs/b/lib/python2.7/site-packages/tornado/stack_context.py", line 228, in wrapped
callback(*args, **kwargs)
File "/Users/DLC/.virtualenvs/b/lib/python2.7/site-packages/tornado/gen.py", line 382, in inner
self.set_result(key, result)
File "/Users/DLC/.virtualenvs/b/lib/python2.7/site-packages/tornado/gen.py", line 315, in set_result
@dmitric
dmitric / lol.m
Created October 10, 2012 05:32
How to make regular expressions in objC
static inline NSRegularExpression * TagRegularExpression() {
static NSRegularExpression *_tagRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_tagRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\\#[\\w]+"
options:NSRegularExpressionCaseInsensitive
error:nil];
});
@dmitric
dmitric / auth.py
Created January 17, 2013 04:54
Old examples of auth in tornado
class FacebookGraphLoginHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
def get(self):
my_url = (self.request.protocol + "://" + self.request.host +"/login/facebook?next="+tornado.escape.url_escape(self.get_argument("next", "/")))
if self.get_argument("code", False):
self.get_authenticated_user(redirect_uri=my_url,client_id=self.settings["facebook_api_key"],client_secret=self.settings["facebook_secret"],code=self.get_argument("code"),callback=self._on_auth,extra_fields=['email'])
return
self.authorize_redirect(redirect_uri=my_url,client_id=self.settings["facebook_api_key"],extra_params={"scope": "read_stream,email,offline_access"})
def _on_auth(self, user):
@dmitric
dmitric / fun_orm.py
Last active June 21, 2018 11:16
A simple read-only ORM like functionality using python's __getattr__. Example uses tornado's torndb Connection and query
class Backend(object):
"""Allows access to backend and removes logic from handlers"""
def __init__(self):
"""Inititalize with the variables you need"""
self.__users_data = None
self.db = Connection(
options.db_host,
options.db,
user=options.db_user,
@dmitric
dmitric / mdx_video.py
Created August 19, 2013 07:40
A dead simple python markdown extension to embed youtube or vimeo videos in markdown. Using oembed is a terrible idea if you have lots of links on a page, and the other implementations I saw were sloppy. You can still use regular links to vimeo and youtube too.
# -*- coding: utf-8 -*-
from markdown import Extension
from markdown.util import etree
from markdown.inlinepatterns import Pattern
SOURCES = {
"youtube": {
"re": r'youtube\.com/watch\?\S*v=(?P<youtube>[A-Za-z0-9_&=-]+)',
"embed": "//www.youtube.com/embed/%s"
},