Open ahasusie opened 5 years ago
useful method
# all()
if all(max >= x for x in nums if x != max):
do sth
# sort([iterable]) is a in place modification, don't return anything
nums.sort()
print nums
# sorted([iterable]) return a new sorted list from the items in iterable.
tmp = sorted(nums)
# change str or an int to array, instead of divide by 10 multiple times
return [int(i) for i in str/str(int)]
arr = list(str)
# array to string
return ''.join(arr)
# matrix
row, col = len(matrix), len(matrix[0])
matrix = [[1, 2, 3], [4, 5, 6]]
zip(*matrix)[::-1] #rotate the remaining matrix counter-clockwise
len = 3
arr = [[True] * len]
[[True, True, True]]
arr = [[True]] * len
[[True], [True], [True]]
# in-place modify nums, should use nums[:], nums[] doesn't work
nums = [1,2,3,4,0,4,0,5,0,7]
nums[:] = [i for i in nums if i != 0] # ok
arr[] = [i for i in nums if i != 0] # ok
# list add items
arr.append() # add at end
arr.extend([iterable]) # equals to arr += [iterable]
arr.insert(index, value)
# sys.argv
import sys
args = sys.argv[1:]
print args
import datetime
datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
'12. January 2019 05:21PM'
# read from config file
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
args = {}
sections = config.sections()
for s in sections:
print config.items(s)
args.update(dict(config.items(s)))
print args
python is interpret language, that means source code is implemented directly without previously compiled into machine-language instructions.
a compiled language is one where the program, once compiled, is expressed in the instructions of the target machine. a interpreted language is one where the instructions are not directly executed by the target machine, but instead read and executed by some other program(virtual machine)
here are the advantages of compiled languages: Faster performance by directly using the native code of the target machine Opportunity to apply quite powerful optimisations during the compile stage
here are the advantages of interpreted languages: Easier to implement No need to run a compilation stage: can execute code directly Can be more convenient for dynamic languages
python source code .py is compiled(translated) into bytecode .pyc, then loaded into python runtime and interpreted by a virtual machine(PVM, which is part of the python system that you installed). the CPU executes the virtual machine code, which performs the work indicated by bytecode. each time an interpreted program run, the interpreter must convert source code into machine code and pull in the runtime lib, to avoid the ineffiency, python compile source code to bytecode the first time is exe
exception
class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg
try:
raise Networkerror("Error")
except Networkerror as e:
print (e.args)
try:
a = [1, 2, 3]
print a[3]
except LookupError:
print "Index out of bound error."
else:
print "Success"
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print "division by zero!"
... else:
... print "result is", result
... finally:
... print "executing finally clause"
If you need to determine whether an exception was raised but don’t intend to handle it, a simpler form of the raise statement allows you to re-raise the exception:
>>> try:
... raise NameError('HiThere')
... except NameError:
... print 'An exception flew by!'
... raise
...
An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: HiThere
assert
def avg(marks):
assert len(marks) != 0,"List is empty."
return sum(marks)/len(marks)
mark2 = [55,88,78,90,79]
print("Average of mark2:",avg(mark2))
mark1 = []
print("Average of mark1:",avg(mark1))
Assertions are the condition or boolean expression which are always supposed to be true in the code. assert statement takes an expression and optional message. assert statement is used to check types, values of argument and the output of the function. assert statement is used as debugging tool as it halts the program at the point where an error occurs.
max integer: sys.maxint
min integer: -sys.maxint-1
x % y
x // y
divmod(x, y) # pair of (x//y, x%y)
math.floor(x) # x is float number
math.ceil(x)
int.bit_length()
float.is_integrer()
logging:
import logging
logging.info("start here...")
logging.debug()
logging.warning()
logging.error()
logging.basicConfig(format='%(asctime)s %(message)s') # add time and date in log
logging.basicConfig(filename='example.log',level=logging.DEBUG) # log into a file
debug:
python -m pdb myscript.py
python option parser
parser = OptionParser()
parser.add_option("--tb","--testbed", type="str", dest="variable_tb", \
help="The INI file that describes the test bed information", \
default="TestBed.ini")
parser.add_option("--td","--testdescription", type="str", dest="variable_td", \
help="The INI file that describes the tests that need to be run", \
default="TestDescription.xml")
parser.add_option("--debug", type="int", dest="dbg", \
help="Run the test suites in debug mode (i.e: ABORT_ON_FAIL set)", \
default=False)
(options, args) = parser.parse_args()
tb_file = options.variable_tb
td_file = options.variable_td
python unittest framework
import unittest
class functionTests(unittest.TestCase):
def setUp(self):
print "do setup work"
self.A = 123
self.B = 456
def testcase_A(self):
print "test case A"
self.assertEqual(self.A, 3423523, "not correct value A")
def testcase_B(self):
print "test case B"
self.assertEqual(self.B, 456, "not correct value B")
def tearDown(self):
print "do tear down work"
def createSuite(self):
suite = unittest.TestSuite()
suite.addTest(functionTests("testcase_A"))
suite.addTest(functionTests("testcase_B"))
return suite
if __name__ == "__main__":
unittest.main()
taskObj = list()
taskObj.append([suite_num, suite_name, priority])
taskObj.sort()
for t in taskObj:
cmd = "cmd /c start /MIN /WAIT \""+t[0]+" "+t[1]+" "+t[2]+"\" "+sys.executable+" c:\\svn\\ta\\suiteprocess.py "+cfgfile
logging.log("cmd: "+cmd)
p = subprocess.Popen(cmd)
python execute shell commands:
Popen + communicate()
subprocess.call(["ls", "-l"]) # call(), check_call()
subprocess.call("ls -l", shell=True)
subprocess.check_output(["ls", "-l"])
subprocess.check_output("ls -l", shell=True)
import shlex, subprocess
command_line = raw_input()
ls -l
args = shlex.split(command_line)
p = subprocess.Popen(args) # Success!
build in function
class A(object):
def foo(self,x): # self holds a reference to a newly created instance
print "executing foo(%s,%s)"%(self,x)
@classmethod
def class_foo(cls,x): # have a reference to a class object as the first parameter
print "executing class_foo(%s,%s)"%(cls,x)
@staticmethod
def static_foo(x):
print "executing static_foo(%s)"%x
a=A()
a.foo(1)
A.class_foo(2)
A.static_foo(3) # or a.static_foo(3)
# We generally use class method to create factory methods. Factory methods return class object ( similar to a constructor ) for different use cases.
# We generally use static methods to create utility functions.
python sequence types: str, unicode, list, tuple, bytearray, buffer, xrange
list & bytearray support more operations:
### deal with files:
id1_set = set()
id2_set = set()
with open('id1.txt', 'r') as id1File:
for line in id1File:
id1_set.add(line.strip())
with open('id2.txt', 'r') as id2File:
for line in id2File:
id2_set.add(line.strip())
diffs = id2_set.difference(id1_set)
for d in diffs:
print d
with open('id1.txt', 'r+') as f:
data = f.read()
f.seek(0)
f.write("this is a testing line for writing...")
f.truncate()
reader_a = open('id1.txt', 'rb')
chunks = []
data = reader_a.read(64)
while data != "":
chunks.append(data)
data = reader_a.read(64)
if data:
chunks.append(data)
print len(chunks)
reader_a.close()
print chunks
with open('id1.txt', 'rb') as reader_b:
data = reader_b.readlines()
print len(data)
print data
with open('id1.txt', 'rb') as reader_c:
data = reader_c.read()
print len(data)
print data
data = []
with open('id1.txt', 'rb') as reader_d:
for line in reader_d:
data.append(line)
print len(data)
print data
"r" - Read - Default value. Opens a file for reading, error if the file does not exist "a" - Append - Opens a file for appending, creates the file if it does not exist "w" - Write - Opens a file for writing, creates the file if it does not exist "x" - Create - Creates the specified file, returns an error if the file exist r+ w+ a+ are used for update In addition you can specify if the file should be handled as binary or text mode "t" - Text - Default value. Text mode "b" - Binary - Binary mode (e.g. images)
string format string methods: https://www.w3schools.com/python/python_ref_string.asp
>>> '{:,}'.format(1234567890)
'1,234,567,890'
>>> points = 19.5
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 88.64%'
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
https://nbviewer.jupyter.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb#solutions https://nbviewer.jupyter.org/github/rasbt/python_reference/blob/master/tutorials/not_so_obvious_python_stuff.ipynb#python_small_int https://nbviewer.jupyter.org/github/rasbt/python_reference/blob/master/tutorials/useful_regex.ipynb