Skip to content

Instantly share code, notes, and snippets.

@tomotaka
tomotaka / update_tag_text.js
Created July 11, 2013 09:30
jQuery update tag text
$("#tagid").text("here is new text for the tag");
@tomotaka
tomotaka / click_handler.js
Created July 11, 2013 09:31
jQuery click handler
$("#tagid").click(function(){
var someAttr = $(this).attr("data-hoge-fuga");
// do something
});
@tomotaka
tomotaka / fluent_log_s3.py
Created July 12, 2013 04:11
parser for fluentd-s3 log
#!/usr/bin/python
# -*- coding: utf-8 -*-
import simplejson
import re
import pprint
import iso8601
__all__ = ('FluentLogS3', 'parse_fluent_log_line')
_parse_pat = re.compile(r'\A(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[\-+]\d\d:\d\d)\s+([^\s]+)\s+(.*)\n\Z')
@tomotaka
tomotaka / Vagrantfile
Created August 1, 2013 08:37
vagrant digital ocean
# coding: utf-8
Vagrant.configure("2") do |config|
config.vm.hostname = "digital-ocean-test"
config.ssh.private_key_path = "~/.ssh/id_rsa"
config.vm.box = "digital_ocean"
config.vm.provider :digital_ocean do |provider|
provider.client_id = "YOUR CLIENT ID"
provider.api_key = "YOUR API KEY"
@tomotaka
tomotaka / for_hana.py
Created August 21, 2013 07:35
appendと+の違い
import pprint
print '----try not append----'
a = []
print 'object id of variable \'a\' => %d' % id(a)
print 'content of variable \'a\': %s' % pprint.pformat(a)
a = a + [1]
print 'added value 1 with + operator'
print 'object id of variable \'a\' has changed: %d' % id(a)
print 'content of variable \'a\': %s' % pprint.pformat(a)
@tomotaka
tomotaka / multi_gthread.py
Created September 4, 2013 08:32
gevent greenlet thread pool
#!/usr/bin/python
# -*- coding: utf-8 -*-
import gevent
from gevent.event import AsyncResult
from gevent.queue import Queue
__all__ = ('GExecutorNotRunningError', 'GExecutor')
class GExecutorNotRunningError(Exception):
@tomotaka
tomotaka / use_pyramid_i18n.py
Last active December 22, 2015 19:29
use pyramid i18n(localizer) out side of web framework(in my case, it's test)
from pyramid.i18n import make_localizer, TranslationString
dir = '/your/locale/dir'
directories = (dir,)
txt_want_to_translate = TranslationString('hoge_moge')
# txt_want_to_translate = TranslationString('hoge_moge', domain='some_domain')
locale = 'ja'
localizer = make_localizer(locale, directories)
transltaed_str = localizer.translate(txt_want_to_translate)
@tomotaka
tomotaka / beautiful_soup_sample.py
Created September 13, 2013 06:10
BeautifulSoup samples
#!/usr/bin/python
# -*- coding: utf-8 -*-
#from bs4 import BeautifulSoup
#soup = BeautifulSoup(some_html_string)
def get_string_by_id(soup, _tag, _id):
tag = soup.find(_tag, id=_id)
return tag.string if tag is not None else None
@tomotaka
tomotaka / test_jinja2_escape.py
Created September 13, 2013 08:09
Q. How does Jinja2 escape filter HTML escape? A. MarkupSafe.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from nose.tools import eq_
import markupsafe
jinja_escape = lambda s: '%s' % markupsafe.escape(s)
# note: lambda s: str(markupsafe.escape(s)) doesn't work in some cases
scraped_string = soup.find('p', id='important').string # should be escaped by jinja2 escape filter
@tomotaka
tomotaka / mongocounter.py
Created October 3, 2013 10:17
cache-enabled counter using MongoDB's '$inc' operator
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import math
__all__ = ('MongoCounterError', 'MongoCounter')
class MongoCounterError(Exception):
pass