Thursday, March 6, 2014

Python note 32

  2012-12-24 20:19   


===========================




Tom
 2012-12-24 20:26
$$ Class Oriented

    def __init__(self): pass
        # main() part, to use classObjName.var; this is more convenience
        # in __inti__, have to use self.var; this is more clas-oriented
    # def __new__(cls):  pass     # for inheritance of multi-generation, multi-parent
    # def __call__(self):  pass   # for mainCObj()
    # def __init__(self):  pass   # for post-instantiation of object

Tom
 2012-12-24 20:27
$$ Loading Chinese py file

    # from prs2ndu8 import *
        # prs2ndu8.py saved in utf-8
        # succeed, but require : str.decode("utf8")
        # add coding: utf-8 ;  succeed in IMPORT;  no need of decode()
    # from prs2ndu import *
        # prs2ndu.py saved in Unicode; fail in IMPORT
    # from prs2ndub import *
        # prs2ndu.py saved in Unicode big endian; fail in IMPORT
    # from prs2ndasc import *
        # prs2ndasc.py saved in ascii/ANSI endian; fail in IMPORT
        # add coding: cp936/gbk ;  fail in IMPORT
        # add coding: utf-8 ;  succeed in IMPORT

Tom
 2012-12-24 20:27
$$ List

    pop([id]); remove(value);
    append(value); extend(list); insert(id, value);
    index(value); count(value); reverse(); sort();
    del list[:]/all; del [pos:stop]

    tuple
    a = (1,2), (3,4)  >>> a == ((1,2), (3,4))
    a = 1,  >>> a == (1,)
    a,b = (1,2)  >>> a==1;  b==2
    a,b = 1,2  >>> a==1;  b==2
    (a,b) = (1,2)  >>> a==1;  b==2

    list("abc")  >>> ["a","b","c"]
    tuple("abc")  >>> ("a","b","c")

Tom
 2012-12-24 20:29
""" Functions to use files.

    f.open ("path/filename","type")
        The first argument is a string containing the filename.
        The second argument is another string containing a few characters
        describing the way in which the file will be used.

        mode can be 'r' when the file will only be read,
        'w' for only writing (an existing file with the same name will be erased),
        and 'a' opens the file for appending;
        any data written to the file is automatically added to the end.
        'r+' opens the file for both reading and writing. The mode argument is optional;
        'r' will be assumed if it’s omitted.

        On Windows, 'b' appended to the mode opens the file in binary mode,
        so there are also modes like 'rb', 'wb', and 'r+b'.

    f.read()
        # 'This is the entire file.\n'
    f.readline()
        # read one line at a time; 
        # then 'Second line of the file\n'

    f.readlines (intByte)
        the same as read(), but parse into lines; 
        If given an optional parameter intByte, it reads that many bytes from the file and enough more to complete a line,
    f.xreadlines()
        # the same as readlines() to read the entire file,
        # readlines() returns a list, xreadlines returns a iterator

    for line in f
         the best way to read a line;
         range() returns an iterator object; 
         iterator, a generator-like tuple, one same space for all elements, next() required
    """

Tom
 2012-12-24 20:33
$$ A. List Comprehesion

    x=[]; temp2=[x.extend(list(db2[y][3:9])) for y in range(len(db2))]
    [db2[x][y] for x in range(len(db2)) for y in range(3,9) ]
        # two ways, To get all numbers into one-level list

    x=[db2[y][3] for y into range(len(db2))]
        # To get all 3rd numbers in one-level list    

    for x,y,z in ((1,2,3),(4,5,6)): print x,y,z
    for x,(y,z) in [(1,(2,3)),(4,(5,6))]: print x,y,z;
        # multiple variables in FOR statement

    print "%6.4f" % (1.0*st.count(st[0])/len(st))
        # calculate the percentage of a number
    map(lambda x: 1.0*st.count(x)/len(st), [db2[0][y] for y in range(3,9)])
        # list of percentage for a row of numbers

Tom
 2012-12-24 20:33
