My python2 pocket reference - 20151212
This is a first version of python2 pocket reference.
Conditions
There is no syntax like switch, case, cond. Only if-elif-else is available.
>>> X = 100 >>> if X == 10: ... print 'X is 10' ... elif X == 20: ... print 'X is 20' ... else: ... print 'X is something else' ... X is something else >>>
Samples handling string and character
convert character to ascii code, like char-code
>>> ord('A') 65 >>>
convert ascii code to character, like code-char
>>> chr(65) 'A' >>>
convert string to byte array
>>> import array >>> array.array('B', "aaa") array('B', [97, 97, 97]) >>> x = array.array('B', "aaa") >>> x array('B', [97, 97, 97]) >>> x[1] 97 >>> x[2] 97 >>> x[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: array index out of range >>>
convert byte array to string with default encoding
>>> x = [97,98,99] >>> ''.join(map(chr,x)) 'abc' >>>
substring
>>> X = "0123456789" >>> Y = X[0:5] >>> Y '01234' >>>
find any characters in string
>>> X = "2000/12/31" >>> X.find('/') 4 >>> X.rfind('/') 7 >>> X = "This is a pen." >>> X.find('is') 2 >>> X.rfind('is') 5 >>> X.find('pen') 10 >>>
Basic File IO
Read from existed files, using 'r'
>>> with open('test.txt','r') as f: ... for line in f: ... print line ... Test >>> ***Write to some files, using 'w' >|python| >>> with open('test.txt', 'r') as f: ... for line in f: ... print line ... Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'test.txt' >>> with open('test.txt', 'w') as f: ... f.write('This is a test message.') ... >>> with open('test.txt', 'r') as f: ... for line in f: ... print line ... This is a test message. >>>
Socket Handling
TCP client as using context, reference is https://stackoverflow.com/questions/16772465/how-to-use-socket-in-python-as-a-context-manager
import argparse import socket from contextlib import contextmanager @contextmanager def tcp_connect_to(*args, **kwargs): s = socket.create_connection(*args, **kwargs) try: yield s finally: s.close() parser = argparse.ArgumentParser(description = 'Command Line Argument Parser') parser.add_argument('-host', dest = 'host', required = True) parser.add_argument('-port', dest = 'port', required = True) argument = parser.parse_args() x = vars(argument) dest = (x['host'], int(x['port'])) with tcp_connect_to(dest) as s: s.send("Hello!") ...
UDP client as using context, reference is same as TCP
import argparse import socket from contextlib import contextmanager @contextmanager def udp_connect_to(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: yield s finally: s.close() parser = argparse.ArgumentParser(description = 'Command Line Argument Parser') parser.add_argument('-host', dest = 'host', required = True) parser.add_argument('-port', dest = 'port', required = True) argument = parser.parse_args() x = vars(argument) dest = (x['host'], int(x['port'])) with udp_connect_to() as s: s.sendto("Hello", dest) data, server = s.recvfrom(8192) print '%s returns %s' % server, data