Skip to content

Instantly share code, notes, and snippets.

@kwatch
kwatch / gist:900037
Created April 3, 2011 00:12
social buttons for Hiki template (hiki/template/view.html)
<% unless @conf.base_url =~ /\.RubiMa/ %>
<!-- social-buttons -->
<div class="social-buttons" style="text-align:right">
<!-- hatena bookmark button -->
<a href="http://b.hatena.ne.jp/entry/<%= @conf.base_url.sub(/^https?:\/\//, '') %>?<%= @conf.options['page'] %>" class="hatena-bookmark-button" title="Add this entry to Hatena Bookmark"><img src="http://b.st-hatena.com/images/entry-button/button-only.gif" alt="Add this entry to Hatena Bookmark" width="20" height="20" style="border: none;" /></a>
<script type="text/javascript" src="http://b.st-hatena.com/js/bookmark_button.js" charset="utf-8"></script>
<!-- delicious bookmark button -->
<a href="http://delicious.com/save" onclick="window.open('http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;"><img src="http://static.delicious.com/img/delicious.gif" alt="Delicious" style="border:none" /></a
@kwatch
kwatch / python-3.2-whatsnew-pep3333.rst
Created April 4, 2011 23:06
Python 3.2 What's New での、PEP3333 セクションの日本語訳

PEP 3333: Python Web Server Gateway Interface v1.0.1

This informational PEP clarifies how bytes/text issues are to be handled by the WGSI protocol. The challenge is that string handling in Python 3 is most conveniently handled with the :class:`str` type even though the HTTP protocol is itself bytes oriented.

この情報提供のための PEP は、WSGI プロトコルにおいてバイト/テキストの問題がどう扱われるべきかを明確にする。

@kwatch
kwatch / whats-new-python320.ja.rst
Created April 6, 2011 23:24
What's New in Python 3.2 translation to Japanese

What's New In Python 3.2

Author: Raymond Hettinger
Release:|release|
Date: |today|
@kwatch
kwatch / gist:1088503
Created July 18, 2011 03:42
simulate named_scope by higher-order function + partial application in Python
##
## named_scope example in Rails2
## (http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality)
##
class User < ActiveRecord::Base
named_scope :active, :conditions => {:active => true}
named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end
# same as User.find(:all, :conditions => {:active => true})
@kwatch
kwatch / gist:1319756
Created October 27, 2011 14:51
Example to instanciate and manipulate recipe object in pyKook 0.6.0
## define generic recipe
@recipe('*.cmo', ['$(1).ml', '$(1).cmi'])
def file_cmo(c):
system(...)
## create specific recipe object for 'a.cmo' from generic recipe
a_cmo_recipe = kookbook['a.cmo']
## manipulate recipe object, for example append dependency
a_cmo_recipe.ingreds.append('b.cmi')
@view_config(route_name='user_new', request_method='GET', renderer='form.html')
def user_new(request):
# ...
@view_config(route_name='user_create', request_method='POST', renderer='form.html')
def user_crate(request):
# ...
...
@kwatch
kwatch / gist:2342562
Created April 9, 2012 09:43
sqlalchemy.exc.InvalidRequestError: Table 'admin_users' is already defined for this MetaData instance.
$ pserve development.ini --reload
Starting subprocess with file monitor
/Users/kwatch/myproj1/local/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.4-x86_64.egg/sqlalchemy/ext/declarative.py:1336: SAWarning: The classname 'AdminUser' is already in the registry of this declarative base, mapped to <class 'smapo.common.models.base.AdminUser'>
_as_declarative(cls, classname, cls.__dict__)
Traceback (most recent call last):
File "/Users/kwatch/myproj1/local/bin/python2.7/pserve", line 9, in <module>
load_entry_point('pyramid==1.3b2', 'console_scripts', 'pserve')()
File "/Users/kwatch/myproj1/local/lib/python2.7/site-packages/pyramid-1.3b2-py2.7.egg/pyramid/scripts/pserve.py", line 47, in main
return command.run()
File "/Users/kwatch/myproj1/local/lib/python2.7/site-packages/pyramid-1.3b2-py2.7.egg/pyramid/scripts/pserve.py", line 290, in run
@kwatch
kwatch / exception_summary.dbtmako
Created April 10, 2012 10:06
Alternative template file for pyramid_debugtoolbar to open errored file quickly
# -*- coding: utf-8 -*-
<%!
##
## This is an alternative template file for pyramid_debugtoolbar.
## Using this template, you can open errored file very quickly.
##
##
## Usage:
##
@kwatch
kwatch / gist:2585260
Created May 3, 2012 12:13
simulate multi-indeces with tuple, without paren
##
## simulate multi-indeces with tuple, without paren
##
class Foo(object):
def __getitem__(self, index):
return index
if __name__ == '__main__':
@kwatch
kwatch / gist:2722877
Created May 18, 2012 02:53
setdefault() for any object
def setdefault(obj, name, value):
if hasattr(obj, name):
return getattr(obj, name)
else:
setattr(obj, name, value)
return value