$$ B. Iteraables

    iter(SEQUENCE)
        # sequence: list, tuple, string;
        # generate an iterator object, (S[0], S[1], ...)
    enumerate(LIST)
        # generate an iterator object, ((0, L[0]), (1, L[1]), ...)

    ITERABLES, must work with IT.next() to get next element; 
        if sequence changed before next(), then iter() changed as well;

    reduce(f(x,y), [1, 2, 3, 4])    # f(f(f(1,2),3),4)
    reduce(f(x,y), [1, 2, 3, 4], 9)    # f(f(f(f(9,1),2),3),4)
    map(f(x), [1, 2, 3, 4, 5])    # [f(1), f(2), f(3)]; map(func,iterable)
    zip("1,2,3"£¬¡±a", "b")    # ("1a", "2b"); zip(iterable, ...)

Tom
 2012-12-24 20:33
$$ C. String Formatting

    str(OBJ/NUM/STRING/SEQUENCE)    # return a string
    repr(x)    # return a string for interpretation
               # replaced into repr MODULE
               # Python3 rename to reprlib MODULE
    STRING.rjust()
    STRING.center()
    STRING.ljust()
    STRING.zfill(NUM)    # fill zero

    STRING.format(value1, value2,...)    # work with {}
    'The value of PI is approximately {}.'.format(math.pi)
    '{0} and {1}'.format('spam', 'eggs')    # with ordered values
    'This {food} is {adjective}.'.format(\
    food='spam', adjective='absolutely horrible')    # with named values
    'The story of {0}, {1}, and {other}.'.\
    format('Bill', 'Manfred', other='Georg')   # with mixed
    'PI is {!r}.'.format(math.pi)    # apply repr()
    'PI is {!s}.'.format(math.pi)    # apply str()
    'PI is {0:.3f}.'.format(math.pi)    # convert to float with 3 decimals
    'PI is {0:10d}.'.format(math.pi)    # convert to integer with 10 digits long

    table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
    print ('Jack: {0[Jack]:d}; Sjoerd: \
    {0[Sjoerd]:d}; Dcab: {0[Dcab]:d}'.format(table))    # with DICTIONARY
    coord = (3, 5)
    print 'X: {0[0]};  Y: {0[1]}'.format(coord)    # with LIST

    '{:<30}'.format('left aligned')   # with length of 30 characters
    '{:>30}'.format('right aligned')
    '{:^30}'.format('centered')
    '{:*^30}'.format('centered')  # use '*' as a fill char

    '{:+f}; {:+f}'.format(3.14, -3.14)  # show PLUS and MINUS
    '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers
    '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the MINUS -- same as '{:f}; {:f}'

    >>> '{:,}'.format(1234567890)    # add thousand separator
    >>> 'Correct answers: {:.2%}'.format(19.5/22)    # percentage
    >>> import datetime
    >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
    >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)    # with date MODULE

    for align, text in zip('<^>', ['left', 'center', 'right']):\
    '{0:{fillx}{alignx}16}'.format(text, fillx=align, alignx=align)    # nested
    >>> octets = [192, 168, 0, 1]
    >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)    # convert to octets
    'C0A80001'

    >>> from string import Template
    >>> s = Template('$who likes $what')
    >>> s.substitute(who='tim', what='kung pao')
    'tim likes kung pao'

Tom
 2012-12-24 20:34
$$ E. Input and Output

    s = raw_input('--> ')
    print s

    >>> f.write('This is a test\n')

    >>> f.read()    # read the whole file
    >>> f.readline()    # read one line
    >>> f.readlines()    # read the whole file, and split into lines
    ['This is the first line of the file.\n', 'Second line of the file\n']

    >>> f = open('/tmp/workfile', 'w')
    >>> print f
    <open file '/tmp/workfile', mode 'w' at 80a0960>

Tom
 2012-12-24 20:34
$$ F. File Functions

    FILE.open (FILEname, FILEtype)
        # file name can include PATH
        # file type: "w", "r", "a"/append, "+r"/both
    FILE.close()
    FILE.name    # file name
    FILE.fileno   # integer file id in OS
    FILE.next()    # next line
    FILE.tell ()    # current position, in byte order
    FILE.seek (OFFset, /FROM/)    # from: 0/begin of file, 1/current, 0/eof of file
    FILE.read (/NUMbytes/)
    FILE.readline (/NUMbytes/)
    FILE.readlines (/NUMbytes/)
    FILE.write £¨STRING)
    FILE.writelines (SEQUENCE)

Tom
 2012-12-24 20:34
$$ G. String Functions

    STRING.strip(/'CHARstringTOdelet'/)
        # delete chars from beginning and ending until non desired
        # deafult is to delete whitespace
    STRING.lstrip(/'CHARstringTOdelet'/)    # delete from beginning
    STRING.rstrip(/'CHARstringTOdelet'/)    # delete from ending

    STRING.split (/DELIMETERstring/,/ISaddLINEfeed/)    # return a list
    STRING.splitlines (/ISappendLINEfeed/)
    STIRNGconnector.join (SEQUENCE)

    from string import maketrans   
    intab = "aeiou"
    outtab = "12345"
    trantab = maketrans(intab, outtab)    # a>>1, e>>2,...
    str = "this is string example....wow!!!";
    print str.translate(trantab, /CHARstringTOdelete/);

