9. Advanced String Formatting type conversion >>>
'{:s}'.format(1) # int 's' Traceback (most recent call last): File
"", line 1, in ValueError: Unknown format code 's' for object of
type 'int' >>> '{!r:}'.format(1) # repr() '1' >>>
'{!s:}'.format(1) # str() '1'
10. Regular Expression
11. re
https://developers.google.com/edu/python/regular-expressions
>>> import re >>> s = 'an example word:cat!!'
>>> m = re.search(r'word:www', s) >>> if m: ...
print('found', m.group()) ... else: ... print('did not find') ...
found word:cat >>> print('1n2n3') 1 2 3 >>>
print(r'1n2n3') 1n2n3
12. re >>> re.search(r'iii', 'piiig') >>>
re.search(r'igs', 'piiig') # match=None >>>
re.search(r'..g', 'piiig') # . -- any single char >>>
re.search(r'ddd', 'p123g') # d -- [0-9] >>>
re.search(r'www', '@@abcd!!') # w -- [a-zA-Z0-9_] >>>
re.search(r'^...', 'p123g') # ^ -- start >>>
re.search(r'...$', 'p123g') # $ -- end
28. collections.abc >>> from abc import * >>>
class C(metaclass=ABCMeta): ... @abstractmethod ... def
absMethod(self): ... pass ... >>> c = C() Traceback (most
recent call last): File "", line 1, in TypeError: Can't instantiate
abstract class C with abstract methods absMethod >>> class
B(C): ... def absMethod(self): ... print("Now a concrete method")
... >>> b = B() >>> b.absMethod() Now a concrete
method http://autonomist.tistory.com/entry/-Python-3--Part-2--
59. sys.argv sys_argv_test.py import sys for i in
range(len(sys.argv)): print('sys.argv[{:d}]: {:s}'.format(i,
sys.argv[i])) > py -3 sys_argv_test.py a b c d sys.argv[0]:
sys_argv_test.py sys.argv[1]: a sys.argv[2]: b sys.argv[3]: c
sys.argv[4]: d
62. unittest test fixture: test case: test suite: test
runner:
63. unittest - assertEqual import unittest def fun(x): return x
+ 1 class MyTest(unittest.TestCase): def test(self):
self.assertEqual(fun(3), 4) if __name__ == '__main__':
unittest.main() .
----------------------------------------------------------------------
Ran 1 test in 0.000s OK
http://docs.python-guide.org/en/latest/writing/tests/
64. unittest failUnless, failIf import unittest def IsOdd(n):
return n % 2 == 1 class IsOddTests(unittest.TestCase): def
testOne(self): self.failUnless(IsOdd(1)) def testTwo(self):
self.failIf(IsOdd(2)) if __name__== '__main__': unittest.main() ..
------------------------------------------- Ran 2 tests in 0.000s
OK
http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html
65. unittest assertRaises import unittest class
TestStringMethods(unittest.TestCase): def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper())
def test_split(self): s = 'hello world' self.assertEqual(s.split(),
['hello', 'world']) with self.assertRaises(TypeError): s.split(2)
if __name__ == '__main__': unittest.main()
https://docs.python.org/3.4/library/unittest.html#basic-example
66. doctest def square(x): """Squares x. >>> square(2)
4 >>> square(-3) 9 """ return x + x if __name__ ==
'__main__': import doctest doctest.testmod() > python
mysquare.py ******************************************* File
"mysquare.py", line 6, in __main__.square Failed example:
square(-3) Expected: 9 Got: -6
******************************************* 1 items had failures: 1
of 2 in __main__.square ***Test Failed*** 1 failures.
67. 2to3 Python 2 Python 3 > dir /b hello.py > type
hello.py print 'Hello' > 2to3 -w hello.py > dir /b
hello.py.bak hello.py > type hello.py print('Hello') print
'Hello' print('Hello')2to3