Thursday, August 31, 2017

Django and more Python

I am interviewing for a Python developer position. I always find my knowledge gap or shortage. I will not stop learning.

Django

With Django, you can take Web applications from concept to launch in a matter of hours.
part 1
source activate py3
conda install django
python -m django —version    # 1.11.4
django-admin startproject mysite    # initailize a project by creating manage.py and mysite folder with starting codes
cd mysite
python manage.py runserver    # create db.sqlite3
http://127.0.0.1:8000/
python manage.py startapp polls  # create polls folder
cd polls
# change text in vews.py
touch urls.py  # create file and change text
cd ..
touch urls.py # create file and change text
part 2
python manage.py migrate  # install apps
cd polls
subl models.py  # add models
cd ../mysite
subl setting.py # add 'polls.apps.PollsConfig' to apps
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate
python manage.py shell
# play with API
python manage.py createsuperuser
Username: admin
password: j*89*****
cd polls
subl admin.py # make poll app modifiable in the admin

Difference between function and generator

star sign before a variable name?

def foo(x,*y, **z):
  print(x)
  print(y)
  print(z)
foo(1,2,3, a=4,b=5)
lis = [2,3]
dic = {'a':4, 'b':5}
foo(1,*lis,**dic)
Inside a function header:
* collects all the positional arguments in a tuple
** collects all the keyword arguments in a dictionary
In a function call:
* unpacks an list or tuple into position arguments
** unpacks an dictionary into keyword arguments

extract lines begin with “error: “

result = [l for l in open(“input.txt”) if l.startswith(“error: “)]

try/execept handling

x = 1
y = 0
z = 0
result = 2
try:
    z= z+1  # 1
    result = x/y
except ZeroDivisionError:
    z = z + result #3
else:
    z= z+1  # if no error occurs
finally:
    z = z+1 #4
z = z+1  #5
print(z)

try:
    fuck
except NameError as e: # or Exception
    print("watch your language")
finally:
    print("go home")

sort

a = [-1,-4,2]
a.sort()  # inplace 
sorted(a, key = abs)

separate string into partitioned tuple

"hello".partition("e")

Python generator vs python function

generator save state at yield, so the result will keep going for next call unless it is exhausted.
import math
def get_primes(number):
    while True:
        if is_prime(number):
            yield number
        number += 1

def is_prime(number):
    if number > 1:
        if number == 2:
            return True
        if number % 2 == 0:
            return False
        for current in range(3, int(math.sqrt(number) + 1), 2):
            #print(current)
            if number % current == 0: 
                return False
        return True
    return False

a = get_primes(2)
a.__next__()

split vs rsplit

rsplit is from the right

class method

class vehicles:
    count = 0
    def __init__(self,value):
        self.value = value
        vehicles.count +=1
    def getval(self):
        return self.value
    def getcount(cls):
        return vehicles.count
    counter = classmethod(getcount)

type1 = vehicles("car")
type2 = vehicles("Bus")
type3 = vehicles("bike")

print(type1.getval(),type2.getval(),vehicles.counter(),type2.getcount()) 
# car Bus 3 3

function decorator

Decorator is a handy way to wrap a function so it temporaly becomes an embedded function.
class Foo():
    @classmethod
    def class_foo(cls):
        print("class: %s" % cls)
Foo.class_foo()   
# equals to
class Foo():
    def class_foo(cls):
        print("class: %s" % cls)
    class_foo = classmethod(class_foo)    
Foo.class_foo()
another example
def my_decorator(some_function):
    def wrapper():
          print("before some_function() is called.")
        some_function()
        print("after some_function() is called.")
    return wrapper

@my_decorator
def just_some_function():
    print("Wheee!")
just_some_function()

zip()

The syntax of zip() is:
zip(*iterables)
example
coordinate = ['x', 'y', 'z']
value = [3, 4, 5, 0, 9]

result = zip(coordinate, value)
resultList = list(result)
print(resultList)

c, v =  zip(*resultList)
print('c =', c)
print('v =', v)

unicode

unicode

The default encoding for python 2 is ASCII, for python 3 is UTF-8, Unicode Transformation Format 8 bit.
for i in range(256):
    if chr(i)== 'A' or chr(i) == 'a' or chr(i)=='0':
        print("attentions!")
    print(i,":",chr(i),end=" ")
chr() and ord() is for ASCII decoding and encoding.
so char ‘0’-‘9’ are encoded as 48-57; char ‘A’-‘Z’ : 65-90, char ‘a’-‘z’: 97-122. Actually, the first 0-127 characters (7 bits) are the same with ASCII defined in 1968, which is Amercian Standard.
UTF-8 is elastic encoding, that each character is represented by 1 to 4 bytes, depending on its belonging. https://en.wikipedia.org/wiki/UTF-8#Description. It is capable of encoding 1,112,064 valid code points in Unicode. (17*2^16-2048, or 0x10FFFF)

中文编码

标准中文电码(Chinese Telegraphic/Commercial code): (http://code.usvisa-application.com/) is 4 digits, from 0000 to 9999.
中国国家标准:
GB2312: in 1981, use 2 bytes, 1st byte(0xA1-0xFE), 2nd byte (0xA1-0xFE), so 83x93= 8000, enough for 3755+3008 = 6763 characters.
GBK/GB13030: use 2 bytes, 2nd byte can be (0x00-0xFF), so 86x256 = 22016
Unicode 10.0: example link , has 87, 882 CJK Unified Ideographs, out of totally 136,755 characters. In unicode 1.01, out of total 28,359 characters, 20,914 is occupied by CJK Ideographs.
Unicode has 17 planes, each plan is 2 bytes, or 2^16= 65,536 coding points.
It requires 3 k characters for general literacy, but 40 k characters for reasoanably complete coverage.
Source separate principle. The term ideograph may be misleading because Chinese script is not strictly a picuture writing system.
For a freqently-used Chinese Character, Unicode uses 2 bytes, UTF-8 uses 3 bytes.

bytes vs string

Bytes are bytes; characters are an abstraction. An immutable sequence of Unicode is called a string. An immutable sequence of numbers (0-256) is called a bytes objects. Each byte within the byte literal can be an ascii character or an encoded hexadecimal number from \x00 to \xff (0–255).
In python 3, string is default unicoded, so no need to use u'literal'.
You can’t mix bytes and strings. (TypeError)
# http://graphemica.com/unicode/characters/page/80
u"\u0041"  # 'A'
u"\u4dff"  # '䷿'
u"\u4E00"  # '一'

b = b'abc' # bytes, immutable
barr = betearray(b) # bytearray, mutable
barr[0] = 102 #  = \x66 = f
barr  # bytearray(b'fbc')

# try different encoding
'一'.encode('raw_unicode_escape')  # b'\\u4e00'
u'哈'.encode('utf8')  # b'\xe5\x93\x88'
u'哈'.encode('gbk')   # b'\xb9\xfe'
u'哈'.encode('big5')  # b'\xab\xa2'