Tom
 2012-12-24 20:34
$$ H. Regular Expression

    >>> import re
    >>> m = re.search('(?<=abc)def', 'abcdef')    # (?<=xx)yy if yy preceded by xx
    >>> m.group(0)
    'def'

    >>> m = re.search('(?<=-)\w+', 'spam-egg')
    >>> m.group(0)
    'egg'

    prog = re.compile(pattern)    # compile to re object
    result = prog.match(string)

    result = re.match(pattern, string)

    re.I, re.IGNORECASE; re.M, re.MULTILINE;
    re.compile(pattern, flags=0)
    re.search(pattern, string, flags=0)
    re.match(pattern, string, flags=0)
    re.split(pattern, string, maxsplit=0, flags=0)
    str=re.sub(pattern, repl, string, count=0, flags=0)    # replace
    tuple=re.subn(pattern, repl, string, count=0, flags=0)    # (str3, numREPL)

    >>> re.split('\W+', 'Words, words, words.')
    ['Words', 'words', 'words', '']
    >>> re.split('(\W+)', 'Words, words, words.')
    ['Words', ', ', 'words', ', ', 'words', '.', '']
    >>> re.split('\W+', 'Words, words, words.', 1)
    ['Words', 'words, words.']
    >>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
    ['0', '3', '9']

    search(string[, pos[, endpos]])
    match(string[, pos[, endpos]])
    split(string, maxsplit=0)
    findall(string[, pos[, endpos]])
    finditer(string[, pos[, endpos]])
    sub(repl, string, count=0)
    subn(repl, string, count=0)

    >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
    >>> m.group(0)       # The entire match
    'Isaac Newton'
    >>> m.group(1)       # The first parenthesized subgroup.
    'Isaac'
    >>> m.group(2)       # The second parenthesized subgroup.
    'Newton'
    >>> m.group(1, 2)    # Multiple arguments give us a tuple.
    ('Isaac', 'Newton')

    >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
    >>> m.group('first_name')
    'Malcolm'
    >>> m.group('last_name')
    'Reynolds'
    >>> m.group(1)
    'Malcolm'
    >>> m.group(2)
    'Reynolds'

    >>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
    >>> m.group(1)                        # Returns only the last match.
    'c3'

    >>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
    >>> m.groups()
    ('24', '1632')

Tom
 2012-12-24 20:35
$$ J. Regular Expression in JavaScript

    * Javascript

    # \.X    \b boudary, \B non boundary;
    #       \w [A-Za-z0-9_], \W non letter;  \d digit [0-9], \D non digit;
    #       \s whitespace, \S non whitespace;
    #       \. ., \" ", \$, \^;       
    #       \n line feed, \r c return; \t h-tab, \v v-tab; \f form feed;
    #       \cX eg. \cM control-M; \0 NULL;
    #       \.xHH, \uHHHH hex code; 
          
    pos    ^ beginning, ^a;  $ ending, z$
    num    a* 0 or more 'a's;  a+ 1 or more 'a's;  a? 0 or 1 'a';  
    .      one of any digit or char
    (xx)    match xx and store to groups
    (?=x)    match but not store
    x(?=y)    match aslo if followed by y
    x(?!y)    match also if not followed by y

    x|y    x or y
    {n}    a{2} 2 a's, aa;  a{1,3} a, aa, aaa;
    [x]    [xyz] x or y or z;  [^xyz] no x or y or z;    [abcd] == [a-d]
    [\b]    backspace

    exec()    re=/d(b)/g; list=re.exec(str);;  list=/d(b)/g.exec(str);
            # list, list.index, list.input, list[0] /macthed substr
            # list[1], list[2]... stored 1, 2...
            # re.lastIndex, re.source /re string;
            # re.ignoreCase, re.global / all-repeat, re.multiline; 
    test()    re.test(subSTR)
    match()    list=str.match(regEXP); 
    search()    int=str.search(regEXP); 
    replace()    str3=str2.replace(/(\w+)\s(\w+)/, "$2, $1")    # with (xx)'s
                 # str3=str2.replace(substr,newsub, flgs)
    split()    list=str.split(separator, /numlist/)

Tom
 2012-12-24 20:35
