Skip to content

Instantly share code, notes, and snippets.

View Answeror's full-sized avatar

Cosmo Du Answeror

View GitHub Profile
@Answeror
Answeror / dumpkanji.py
Created December 30, 2013 16:02
hanzi(chinese) to kanji(japanese) character conversion
import requests
from bs4 import BeautifulSoup as BS
import json
def gen(soup):
for tr in soup.select('tr'):
tds = tr.select('td.tdR4')
if len(tds) == 6:
yield tds[2].string, tds[3].string
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Answeror
Answeror / current_time.cpp
Created May 13, 2013 06:09
Get total seconds since 1970-01-01, millisecond precision.
#include <boost/date_time/posix_time/posix_time.hpp>
double current_time()
{
using namespace boost::gregorian;
using namespace boost::posix_time;
const boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
ptime time_t_epoch(date(1970,1,1));
time_duration diff = now - time_t_epoch;
@Answeror
Answeror / decode.py
Created October 9, 2012 01:59
Python3 try decode.
def decode(bs, encodings=['utf-8', 'cp936', 'gbk', 'gb18030', 'big5', 'latin1']):
for en in encodings:
try:
return bs.decode(en)
except:
pass
raise RuntimeError('Decoding failed.')
@Answeror
Answeror / gist:3832085
Created October 4, 2012 08:02
Activate virtualenv in blender
def activate_virtualenv(name):
"""Activate given virtualenv.
Virtualenv home folder is given by environment variable ``WORKON_HOME`` or
``~/Envs`.
"""
if 'WORKON_HOME' in os.environ:
home = os.environ['WORKON_HOME']
else:
home = os.path.expanduser(os.path.join('~', 'Envs'))
@Answeror
Answeror / gist:3775675
Created September 24, 2012 12:19
deterministic testing with numpy.random.
from numpy import random
from numpy.random import rand
class TestRandomly(object):
def setUp(self):
self.state = random.get_state()
random.seed(0)
@Answeror
Answeror / gist:3741730
Created September 18, 2012 07:10
Make two image height equal
% from http://tex.stackexchange.com/a/13039
\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Image2 ...
\sbox\IBoxA{\includegraphics[width=0.45\textwidth]{#1}}
\sbox\IBoxB{\includegraphics[width=0.45\textwidth]{#4}}%
\ifdim\ht\IBoxA>\ht\IBoxB
\setlength\IHeight{\ht\IBoxB}\else\setlength\IHeight{\ht\IBoxA}\fi%
@Answeror
Answeror / fetch.py
Created September 16, 2012 05:22
程序员的爱情表白
# fetch jpgs from tuita
import urllib3
from pyquery import PyQuery as pq
http = urllib3.PoolManager()
url = 'http://yokicharlotte.tuita.com/blogpost/23832258'
r = http.request('GET', url)
d = pq(r.data.decode('utf-8'))
imgs = d('.photo_group')('p.pic')('img')