Skip to content

Instantly share code, notes, and snippets.

View methane's full-sized avatar

Inada Naoki methane

View GitHub Profile
@methane
methane / gist:1894166
Created February 23, 2012 18:09
Redis を試した時のメモ
inada-n@valkryie:~/work/redis$ mkvirtualenv redis # 実験用の独立した環境を容易
Using real prefix '/usr'
New python executable in redis/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/inada-n/.virtualenvs/redis/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/inada-n/.virtualenvs/redis/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/inada-n/.virtualenvs/redis/bin/preactivate
virtualenvwrapper.user_scripts creating /home/inada-n/.virtualenvs/redis/bin/postactivate
virtualenvwrapper.user_scripts creating /home/inada-n/.virtualenvs/redis/bin/get_env_details
@methane
methane / gist:1970665
Created March 4, 2012 04:28
monkey_example.py
#coding: utf-8
import socket
class FooClient(object):
def __init__(self, host, port):
self._sock = socket.socket()
self._sock.connect((host, port))
def say(self):
self._sock.sendall('Foo!\n')
all:
cython --embed mltest.py
gcc -O2 mltest.c `python-config --cflags --ldflags` -fPIC -o mltest
cython --embed mltest2.pyx
gcc -O2 mltest2.c `python-config --cflags --ldflags` -fPIC -o mltest2
@methane
methane / autocompile.py
Created March 17, 2012 15:20
Compile cpp files automatically.
#!/usr/bin/env python
from watchdog import events, observers
from paver.easy import *
import subprocess
class AutoCompile(events.FileSystemEventHandler):
def compile(self, src):
if src.endswith('.cpp'):
src = path(src)
dst = src.splitext()[0]
In [1]: import msgpack
In [2]: packed = msgpack.packb([1,2,3])
In [3]: packed
Out[3]: '\x93\x01\x02\x03'
In [4]: packed *= 3
In [5]: len(packed)
@methane
methane / n_way_cache.py
Created March 21, 2012 04:26
Describing n-way cache.
NWAY = 4
LINESIZE_BITS = 4
LINESIZE = 2 ** LINESIZE_BITS # 64
CACHESIZE = 4*1024
NUMLINES = CACHESIZE / LINESIZE
LINES = [(None, None)] * NUMLINES
@methane
methane / gist:2185380
Created March 24, 2012 17:28
Tornado Example: Delegating an blocking task to a worker thread pool from an asynchronous request handler
from time import sleep
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from multiprocessing.pool import ThreadPool
_workers = ThreadPool(10)
def run_background(func, callback, args=(), kwds={}):
def _callback(result):
@methane
methane / delete_old_log.py
Created April 3, 2012 09:58
古いレコード削除プログラム
#!/usr/bin/env python
# coding: utf-8
from __future__ import print_function
import datetime as dt
import os
import sys
import time
import logging
from ConfigParser import SafeConfigParser
@methane
methane / switchbench.py
Created April 3, 2012 16:28
Measure the cost of greenlet switching.
import gevent
import sys
N = int(sys.argv[1])
M = int(sys.argv[2])
def sleeper(n):
for i in xrange(N):
#print(n)
gevent.sleep(0)
<?php
$d = file_get_contents('index.html');
//$d = htmlspecialchars($d);
$d = strtr($d, array('&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;'));
for ($i =0; $i < 10000; ++$i) {
strtr($d, array('&amp;'=>'&', '&lt;'=>'<', '&gt;'=>'>'));
}