1.
Keywords—>with
with open('/etc/passwd','r') as f:
for eachLine in f:
print eachLine,
2.
Provide one parameter to assert
>>> assert 1==0, "one does not equal zero silly"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: one does not equal zero silly
3.
Imagine how to implement assert with function
def assert(expr, args=None):
if __debug__ and not expr:
raise AssertionError, args
4.
Defile new exception class.
class NetworkError(IOError):
pass
class FileError(IOError):
pass
5.
To check whether one module supports a function or not.
"access" in dir(os)
6.
Function deco
#!/usr/bin/env python
from time import ctime, sleep
def tsfunc(func):
def wrappedFunc():
print '[%s] %s() called' %(ctime(), func.__name__)
return func()
return wrappedFunc
@tsfunc
def foo():
pass
foo()
sleep(4)
for i in range(2):
sleep(1)
foo()
Output:
➜ core_python git:(dev) ✗ python deco.py
[Sat Aug 22 20:36:13 2015] foo() called
[Sat Aug 22 20:36:18 2015] foo() called
[Sat Aug 22 20:36:19 2015] foo() called
7
non-fixed args
def tupleVarArgs(arg1, arg2 = 'defaultB', *theRest):
'Display regular args and non-keyword variable args'
print 'formal arg 1:', arg1
print 'formal arg 2:', arg2
for eachArg in theRest:
print 'another arg:', eachArg
if __name__ == '__main__':
tupleVarArgs('acg')
tupleVarArgs('23',5.67)
tupleVarArgs('adf','123',67,90)
7.
filter , map , reduce
#!/usr/bin/env python
from random import randint
'Demonstrate filger'
def testFilter():
def odd(n):
return n%2
allNums = []
for eachNum in range(9):
allNums.append(randint(1,99))
print filter(odd, allNums)
def testFilterLambda():
'Using lambda to re-write the code'
allNums = []
for eachNum in range(9):
allNums.append(randint(1,99))
print filter(lambda n:n%2, allNums)
def testFilterList():
allNums = []
for eachNum in range(9):
allNums.append(randint(1, 99))
print [n for n in allNums if n%2]
def testFilterParseList():
print [n for n in [randint(1, 99) for i in range(9)] if n%2]
def testMap():
print map((lambda x:x+2),[0,1,2,3,4,5,6])
def testMapMultiList(): #just he hadoop map/reduce
print map((lambda x,y:x+y), [1,3,5],[2,4,6])
def testMapNoneList(): #Just like zip function
print map(None, [1,3,5],[2,4,6])
def testReduce():
print 'the total is:',reduce((lambda x,y:x+y),range(5))
if __name__ == '__main__':
print '_'*20
testFilter()
testFilterLambda()
testFilterList()
testFilterParseList()
print '_'*20
testMap()
testMapMultiList()
testMapNoneList()
print '_'*20
testReduce()
'''
Output:
____________________
[13, 75, 31, 95]
[39, 49, 89, 53, 7]
[25, 77, 95, 79]
[91, 41, 31, 85]
____________________
[2, 3, 4, 5, 6, 7, 8]
[3, 7, 11]
[(1, 2), (3, 4), (5, 6)]
____________________
the total is: 10
'''
8.
global , local , closure
'Global or Local'
def foo():
m = 3
def bar():
n = 4
print m + n
print m
bar()
#print m
#bar()
foo()
'Example about closure'
def counter(start_at = 0):
count = [start_at]
def incr():
count[0] += 1
return count[0]
return incr
count = conter(5) #From this we can see this seems like the object of a class
print count()
print count()