python 표준 라이브러리

Click here to load reader

Upload: -

Post on 16-Aug-2015

185 views

Category:

Software


0 download

TRANSCRIPT

  1. 1. 7 2015.7
  2. 2. Python : : datetime, calendar, collections, copy : math, random map(), filter() functools, itertools : os.path, pathlib, fnmatch : pickle : csv : os : sys : unittest, doctest , 9 , ,
  3. 3. Advanced String Formatting
  4. 4. Advanced String Formatting % format() format() Format Specification Mini-Language
  5. 5. Advanced String Formatting positional argument, keyword argument >>> '{0}{1}'.format('', '') # positional argument '' >>> '{a}{b}'.format(a='', b='') # keyword argument '' >>> '{}{}'.format('', '') # ( ) '' >>> '{0}{1}{2}{3}{4}{5}{6}'.format('', '', '', '', '', '', '') '' >>> '{6}{5}{4}{3}{2}{1}{0}'.format('', '', '', '', '', '', '') ''
  6. 6. Advanced String Formatting width, alignment >>> '{:2}{:2}'.format('', '') # (width) 2 ' ' >>> for n in (1, 23, 456): ... print('{:>4}'.format(n)) # , 4 ... 1 23 456 >>> for fruit in ('apple', 'banana', 'strawberry'): ... print('{:^10}'.format(fruit)) # , 10 ... apple banana strawberry
  7. 7. Advanced String Formatting presentation types >>> '{:s}'.format('') # 's': (string) >>> {:d}.format(1) # d: (decimal) 1 >>> {:2.1f}.format(36.55) # f: (fixed point) 36.5 >>> {:2.1e}.format(36.55) # e: (exponent notation) '3.7e+01'
  8. 8. Advanced String Formatting presentation types >>> fmt = '{:.2%} {:s} .' # '%': >>> for n in [0.99, 0.999, 0.9999, 0.99999]: ... print(fmt.format(n, '')) ... 99.00% . 99.90% . 99.99% . 100.00% .
  9. 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. 10. Regular Expression
  11. 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. 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
  13. 13. re Repitation () + -- 1 (occurrences) * -- 0 ? -- 0 1 >>> re.search(r'er+y', 'strawberry') >>> re.search(r'e+y', 'strawberry') >>> re.search(r'e*y', 'strawberry') >>> re.search(r'er?y', 'strawberry') >>> re.search(r'ex?.+y', 'strawberry')
  14. 14. re >>> s = 'purple [email protected] monkey dishwasher' >>> m = re.search('([w.-]+)@([w.-]+)', s) >>> if m: ... print(m.group()) ... print(m.group(1)) ... print(m.group(2)) ... [email protected] alice-b google.com
  15. 15. re findall >>> s = 'purple [email protected], blah monkey [email protected] blah dishwasher' >>> emails = re.findall(r'[w.-]+@[w.-]+', s) >>> for email in emails: ... print(email) ... [email protected] [email protected]
  16. 16. Data Types
  17. 17. datetime , >>> from datetime import datetime >>> datetime.now().weekday() # 0: , 6: 3 >>> datetime.now().isoweekday() # 1: , 7: 4 >>> tw = todays_weekday = datetime.now().isoweekday() >>> if 1 > import calendar >>> calendar.prmonth(2015, 7) July 2015 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 >>> calendar.setfirstweekday(6) >>> calendar.prmonth(2015, 7) July 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 >>> calendar.isleap(2014) False >>> calendar.isleap(2016) True 6: Sunday
  18. 19. collections
  19. 20. collections Python (dict, list, set, tuple) https://docs.python.org/3/library/collections.html
  20. 21. collections.ChainMap >>> from collections import ChainMap >>> default_settings = {'a': 1, 'b': 2} >>> custom_settings = {'b': 10, 'c': 11} >>> applied_settings = ChainMap(custom_settings, default_settings) >>> applied_settings ChainMap({'b': 10, 'c': 11}, {'b': 2, 'a': 1}) >>> for k, v in applied_settings.items(): ... print(k, v) ... b 10 c 11 a 1 http://stackoverflow.com/questions/23392976/what-is-the-purpose-of-collections-chainmap
  21. 22. collections.Counter >>> import collections >>> C = collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']) >>> C Counter({'b': 3, 'a': 2, 'c': 1}) >>> C.update('abcdaab') >>> C Counter({'b': 5, 'a': 5, 'c': 2, 'd': 1})
  22. 23. collections.deque double-ended queue >>> import collections >>> d = collections.deque([1, 2], maxlen=5) >>> d.append(3) >>> d deque([1, 2, 3], maxlen=5) >>> d.appendleft(4) >>> d deque([4, 1, 2, 3], maxlen=5) >>> d.append(5) >>> d deque([4, 1, 2, 3, 5], maxlen=5) >>> d.append(6) >>> d deque([1, 2, 3, 5, 6], maxlen=5) >>> d.pop() 6 >>> d deque([1, 2, 3, 5], maxlen=5) >>> d.popleft() 1 >>> d deque([2, 3, 5], maxlen=5) >>> d.extend([7, 8]) >>> d deque([2, 3, 5, 7, 8], maxlen=5) >>> list(d) [2, 3, 5, 7, 8]
  23. 24. collections.defaultdict >>> import collections >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = collections.defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> d defaultdict(, {'red': [1], 'yellow': [1, 3], 'blue': [2, 4]}) >>> list(d.items()) [('red', [1]), ('yellow', [1, 3]), ('blue', [2, 4])] https://docs.python.org/3/library/collections.html#defaultdict-examples
  24. 25. collections.namedtuple >>> import collections >>> Point = collections.namedtuple('Point', ['x', 'y']) >>> p = Point(11, y=22) >>> print(p[0], p[1]) # indexable like plain tuple 11 22 >>> print(p.x, p.y) # field accessible by name 11 22 >>> p # readable __repr__ Point(x=11, y=22) https://docs.python.org/3/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
  25. 26. collections.OrderedDict >>> d = {} >>> d['a'] = 'A' >>> d['b'] = 'B' >>> d['c'] = 'C' >>> d['d'] = 'D' >>> d['e'] = 'E' >>> d {'d': 'D', 'b': 'B', 'c': 'C', 'a': 'A', 'e': 'E'} >>> import collections >>> od = collections.OrderedDict() >>> od['x'] = 'X' >>> od['y'] = 'Y' >>> od['a'] = 'A' >>> od['b'] = 'B' >>> od['c'] = 'C' >>> od OrderedDict([('x', 'X'), ('y', 'Y'), ('a', 'A'), ('b', 'B'), ('c', 'C')])
  26. 27. collections UserDict, UserList, UserString dict, list, string wrapper dict, list, str
  27. 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--
  28. 29. copy Shallow and Deep Copy (shallow copy) >>> L1 = [['apple', 'banana'], 'milk'] >>> import copy >>> L2 = copy.copy(L1) >>> L1[0].append('orange') >>> L1[1] = 'cheese' >>> L1 [['apple', 'banana', 'orange'], 'cheese'] >>> L2 [['apple', 'banana', 'orange'], 'milk'] (deep copy) >>> L1 = [['apple', 'banana'], 'milk'] >>> import copy >>> L2 = copy.deepcopy(L1) >>> L1[0].append('orange') >>> L1[1] = 'cheese' >>> L1 [['apple', 'banana', 'orange'], 'cheese'] >>> L2 [['apple', 'banana'], 'milk']
  29. 30. Numeric and Mathematical Modules
  30. 31. math >>> math.pi # 3.141592653589793 >>> math.pow(2, 5) # 2 * 2 * 2 * 2 * 2 32.0 >>> math.sqrt(144) # 144 12.0 >>> math.factorial(4) # 1 * 2 * 3 * 4 24
  31. 32. random Pseudo random number generator Uniform selection os.urandom() SystemRandom https://docs.python.org/3.4/library/random.html
  32. 33. random.choice() >>> import random >>> dice = [i for i in range(1, 7)] >>> dice [1, 2, 3, 4, 5, 6] >>> random.choice(dice) 4 >>> random.choice(dice) 6 >>> random.choice(dice) 2 >>> random.choice(dice) 2 >>> random.choice('aeiou') 'u' >>> random.choice('aeiou') 'a' >>> random.choice('aeiou') 'i' >>> random.choice('aeiou') 'a' >>> random.choice('aeiou') 'a' >>> random.choice('aeiou') 'u'
  33. 34. random.randrange() >>> random.randrange(7) 2 >>> random.randrange(7) 4 >>> random.randrange(7) 0 >>> random.randrange(7) 3 >>> random.randrange(7) 1 >>> random.randrange(7) 1 >>> random.randrange(7) 0 >>> random.randrange(3, 30, 3) 15 >>> random.randrange(3, 30, 3) 24 >>> random.randrange(3, 30, 3) 3 >>> random.randrange(3, 30, 3) 12 >>> random.randrange(3, 30, 3) 27 >>> random.randrange(3, 30, 3) 21 >>> random.randrange(3, 30, 3) 24
  34. 35. random.shuffle() >>> dice [1, 2, 3, 4, 5, 6] >>> random.shuffle(dice) >>> dice [5, 2, 4, 3, 1, 6] >>> new_dice = tuple([i for i in range(1, 7)]) >>> new_dice (1, 2, 3, 4, 5, 6) >>> random.shuffle(new_dice) Traceback (most recent call last): File "", line 1, in File "C:Python34librandom.py", line 272, in shuffle x[i], x[j] = x[j], x[i] TypeError: 'tuple' object does not support item assignment
  35. 36. Functional Programming in Python
  36. 37. vs (Imperative), (procedural) (Loop) (state): . (declarative), (functional) (recursion) Loop state
  37. 38. map() Iterable object >>> prime_nums = (1, 2, 3, 5, 7, 9, 11, 13) >>> [p * p for p in prime_nums] [1, 4, 9, 25, 49, 81, 121, 169] >>> list(map(lambda x: x * x, prime_nums)) [1, 4, 9, 25, 49, 81, 121, 169] >>> data = ' | 010-300-3333 ||' >>> data.split('|') [' ', ' 010-300-3333 ', '', ''] >>> list(map(str.strip, data.split('|'))) [' ', '010-300-3333', '', ''] : ()
  38. 39. filter() ( True ) filter >>> multiples_of_3 = range(3, 31, 3) # 30 3 >>> tuple(multiples_of_3) (3, 6, 9, 12, 15, 18, 21, 24, 27, 30) >>> tuple(filter(lambda x: x % 15 != 0, multiples_of_3)) # 15 (3, 6, 9, 12, 18, 21, 24, 27) >>> langs = ('C', 'Java', 'Perl', 'PHP', 'Python') >>> tuple(filter(lambda s: s.startswith('P'), langs)) # 'P' ('Perl', 'PHP', 'Python') : ()
  39. 40. itertools count() >>> import itertools >>> natural = itertools.count(1) >>> next(natural) 1 >>> next(natural) 2 >>> next(natural) 3 >>> even = itertools.count(2, 2) >>> next(even) 2 >>> next(even) 4 >>> next(even) 6 >>> negative = itertools.count(-1, -1) >>> next(negative) -1 >>> next(negative) -2 >>> next(negative) -3
  40. 41. itertools cycle() >>> import itertools >>> day = itertools.cycle('') >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) '' >>> next(day) ''
  41. 42. functools reduce() >>> L = [1, 2, 3, 4, 5] >>> import functools >>> functools.reduce(lambda a, x: a + x, L) 15 https://docs.python.org/3/library/functools.html
  42. 43. File and Directory Access
  43. 44. os.path >>> import os >>> print(os.path.join('c:python34', 'python.exe')) c:python34python.exe >>> print(os.path.join('/usr/bin', 'python')) /usr/binpython >>> print(os.path.expanduser('~')) C:UsersYong Choi >>> print(os.path.join(os.path.expanduser('~'), 'documents')) C:UsersYong Choidocuments http://juehan.github.io/DiveIntoPython3_Korean_Translation/comprehensions.html#ospath
  44. 45. pathlib (ver. 3.4) Pure Path: Concrete Path: Pure Path . Path >>> from pathlib import * >>> p = Path('c:/python34/python.exe') >>> p WindowsPath('c:/python34/python.exe') >>> p.exists() True >>> PurePath('/usr/bin/python3').parts ('', 'usr', 'bin', 'python3') https://docs.python.org/3/library/pathlib.html
  45. 46. pathlib properties >>> PureWindowsPath('c:/Program Files/').drive 'c:' >>> PurePosixPath('my/library/setup.py').name 'setup.py' >>> PureWindowsPath('//some/share/setup.py').name 'setup.py' >>> PurePosixPath('my/library/setup.py').suffix '.py' >>> PurePosixPath('my/library.tar.gz').suffix '.gz' >>> PurePosixPath('my/library.tar.gz').suffixes ['.tar', '.gz'] >>> PurePosixPath('my/library.tar.gz').stem 'library.tar'
  46. 47. pathlib methods >>> PureWindowsPath('c:/Windows').as_uri() 'file:///c:/Windows' >>> PurePosixPath('/etc').joinpath('passwd') PurePosixPath('/etc/passwd') >>> PurePath('/a/b/c.py').match('b/*.py') True >>> PurePath('/a/b/c.py').match('a/*.py') False >>> PureWindowsPath('b.py').match('*.PY') True >>> PurePosixPath('b.py').match('*.PY') False
  47. 48. fnmatch >>> import fnmatch >>> import os >>> matches = [] >>> for root, dirnames, filenames in os.walk('C:Python34'): ... for filename in fnmatch.filter(filenames, '*.exe'): ... matches.append(os.path.join(root, filename)) ... >>> matches ['C:Python34python.exe', 'C:Python34pythonw.exe', 'C:Python34Libdi stutilscommandwininst-10.0-amd64.exe', 'C:Python34Libdistutilscomman dwininst-10.0.exe', 'C:Python34Libdistutilscommandwininst-6.0.exe',
  48. 49. Data Persistence
  49. 50. pickle Python >>> import pickle >>> favorite_color = {'lion': 'yellow', 'kitty': 'red'} >>> pickle.dump(favorite_color, open('favorite_color', 'wb')) >>> import pickle >>> favorite_color = pickle.load(open('favorite_color', 'rb')) >>> favorite_color {'kitty': 'red', 'lion': 'yellow'} https://wiki.python.org/moin/UsingPickle https://commons.wikimedia.org/wiki/File:Pickled_Walnuts_Jar.jpg
  50. 51. File Formats
  51. 52. CSV(Comma Seperated Values)
  52. 53. csv.writer >>> import csv >>> with open('eggs.csv', 'w', newline='') as csvfile: ... spamwriter = csv.writer(csvfile, delimiter=' ', ... quotechar='|', quoting=csv.QUOTE_MINIMAL) ... spamwriter.writerow(['Spam'] * 5 + ['Baked Beans']) ... spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) ... 40 37 Spam Spam Spam Spam Spam |Baked Beans| Spam |Lovely Spam| |Wonderful Spam|
  53. 54. csv.reader Spam Spam Spam Spam Spam |Baked Beans| Spam |Lovely Spam| |Wonderful Spam| >>> import csv >>> with open('eggs.csv', newline='') as csvfile: ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') ... for row in spamreader: ... print(', '.join(row)) ... Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Lovely Spam, Wonderful Spam https://docs.python.org/3/library/csv.html
  54. 55. Generic Operating System Services
  55. 56. os >>> os.environ['HOME'] 'C:UsersYong Choi' >>> os.environ['NUMBER_OF_PROCESSORS'] '4' >>> os.getcwd() 'C:UsersYong Choi' >>> os.chdir('Program Files') >>> os.getcwd() 'C:Program Files'
  56. 57.
  57. 58. sys >>> sys.platform 'win32' >>> sys.byteorder 'little' >>> sys.getwindowsversion() sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') >>> sys.version_info sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0) >>> sys.winver '3.4'
  58. 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
  59. 60. sys.path D:summer_pythonlecture07mycalendar.py import calendar def prmonth(theyear, themonth, w=0, l=0): calendar.setfirstweekday(6) calendar.prmonth(theyear, themonth, w, l) >>> import sys >>> sys.path.append('D:summer_pythonlecture07') >>> import mycalendar >>> mycalendar.prmonth(2015, 7)
  60. 61. Development Tools
  61. 62. unittest test fixture: test case: test suite: test runner:
  62. 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/
  63. 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
  64. 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
  65. 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.
  66. 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