In T-SQL, aggregate functions(ex. COUNT(), SUM()) return NULL value or raise Error if all values are NULL or no rows. Here’s query count rows correctly even if all column values are NULL or no rows hit. — Count rows(exclude NULL value)…
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() >>…
Git: checkout from remote branch
git fetch git checkout -b {branch_name} origin/{branch_name}
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,…
CheckIO: Count “Striped words”
I learned re.split(pattern, text) in CheckIO Count Striped words question. *Striped word is a word its alphabets sequenced vowels or consonants alternately. import string import re VOWELS = “AEIOUY” CONSONANTS = “BCDFGHJKLMNPQRSTVWXZ” DELIMITER = string.punctuation + string.whitespace def split_text(text, delimiters):…
Git: ignore file permission change
By default, git detects file permission change in workspace. To ignore this, exec the following command. git config core.filemode false
Python: format
I learned string.format() today. template = “{foo}, {bar}, {baz}” print template.format(foo=”FOO”, bar=”BAR”, baz=”BAZ”) >> FOO, BAR, BAZ
Python: True or False
I learned all python objects can be classified “True” or “False”. “”, [], {}, None are evaluated “False”. def check(data): if( filter(unicode.uppercase, data) and filter(unicode.lowercase, data) ): return True else: return False str = “aaa” print check(str) >>False str =…