Skip to content

Instantly share code, notes, and snippets.

@aminami1127
aminami1127 / profile.py
Created April 18, 2016 12:45
profile.py
import cProfile
import pstats
def func():
"""write target function here"""
pass
def prof(func, name):
digits :: Int -> Int
digits = length . show
square :: Num a => a -> a
square = (^ 2)
ultimate :: Int -> String
ultimate 42 = "ultimate"
ultimate n = show n
@aminami1127
aminami1127 / slack_post.py
Last active August 29, 2015 14:24
slack api post message
# 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',
@aminami1127
aminami1127 / row_col.py
Created June 21, 2015 14:15
Exchange rows and columns
# ABC
# DEF
# GHI
>>> rows = ['ABC', 'DEF', 'GHI']
>>> cols = [''.join(x) for x in zip(*rows)]
>>> cols
['ADG', 'BEH', 'CFI']
@aminami1127
aminami1127 / datetime_today.py
Created June 21, 2015 06:35
datetime.today() does't return date object
>>> 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()
@aminami1127
aminami1127 / weeks_month.py
Created June 14, 2015 03:15
Calculate weeks or months
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)
@aminami1127
aminami1127 / copy_model.py
Created June 8, 2015 22:59
Django: copy model instance
obj = Foo.objects.get(pk="foo")
obj.pk = "bar"
obj.save()
@aminami1127
aminami1127 / get_post_params.py
Last active August 29, 2015 14:20
Get post parameters from response object in requests
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
@aminami1127
aminami1127 / gen_pass.py
Last active August 29, 2015 14:19
password generator
# -*- 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
@aminami1127
aminami1127 / deco.py
Last active August 29, 2015 14:19
General decorator
# -*- 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])