def checkio(rows): cols = [”.join([rows[y][x] for y in range(3)]) for x in range(3)] digs = [”.join([rows[x][x] for x in range(3)]), ”.join([rows[x][2 – x] for x in range(3)])] triples = rows + cols + digs for check in triples: winner =…
CheckIO: The Most Wanted Letter
from string import ascii_lowercase def checkio(text): letters = [x for x in text.lower() if x in ascii_lowercase] counts = sorted({x : letters.count(x) for x in letters}.items(), key=lambda x:x[1], reverse=True) wanted_count = counts[0][1] wanted = sorted([x[0] for x in counts if…
Python: datetime.today()
datetime.today() does’t return today’s date only but today’s datetime. And using datetime.combine, date object can be converted to datetime. >>> from datetime import datetime, date # datetime.today() returns datetime not only date >>> datetime.today() datetime.datetime(2015, 6, 21, 15, 19, 33,…
Python: 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) next_month.strftime(‘%Y-%m-%d’) # ‘2015-07-14’ from calendar import monthrange lastday_of_month = monthrange(2015, 6)[1] lastday_of_month #…
Django: copy model instance
To copy model instance, replacing pk value is the easiest way. obj = Foo.objects.get(pk=”foo”) obj.pk = “bar” obj.save() http://stackoverflow.com/questions/4733609/how-do-i-clone-a-django-model-instance-object-and-save-it-to-the-database
Python: get post parameters from response object (requests)
In requests, response object has PreparedRequest object and it only contains ‘raw’ post request parameters (not as dict). 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)…
Linux: File password protection
Using gpg # compress and encrypt gpg -c filename >> Enter password # filename.gpg is encrypted file # decrypt gpg -d filename.gpg http://superuser.com/questions/162624/how-to-password-protect-gzip-files-on-the-command-line
Python: password generator
Password generator for myself(works in python3). # -*- coding:utf-8 -*- from string import ascii_lowercase, ascii_uppercase, digits import random def iter_password(length=8, num_of_numbers=2, num_of_uppers=2): numbers = list(digits) uppers = list(ascii_uppercase) lowers = list(ascii_lowercase) num_of_lowers = length – num_of_numbers – num_of_uppers while True:…
Python: General decorator
General decorator, that can be used to class method and function (keyword args are also permitted) by checking whether args[0] is ‘self’ or not and kwargs. # -*- coding:utf-8 -*- import inspect def multiply(func): def _multiply(*args, **kwargs): if inspect.getargspec(func).args[0] ==…
Django: csrf_exempt of class based view
To suppress csrf verification of class based view, @csrf_exempt decorator in veiws.py doesn’t work in Django 1.4 . http://stackoverflow.com/questions/10252238/csrf-exempt-stopped-working-in-django-1-4#comment13230749_10252521 Instead, decorating the as_view function in urls.py works fine! from django.views.decorators.csrf import csrf_exempt urlpatterns = patterns(”, url(r’^$’, csrf_exempt(views.IndexView.as_view()), name=’index’),