Skip to content

Instantly share code, notes, and snippets.

View tkmru's full-sized avatar
💭
🍣 💰 🍖 🍶

@tkmru tkmru

💭
🍣 💰 🍖 🍶
View GitHub Profile
@tkmru
tkmru / tuple.py
Last active December 17, 2015 00:28
突然のtuple
>>>1,
(1,)
>>>1,2
(1,2)
@tkmru
tkmru / extract_text.py
Created July 19, 2013 19:40
This program extract text from html file.
# coding: UTF-8
import re
def extract_text(html):
#First, I deal with tags.
cleaned = re.sub(r"<[^>]*?>", "", html)
cleaned = re.sub(r"(?s)<!--(.*?)-->[\n]?", "", cleaned)
cleaned = re.sub(r"(?s)<.*?>", "", cleaned)
#Finally, I deal with special chars.
@tkmru
tkmru / 2list_to_dict.py
Created August 6, 2013 18:16
It make dict from two list. Using zip() become easy.
# coding: UTF-8
keys = ['one', 'two', 'three']
values = [1, 2, 3]
dict(zip(keys, values)) #return {'three': 3, 'two': 2, 'one': 1}
@tkmru
tkmru / convert_time.py
Created August 6, 2013 18:52
It make datetime object from struct_time.
def convert_time(struct_time): #struct_time to datetime object
return datetime.datetime(*struct_time[:6])
@tkmru
tkmru / expand_url.py
Created December 11, 2013 00:07
This code expand short URL.
# coding: UTF-8
import sys
import urllib2
def expandURL(shortURL):
url = urllib2.urlopen(shortURL).geturl()
return url
@tkmru
tkmru / xytoyx.py
Created December 15, 2013 19:08
x and y exchange each other.
x, y = y, x
@tkmru
tkmru / tryandelse.py
Created December 15, 2013 19:32
if "try sentence" use "else", code detect error strictly.
# bad case
try:
do_something()
log.debug(u"complete") # if error raise here, code can't detect error.
except:
log.debug(u"error")
# good case
try:
do_something()
@tkmru
tkmru / fromkeys.py
Created December 15, 2013 20:27
Using fromkeys, keys's list become dict object.
>>> dict.fromkeys(['A', 'B', 'C'], True)
{'A': True, 'C': True, 'B': True}
>>> dict.fromkeys(['A', 'B', 'C'])
{'A': None, 'C': None, 'B': None}
@tkmru
tkmru / setdefault.py
Created December 15, 2013 20:39
dict.setdefault(key, default) is if key in dict: return dict[key] else: dict[key] = default return default
def group_by(dictionary):
d = {}
for k, v in dictionary.iteritems():
d.setdefault(k, []).append(v)
return d
@tkmru
tkmru / modulepass.py
Created December 15, 2013 21:37
How to find pass of module.
>>> import re
>>> re.__file__
'/usr/lib/python2.7/re.pyc'