$$ Dictionary
        
        # Unlike sequences, which are indexed by a range of numbers,
        # dictionaries are indexed by keys, which can be any immutable type;
        # # strings and numbers can always be keys.
        # Tuples can be used as keys if they contain only strings, numbers, or tuples;
        # # if a tuple contains any mutable object either directly or indirectly,
        # it cannot be used as a key.

        dict = {}    # empty
        dict[KEY] = VALUE    # add an item
        del dict[KEY]    # delete an item
        dict.items()
        dict.keys()
        dict.values()
        dict.get(KEY, RETURNvalueIFfail)    # get a value
        for key in dict:    # extract keys only
        for key, value in dict.items():
        KEY in dict    # same as:  KEY in dict.keys()
        VALUE in dict.values()
        dict [KEY].append(VALUE)
        dict(LIST)
        dict(sorted(DICT.items(),key lambda x=x[1])    # sorted by value
        dict(sorted(DICT.items()))    # sorted by key

        >>> tel = {'jack': 4098, 'sape': 4139}
        >>> tel['guido'] = 4127
        >>> tel
        {'sape': 4139, 'guido': 4127, 'jack': 4098}

        >>> tel['jack']
        4098
        >>> del tel['sape']
        >>> tel['irv'] = 4127
        >>> tel
        {'guido': 4127, 'irv': 4127, 'jack': 4098}
        >>> tel.keys()
        ['guido', 'irv', 'jack']

        >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
        {'sape': 4139, 'jack': 4098, 'guido': 4127}
        >>> dict(sape=4139, guido=4127, jack=4098)
        {'sape': 4139, 'jack': 4098, 'guido': 4127}

        >>> for i, v in enumerate(['tic', 'tac', 'toe']):
        # (i, v) -- ((0,'tic'), (1,'tac'), (2,'toe'))

        >>> questions = ['name', 'quest', 'favorite color']
        >>> answers = ['lancelot', 'the holy grail', 'blue']
        >>> for q, a in zip(questions, answers):
        # (q, a) -- (('name', 'lancelot'), ('quest', 'the holy grail'), ('favorite color', 'blue'))

Tom
 2012-12-24 20:35
$$ Command string

        EXEC statement, exec COMMANDstring may return a value
        EVAL function, eval (EXPRESSIONstring) must return a value
        COMPILE function, compile (COMMANDstring, "", EXECfunc)
        compile(source, filename, mode[, flags[, dont_inherit]])
        EXPR function,
            returns a COMMANDstring for eval(expr(object))
            works with __repr__ in class

            # exec STATEMENTstring
            # eval EXPstring
            # map (FUNC, ARG2list, ARG3list)


            # __repr__ returns a string to execute
            # __str__ works in PRINT statement

            # inside class, only methods, def func(self, [args])
            # inside method, call by self.property and self.method

        a += b - c :: a = a + (b - c)
        [x /if switch(x) else xx/ for x in items /if filter(x)/]

Tom
 2012-12-24 20:35
$$ Overview Chinese Code History

    Chinese code : Unicode ,gb2312 , cp936 ,GBK£¬GB18030
    936 Code Page (ANSI/OEM - Simplified Chinese GBK)

    # FILE HEADER of text file
    FF FE - Unicode
    FE FF - Unicode big endian
    EF BB BF - UTF-8

    UTF-8 fits to ISO-8859-1
    Chinese in 3 bytes
    GB2312, GBK, GB18020 in 2 bytes - DBCS
    AABB Chinese -- AA BB big endian, BB AA little endian

    GB2313 - CP20936
    GBK - CP936
    GB18030 supports more characters
    BIG5 - CP950

    # HISTORY
    1980 GB2312 - simplified Chinese - 7445 characters
    1983 Big5 - tranditional Chinese - 13060
    1995 GBK1.0 - 21886 characters
    2000 GB18030 - 27484 characters
    2003 Big55-2003
    2002 ISO 10646 - Unicode CNS 14649 - CJK - 20902
    2008 CNS 11643

    Unicode - UCS
    ISO - ISO 10646
    Unicode.org - Unicode
    Unicode 2.0 - ISO 10646-1
    Unicode 4.1.0
    ISO 10646-3:2003

    UTF-8, UTF-7, UTF-16, (UTF-32)
    GB18030 ~~ CP54936, but some GB18030 in 4 bytes

    UCS-2  - 2^16
    UCS-4  - 2^31
    BMP - plane0 of group0 of UCS4


Tom
 2012-12-24 20:36
$$ Tip : Global variable through dummy class

    class dummy():

        # PURPOSE : to create an object to carry values among classes or functions
        
        # REASON : in class/function, cannot reach global simple variables (number, string)
        # USAGE : x=dummy(); x.a=2; del x.a; # add/remove a properties
        pass

    p = dummy()
        # global passing channel


No comments:

Post a Comment