This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import pstats | |
def func(): | |
"""write target function here""" | |
pass | |
def prof(func, name): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
digits :: Int -> Int | |
digits = length . show | |
square :: Num a => a -> a | |
square = (^ 2) | |
ultimate :: Int -> String | |
ultimate 42 = "ultimate" | |
ultimate n = show n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import requests | |
API_BASE_URL = 'https://slack.com/api/{api}' | |
if __name__ == '__main__': | |
api = 'chat.postMessage' | |
url = API_BASE_URL.format(api=api) | |
params = { | |
'token': 'xxxx-xxxx-xxx', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ABC | |
# DEF | |
# GHI | |
>>> rows = ['ABC', 'DEF', 'GHI'] | |
>>> cols = [''.join(x) for x in zip(*rows)] | |
>>> cols | |
['ADG', 'BEH', 'CFI'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from datetime import datetime, date | |
# datetime.today() returns datetime not only date | |
>>> datetime.today() | |
datetime.datetime(2015, 6, 21, 15, 19, 33, 753232) | |
>>> datetime.now() | |
datetime.datetime(2015, 6, 21, 15, 19, 37, 753692) | |
# to get today's date only | |
>>> datetime.today().date() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dateutil.relativedelta import relativedelta | |
from datetime import datetime | |
today = datetime.today() | |
today.strftime('%Y-%m-%d') | |
# '2015-06-14' | |
next_week = today + relativedelta(weeks=1) | |
next_week.strftime('%Y-%m-%d') | |
# '2015-06-21' | |
next_month = today + relativedelta(months=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj = Foo.objects.get(pk="foo") | |
obj.pk = "bar" | |
obj.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
response = requests.post('http://google.com', data={'foo':'test', 'bar':'test'}) | |
print(response.request) | |
# <PreparedRequest [POST]> | |
raw_params = response.request.body | |
print(raw_params) | |
# foo=test&bar=test # raw post parameters(not dict) | |
from urlparse import parse_qs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from string import ascii_lowercase, ascii_uppercase | |
import random | |
def iter_password(length=8, num_of_numbers=2, num_of_uppers=2): | |
numbers = list(map(str, range(0, 10))) | |
uppers = list(ascii_uppercase) | |
lowers = list(ascii_lowercase) | |
num_of_lowers = length - num_of_numbers - num_of_uppers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import inspect | |
def multiply(func): | |
def _multiply(*args, **kwargs): | |
if inspect.getargspec(func).args[0] == 'self': | |
# 'if inspect.ismethod(func):' doesn't work in this situation, | |
# because when decorating the method isn't bounded to its class yet. | |
return func(*args, **kwargs) * (kwargs['num'] if 'num' in kwargs else args[1]) |
NewerOlder