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)…
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] ==…
Python: Multiple Assignment by Generator Comprehensions
Generator comprehensions return generator in single assignment variable, but in multiple assignment it doesn’t return generators. # generator comprehension gen = ((x for x in [1, 2, 3])) gen >><generator object <genexpr> at 0x7f92b4e33500> # returns not generator but each…
Python: List Comprehensions are better
Using List Comprehensions, we don’t have to write ‘lamda’ expressions to map or filter values and the code is simpler. dct_lst = [{‘key1’: 1, ‘key2’: 2}, {‘key1’: 2, ‘key2’: 3}] # using list comprehension result = [x[‘key2’] * 10 for x…
Python: format()
In Python3, using format() for string formatting is standard instead using traditional “%” formatting. ‘FOO{0}BAR’.format(10) >>’FOO10BAR’ # parameters can be indexed or named ‘FOO{0}{1}{2}BAR’.format(‘a’, ‘b’, ‘c’) >>’FOOabcBAR’ ‘FOO{a}{b}{c}BAR’.format(a=1, b=2, c=3) >>’FOO123BAR’ # with separater ‘FOO{0:,d}BAR’.format(1000000) >>’FOO1,000,000BAR’ https://docs.python.org/2.7/library/stdtypes.html#str.format >This method of…
Python: Clear list
L1 = [1, 2, 3] L2 = L1 print(L1) >>[1, 2, 3] print(L2) >>[1, 2, 3] L1 = [] # create new empty list, so L2 values still exist print(L1) >>[] print(L2) >>[1, 2, 3] # not cleared L1 =…
Python: tempfile read
Before reading tempfile contents, execute “seek(0)” is needed to point beginning of the file. http://stackoverflow.com/questions/1202848/python-tempfile-temporaryfile-cannot-be-read-why # -*- coding:utf-8 -*- from tempfile import NamedTemporaryFile from os import SEEK_END with NamedTemporaryFile() as f: #before seek(0) f.read() returns nothing f.write(‘Foo’) print f.read() >>…
Python: String encode
string encode from ‘utf-8’ to ‘cp932’ avoiding “UnicodeDecodeError” # -*- coding: utf-8 -*- ENCODE_TO = ‘cp932’ L = [‘a’, ‘漢字’, u’漢字’, 1, None] for c in L: if isinstance(c, unicode): print c.encode(ENCODE_TO) elif isinstance(c, str): # when element is str,…