Skip to content

Instantly share code, notes, and snippets.

@whosaysni
whosaysni / JsonSerializedDict
Created March 28, 2014 13:30
Dictionary with JSON-serialized internal buffer. Placed in the Public Domain.
from json import dumps, loads
from UserDict import DictMixin
class JsonSerializedDict(DictMixin):
"""
>>> JSD = JsonSerializedDict
>>> d = JSD()
>>> print d
{}
@whosaysni
whosaysni / get_secret_key.py
Created April 17, 2014 00:05
Generating SECRET_KEY on first run
import os, binascii
def get_secret_key(key_filename):
key_path = os.path.join(BASE_DIR, 'conf', key_filename)
if not os.path.exists(key_path):
try:
umask_saved = os.umask(0277)
with open(key_path, 'wb') as key_file:
key_file.write(binascii.b2a_base64(os.urandom(64)).rstrip('='))
finally:
os.umask(umask_saved)
@whosaysni
whosaysni / form.html
Created April 17, 2014 13:11
Confirmation/submit dialog with MetroUI css
<form id="delete_form"
method="POST" action="{% url 'delete_something' %}">
{% csrf_token %}
<div>
<input type="hidden" name="delete" value="">
<button id="delete_button" type="button">Delete</button>
<script>
$('#delete_button').on('click',
form_confirmation_factory('delete_form',
'Delete', 'Existing something will be removed.', 'OK', 'Cancel'))
@whosaysni
whosaysni / mytemplate.html
Created April 17, 2014 23:21
Padding blank row to last page with Django pagination
<table>
{% for obj in page %}
<tr><td>{{ obj.blah }}</td></tr>
{% endfor %}
{% ifequal page.number page.paginator.num_pages %}
{% for padding in page.paginator.paddngs %}
<tr><td> </td></tr>
{% endfor %}
{% endifequal %}
</table>
@whosaysni
whosaysni / add_user.html
Last active August 29, 2015 14:00
Template-only MetroUI-widgets-in-Django
<div class="container padding10">
<h1>Add user</h1>
<div class="padding10">
<form method="POST" action="{% url 'user_add' %}">
{% csrf_token %}
<fieldset>
{% with field=form.is_superuser type='switch' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.is_active type='switch' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.username type='text' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.password type='password' %}{% include "share/form_field.html" %}{% endwith %}
@whosaysni
whosaysni / dbms_model.py
Last active August 29, 2015 14:00
Modelizing auxiliary DBMS using Django's multi-db framework
# coding: utf-8
"""外部DBMS
"""
import os
from functools import wraps
from json import loads
from logging import getLogger
from threading import local
@whosaysni
whosaysni / datatables.py
Last active August 29, 2015 14:01
Djangoで dataTables.js を使うためのクエリパーザ
# coding: utf-8
"""dataTables.js 用のクエリパーザ
"""
import re
class DataTablesQueryParser(object):
"""dataTables.js 用のクエリパーザ
"""
def __init__(self, request, column_map=dict(), method='POST'):
@whosaysni
whosaysni / matchcontext.py
Last active August 29, 2015 14:01
re の MatchObject を with のコンテキストにする
# coding: utf-8
class MatchContext(object):
"""
>>> import re
>>> with MatchContext(re.match(r'(.)', 'a')) as context:
... print context.groups()
('a',)
>>> import re
>>> with MatchContext(re.match(r'(.)', '')) as context:
@whosaysni
whosaysni / closable_tab.js
Last active August 29, 2015 14:01
jquery-ui のタブを閉じられるようにして、リストの要素をクリックしたときに重複を許さずタブを追加していく
// requires jquery-ui.
function closable_tab (tab_container_id, tab_config) {
// 閉じるボタン付きのタブコントロール
var tab_container = $(tab_container_id);
var tab_list = tab_container.find('ul');
// タブ追加用のハンドラ
function _add_page (evt) {
console.log(evt);
var id = tab_config.get_id(evt.currentTarget);
@whosaysni
whosaysni / blastall
Last active August 29, 2015 14:04
Mimic legacy blast's blastall to execute BLAST+ commands: Legacy blastall のふりをして BLAST+ コマンドを実行する
#!/usr/bin/env python
# coding: utf-8
"""Mimic legacy blast's blastall to execute BLAST+ commands
"""
import argparse, os, sys
def main():
sys.stderr.write('>>>>> %s\n' %(' '.join(sys.argv)))
parser = argparse.ArgumentParser()
parser.add_argument('-p', dest='program', required=True)