Skip to content

Instantly share code, notes, and snippets.

@tomotaka
tomotaka / cssdatauriexpander.py
Last active December 31, 2015 05:29
expanding url(./img/hoge.png) => url(data:image/png;....) with filesystem-based caching
import re
import base64
import hashlib
import os.path
import gevent.fileobject as gfo
def _read_file(filepath):
fh = open(filepath, 'rb')
gfh = gfo.FileObject(fh, 'rb')
ret = gfh.read()
@tomotaka
tomotaka / urlshortener.py
Created December 16, 2013 10:18
mongo-based url shorten service engine
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# requirements: pip install mongoengine simplejson bottle
import mongoengine
import string
from bottle import (
route, run, HTTPResponse, request
)
import simplejson
@tomotaka
tomotaka / seitekidouteki.md
Last active June 4, 2018 08:02
静的型言語 vs 動的型言語

静的型 vs 動的型

※個人の見解です!!

静的片付け言語のいいところ

  • コード自体の情報量が多い
    • ドキュメントの自動生成が楽(少ないドキュメンテーション作業で質の高いドキュメントが作れる)
    • エディタ/IDEの自動補完をより強力にすることができる
  • 実行するまでもなく様々なことがコンパイル時に検査される
  • 実行速度が速い(言語が多い)
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pprint import pprint, pformat
import random
def randomize(items):
randomized = []
while 0 < len(items):
idx = random.randint(0, len(items)-1)
@tomotaka
tomotaka / stream_text_processor.py
Last active August 29, 2015 13:56
cat test.txt | python ./stream_text_processor.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import hashlib
import gevent
import gevent.queue
import gevent.fileobject as gfo
@tomotaka
tomotaka / schwartz.java
Created June 23, 2014 07:18
schwartz.java
class Tuple {
public Object obj;
public int idx;
Tuple(Object o, int i) { this.obj = o; this.idx = i; }
}
List<Tuple> tmp = new ArrayList<Tuple>();
int i = 0;
for (Object obj : objects) {
tmp.add(new Tuple(obj, i++));
}
@tomotaka
tomotaka / concurrentcrawler.py
Last active August 29, 2015 14:13
concurrent crawler using gevent
import gevent.queue as gq
class ConcurrentCrawler(object):
def __init__(self, func=crawl_all, concurrency=10, q_max=100):
self._func = func
self._concurrency = concurrency
self._q_max = q_max
self._queue = gq.Queue(maxsize=self._q_max)
self._workers = None
@tomotaka
tomotaka / sqlitedict_vs_plyvel.py
Last active August 29, 2015 14:20
sqlitedict vs plyvel(LevelDB)
# -*- coding: utf-8 -*-
import time
import hashlib
import os
from contextlib import contextmanager
import shutil
import plyvel
from sqlitedict import SqliteDict
@tomotaka
tomotaka / calc_mean_bench.py
Last active August 29, 2015 14:26
mean calculation benchmark
import numpy as np
import random
import time
from contextlib import contextmanager
data = []
for i in xrange(300000):
data.append(random.randint(1, 100000000))
@tomotaka
tomotaka / ldbdump.py
Created September 17, 2015 07:34
json-lines dumper of leveldb
# -*- coding: utf-8 -*-
import time
# pip install click plyvel simplejson
import click
import plyvel
import simplejson as json
@click.command()