Chapter 15. Writing Good Programs

You will learn

  • How to divide a project into smaller tasks
  • How to divide a program into functions and classes
  • How to use pylint to improve your program code
  • How to use Mercurial to keep track of program versions(git 배웠으니 pass)
  • How to share your program with other people
  • How to improve a program iteratively
  • How to build your own modules and packages

15.2 Problem description: uncertainty

15.2.1 There is Uncertainty in Writing Programs

15.2.2 Example Programming Project

15.3 SOFTWAR ENGINEERING

  • First, make it work
  • Second, make it nice.
  • Third, and only if it is really necessary, make it fast.
  • 이전 장: to write working program
  • 이번 장: writing clean, transparent programs
  • Third: 깊은 알고리즘 같은 지식 필요

15.3.1 Dividing a Programming Project into Smaller Tasks

  • 프로그램이 어떤걸 하냐?
  • 이 프로그램이 주는 이익이 뭐냐?
  • 얼마나 효과적인가 확인하라
  • 목표를 정확히 정해야 한다. 당장 써라
  • FASTA files와 aaRS protein sequences을 깨끗이 해라. sequence alignment를 만드는데 사용될 것
  • 목표를 명확히 해야 시간을 줄일 수 있다.

What is the input?

  • 프로그램이 읽기 위해서는 어떤 데이터가 필요한가?

  • 프로그램은 디렉토리 안의 많은 FASTA files이 포함하고 있는 protein sequences를 읽을 것이다. 이 sequence 포맷은

gi|sequence name|species name AMINQACIDSEQUENCE

  • file을 읽기 위한 함수를 작성할 수 있다. 그러나 아직 시작하지마!

What is the output?

  • 이 파일들은 어떤 프로그램들을 생산하나?
  • 자세한 logfile을 쓰기 위해 무엇이 필요한가?
  • windows을 열기 위해 필요한 것은? graphics 을 만들기 위해 필요한 것은?
  • 입력하는 데이터나 파라미터가 바뀌면 무슨 일이 일어나는가?
  • The program writes a single FASTA file with aaRS sequences.
  • First, the format of the FASTA file should be the same as it is in the input.
  • Second, the order of the sequences in the output does not matter.
  • Third, the format described for the FASTA output must be readable by the program used for creating a sequence alignment.
  • 그것들의 추정을 써봐라. 체크하고 프로젝트 관계자와 의논해라.
  • 깨끗하게 하는 것이나 자세한 것은 이 시기에 하지 마라. 나중에 해라.

What should happen between input and output?

aaRS program needs to do following:
  • Remove all sequences that do not have Phenylalanine or Phe in its name.
  • Remove all duplicate sequence entries(identical sequences).

  • duplicate entires가 뭔 뜻이냐? Does it mean that the sequence is identical or that the description is identical? Or both?

  • 정확히 정의를 하고 가자. 의심이 생기면 먼저 물어보라. 프로그램은 나중이다.

  • collecting requirements

  • user story
  • 명확한 요구사항은 너의 프로그래밍 시간을 줄여줄 것이다.

15.3.2 Split a Program into Functions and Classes

  • 짧은 프로그램이라면 바로 코딩할 수 있다.
  • 큰 프로그램이라면 trouble을 피할 수 있어야 한다.
  • 먼저 지지대를 설치하라: functions과 classes를 디자인하라
  • 좋은 전략은 input, output, work을 위한 functions을 나누어 정의

In [1]:
def read_fasta_files(directory):
    '''
    Reads a directory with many FASTA files containing
    protein sequences.
    '''
    pass

def filter_phe(sequences):
    '''
    Removes all sequences that do not have
    Phenylalanine or Phe in their name.
    '''
    pass

def remove_duplicates(sequences):
    '''
    Remove all sequence records, having an identical
    sequence.
    '''
    pass

def write_fasta(sequences, filename):
    '''
    Writes a single FASTA file with aaRS sequences.
    '''
    pass

if __name__ == '__main__':
    INPUT_DIR = 'aars/'
    OUTPUT_FILE = 'phe_filtered.fasta'
    seq = read_fasta_files(INPUT_DIR)
    filter_phe(seq)
    remove_duplicates(seq)
    write_fasta(seq, OUTPUT_FILE)

근데 프로그래밍을 처음부터 이렇게 구조적으로 짤 수 있나?

  • 나도 이렇게 짜는데 굉장히 오랜 시간이 걸렸는데??
  1. 내가 접했던 방법은 처음엔 무조건 동작하게끔 한다(예외처리 전혀없이)
  2. 그 일부분을 함수로 만들어 떼낸다.
  3. Exception handling을 추가한다.
  4. Test code를 작성한다.
  • 이런 로직인데.. 저렇게 구조적으로 알려준다고 해도 처음부터 구조적으로 짤 수 없다. 차차 나아지는 거다.
  • 그런데 생각하는 방법은 많이 배울 수 있으니 필히 마스터하자.

  • 짧은 코드일수록 디버깅이 쉽다.
  • 현재 이 코드는 아무일도 안한다. 하지만 이렇게 구조적으로 짜놓게 되면 살만 덧붙이면 되기 때문에 훨씬 쉽게 코딩이 가능하다(요즘에 와서 깨달은 사항: 예전에 이런 내용을 많이 봤으면 '참 좋았겠다'라는 생각 잠깐 함. 그런데 삽질을 했으니 그게 중요하다고 깨달은거지. 그냥 편히 왔으면 그냥 그러려니 했을것 같음)

BOX 15.1 Writing smaller programs

  • "Today I had a successful day at program: I deleted 300 lines of code"
  • 프로그래밍의 미덕: 에러를 쉽게 찾을 수 있어야 한다.
  • Master Foo and the Ten Thousand Lines
  • 파이썬 철학: 간단한 것이 더 좋다.

python program1.py

python program2.py

python program3.py

  • 걱정하지 마라. 이렇게 구조화를 먼저 하는거다.

  • 나중에

import program1
import program2
import program3
  • 이렇게 해주면 되면 잘 정의된 component 프로그램이 되는 것이다.
  • 짧은 프로그램으로 나누는 것은 art다.

15.3.3 Writing Well-Formatted Code

PEP8: standardized convention to format code

  • You should add a space after commas and arithmetic operators
  • Variables in functions should have lowercase names.
  • Constans in modules should have uppercase names.
  • After each function there should be two empty lines.(응? 한줄로 알고 있는데 Code Like a Pythonista: Idiomatic Python )
  • Each function should have a documentation string as a comment.
  • pylint: check how well the Python code complies with the PEP8 convention.
  • 7.0~9.0 사이만 유지하면 된다. 꼭 10점 맞으려고 하지 않아도 된다.
  • pylint | Kang Sung Jin

pylint 장점

  • IDE에서 작성하면 어느정도의 Python PEP 8문서의 내용들과 동일하게 잡아주지만…
  • 못잡아주는 아이들을 잡아준다.
  • 모듈 이름도 이렇게 해라 저렇게해라..
  • 함수내에 너무 많이 뭘하고 있다..등등…
  • 작게 작게 짜를수 있도록 세팅을 할수 있도록 해주는게 좋음.

pylint 단점

  • 귀찮을 수도 있다...
  • 억수로 귀찮다.......

In [3]:
!pylint 12-debugging/handling_exceptions_else.py


No config file found, using default configuration
************* Module handling_exceptions_else
C: 15, 4: Invalid constant name "filename" (invalid-name)
C: 16, 4: Invalid constant name "in_file" (invalid-name)


Report
======
10 statements analysed.

Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |2      |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |0      |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |0      |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |0      |NC       |NC         |
+-----------+-------+---------+-----------+



Messages
--------

+-------------+------------+
|message id   |occurrences |
+=============+============+
|invalid-name |2           |
+-------------+------------+



Global evaluation
-----------------
Your code has been rated at 8.00/10

Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |11     |44.00 |NC       |NC         |
+----------+-------+------+---------+-----------+
|docstring |12     |48.00 |NC       |NC         |
+----------+-------+------+---------+-----------+
|comment   |0      |0.00  |NC       |NC         |
+----------+-------+------+---------+-----------+
|empty     |2      |8.00  |NC       |NC         |
+----------+-------+------+---------+-----------+



Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |NC         |NC         |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+



Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |NC       |NC         |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |NC       |NC         |
+-------------------------+------+---------+-----------+




In [6]:
!pylint 10-functions/calc_distance.py


No config file found, using default configuration
************* Module 10-functions.calc_distance
C: 18, 0: Trailing whitespace (trailing-whitespace)
C: 16, 0: Invalid argument name "p1" (invalid-name)
C: 16, 0: Invalid argument name "p2" (invalid-name)
C: 18, 4: Invalid variable name "dx" (invalid-name)
C: 19, 4: Invalid variable name "dy" (invalid-name)
C: 20, 4: Invalid variable name "dz" (invalid-name)


Report
======
10 statements analysed.

Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+



Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |6      |5        |+1.00      |
+-----------+-------+---------+-----------+
|refactor   |0      |0        |=          |
+-----------+-------+---------+-----------+
|warning    |0      |0        |=          |
+-----------+-------+---------+-----------+
|error      |0      |0        |=          |
+-----------+-------+---------+-----------+



Messages
--------

+--------------------+------------+
|message id          |occurrences |
+====================+============+
|invalid-name        |5           |
+--------------------+------------+
|trailing-whitespace |1           |
+--------------------+------------+



Global evaluation
-----------------
Your code has been rated at 4.00/10 (previous run: 5.00/10, -1.00)

Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |9      |37.50 |9        |=          |
+----------+-------+------+---------+-----------+
|docstring |13     |54.17 |13       |=          |
+----------+-------+------+---------+-----------+
|comment   |0      |0.00  |0        |=          |
+----------+-------+------+---------+-----------+
|empty     |2      |8.33  |2        |=          |
+----------+-------+------+---------+-----------+



Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |1          |=          |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |1      |1          |=          |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+



Docstrings

  • 각 function이나 class 안에 triple-quoted로 감싸져있는 comment가 docstring 이다.
class AARSFilter(object):
    '''
    Reads a set of FASTA files and remove duplicate
    sequence entires.
    '''
    pass

comments

  • #이 앞에 있으면 인터프리터는 무시한다. 그래서 주석이 된다.
  • 주석은 짧게 쓰고 가장 중요한 정보를 줄 수 있다.
  • 프로그램을 수정하고 주석도 같이 수정하자. 그렇지 않으면 나중에 수정할 때 굉장히 헷갈릴 수 있다.

15.3.4 Using a Repository to Control Program Versions

  • 뭔가 잘못됐을 때 원래대로 돌아갈 수 있는가?
  • 이건 git 하면서 많이 했으니 pass~

15.3.5 How to Release Your Program to Other People

  • release: To distribute a program, you create a stable version
  • First, set a version number: 공동작업자와 작업할 때 도움을 줄 것
  • Second, Write a short README.TXT file

    • What is the name of the program?
    • What is your name and how can you be contacted?
    • What is the version number?
    • What is the program good for? Descibe it in 50-100 words.
    • How can the program be used? A good idea is to include a simple command-line example.
    • Under what conditions can the program be distributed? For example,
      • "All rights reserved".
      • "Distributed under the conditions of the Python license"
      • 이밖에도 많은 소프트웨어 라이센스들이 있다.
  • Finally, create a zip file out of the directory with the program, including the README.TXT file.

몇가지 release 방법

  • distutils library를 사용해서 install script를 자동 생성
  • py2exe를 사용해서 Windows의 실행가능한 파일을 만든다. 이렇게 만들면 사용자들은 python을 설치하지 않아도 프로그램을 사용할 수 있다.
  • sourceforge나 github account를 활용
  • 1. An Introduction to Distutils — Python v2.7.8 documentation

15.3.6 The Cycle of Software Development

  • Plan: You first need to have a rough idea what your program should do.
  • Program: Next you need to implement the plan. Create the functions, classes, and moduels that realize your initial idea.

BOX 15.2 SMALL DATA FILES FOR TESTING

  • input을 줄여서 테스트 해봐라.
  • 그렇다면 확인해야 할 output도 줄어드니 프로그램 분석이 용이하다.

input - 9개

Primary 441.462
Secondary   29.031
Secondary   46.009
Secondary   40.932
Secondary   34.952
Primary 139.907
Secondary   82.248
Secondary   39.819
Secondary   144.143

output - 9개

category <100 100-300 >300
Primary :     1     1     1
Secondary:    2     2     2
  • Prove: 너의 프로그램을 리얼 월드에 드러내라. 유용한가? feedback을 받아라.

Manifesto for Agile Software Development

  1. individuals and interactions over processes and tools
  2. working software over comprehensive documentation
  3. customer collaboration over contract negotiation
  4. responding to change over following a plan
  • Scrump
  • eXtreme Programming
  • Crystal

  • toolbox of software engineering techniques이 bio informatics research, 자동화된 test, code reviews, user stories has been described에 사용된다.

15.4 Examples

Examples 15.1 Creating Your Own Module

  • 큰 파일은 읽기 어려우나 작은 여러 파일로 쪼개라.
  • 각 모듈들은 쓰기 쉽다.

  • Collect all functions for file parsing in one module.

  • Collect all functions for creating output in one module.
  • Create a module for constants like filenames and parameters.
  • Assign the paths of your data files to variable names in a separate module

In [7]:
!pwd


/Users/re4lfl0w/Documents/ipython/books/Managing_Your_Biological_Data_with_Python

In [1]:
!cat 10-functions/calc_distance.py



























In [2]:
import sys
sys.path.append('/Users/re4lfl0w/Documents/ipython/books/Managing_Your_Biological_Data_with_Python/10-functions/')
import calc_distance


10.3923048454

In [4]:
from pprint import pprint

In [5]:
pprint(sys.path)


['',
 '/Library/Python/2.7/site-packages/pip-1.4-py2.7.egg',
 '/Library/Python/2.7/site-packages/beautifulsoup4-4.2.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/numpy-1.9.0.dev_c50e60d-py2.7-macosx-10.8-x86_64.egg',
 '/Library/Python/2.7/site-packages/pandas-0.12.0_307_g3a2fe0b-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/pymc-2.2-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/scipy-0.14.0.dev_2a4ba40-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/readline-6.2.4.1-py2.7-macosx-10.7-intel.egg',
 '/Library/Python/2.7/site-packages/pyparsing-1.5.7-py2.7.egg',
 '/Library/Python/2.7/site-packages/tornado-3.1.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/pyzmq-13.1.0-py2.7-macosx-10.6-intel.egg',
 '/Library/Python/2.7/site-packages/pika-0.9.13-py2.7.egg',
 '/Library/Python/2.7/site-packages/patsy-0.2.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/Pygments-1.6-py2.7.egg',
 '/Library/Python/2.7/site-packages/Sphinx-1.2b1-py2.7.egg',
 '/Library/Python/2.7/site-packages/html5lib-1.0b3-py2.7.egg',
 '/Library/Python/2.7/site-packages/lxml-3.2.3-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/egenix_mx_experimental-3.0.0-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/twitter-1.10.2-py2.7.egg',
 '/Users/re4lfl0w/Documents/ipython_private/scripts',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/PIL',
 '/Library/Python/2.7/site-packages/IPython/extensions',
 '/Users/re4lfl0w/.ipython',
 '/Users/re4lfl0w/Documents/ipython_private/scripts/',
 '/Users/re4lfl0w/Documents/ipython/books/Managing_Your_Biological_Data_with_Python/10-functions/']

In [14]:
%%writefile tmp/neuron_count.py
#


Writing tmp/neuron_count.py

In [15]:
%%writefile tmp/shrink_images.py
#


Writing tmp/shrink_images.py

In [23]:
!ls -l tmp


total 24
-rw-r--r--  1 re4lfl0w  staff    0 Aug 25 04:40 __init__.py
-rw-r--r--  1 root      staff  102 Aug 25 04:40 __init__.pyc
-rw-r--r--  1 root      staff    1 Aug 26 17:31 neuron_count.py
-rw-r--r--  1 root      staff    1 Aug 26 17:31 shrink_images.py

In [24]:
!ls -l tmp2


total 24
-rw-r--r--  1 root  staff    0 Aug 26 17:31 __init__.py
-rw-r--r--  1 root  staff  103 Aug 26 17:32 __init__.pyc
-rw-r--r--  1 root  staff    1 Aug 26 17:31 neuron_count.py
-rw-r--r--  1 root  staff    1 Aug 26 17:31 shrink_images.py

In [20]:
!ls -l


total 6472
-rw-r--r--@  1 re4lfl0w  staff     6148 Aug 23 02:06 .DS_Store
drwxr-xr-x   6 root      staff      204 Aug 26 15:11 .ipynb_checkpoints
drwxr-xr-x   6 re4lfl0w  staff      204 Aug 23 00:48 01-the_Python_shell
drwxr-xr-x   7 re4lfl0w  staff      238 Aug 23 00:48 02-your_first_program
drwxr-xr-x  11 re4lfl0w  staff      374 Aug 23 00:48 03-data_columns
drwxr-xr-x  15 re4lfl0w  staff      510 Aug 23 00:48 04-parsing_data
drwxr-xr-x  12 re4lfl0w  staff      408 Aug 23 00:48 05-searching_data
drwxr-xr-x  18 re4lfl0w  staff      612 Aug 23 00:48 06-filtering_data
drwxr-xr-x  11 re4lfl0w  staff      374 Aug 23 00:48 07-tabular_data
drwxr-xr-x  12 re4lfl0w  staff      408 Aug 23 00:48 08-sorting_data
drwxr-xr-x  14 re4lfl0w  staff      476 Aug 23 00:48 09-pattern_matching
drwxr-xr-x  26 re4lfl0w  staff      884 Aug 26 17:25 10-functions
drwxr-xr-x   8 re4lfl0w  staff      272 Aug 23 00:48 11-classes
drwxr-xr-x   7 re4lfl0w  staff      238 Aug 26 16:23 12-debugging
drwxr-xr-x  10 re4lfl0w  staff      340 Aug 23 00:48 13-R_and_Python
drwxr-xr-x  10 re4lfl0w  staff      340 Aug 23 00:48 14-building_pipelines
drwxr-xr-x   8 re4lfl0w  staff      272 Aug 23 00:48 16-plotting_diagrams
drwxr-xr-x   9 re4lfl0w  staff      306 Aug 23 00:48 17-PyMOL
drwxr-xr-x  11 re4lfl0w  staff      374 Aug 23 00:48 18-image_manipulation
drwxr-xr-x  12 re4lfl0w  staff      408 Aug 23 00:48 19-sequences
drwxr-xr-x   8 re4lfl0w  staff      272 Aug 23 00:48 20-web_retrieval
drwxr-xr-x   9 re4lfl0w  staff      306 Aug 23 00:49 21-pdb_structures
-rw-r--r--   1 root      staff  3201368 Aug 26 08:40 ch10.ipynb
-rw-r--r--   1 root      staff    59324 Aug 26 17:10 ch12.ipynb
-rw-r--r--   1 root      staff    31242 Aug 26 17:31 ch15.ipynb
drwxr-xr-x  12 re4lfl0w  staff      408 Aug 26 17:15 images
-rw-r--r--   1 root      staff      331 Aug 25 09:35 test_my_range.py
-rw-r--r--   1 re4lfl0w  staff      747 Aug 25 05:09 test_my_range.pyc
drwxr-xr-x   6 re4lfl0w  staff      204 Aug 26 17:31 tmp
drwxr-xr-x   6 root      staff      204 Aug 26 17:31 tmp2

Example 15.2 Creating Your Own Package

  • package: folder 개념
  • group을 만들어서 package로 만들려면 같은 위치에 있으면 된다.
  • 또한 각 서브 디렉토리에 __init__.py 을 추가해줘야 한다. init.py는 empty 파일이면 된다.

In [22]:
# 디렉토리도 import 할 수 있다.
import tmp2

In [25]:
from tmp2 import neuron_count

In [26]:
from tmp2.shrink_images import *

In [29]:
!ls -l /Library/Python/2.7/site-packages/ | grep __init__.py

In [30]:
!ls -l /Library/Python/2.7/site-packages/sklearn | grep __init__.py


-rw-r--r--   1 root  wheel    2805 Dec 14  2013 __init__.py
-rw-r--r--   1 root  wheel    2821 Dec 14  2013 __init__.pyc

__init__.py 존재

  • site-packages에서도 이렇게 활용하고 있다.

Q&A: What does python do if modules import each other in a circular manner?

  • A imports B
  • B imports C
  • C imports A again
  • 복잡한 것이 문제가 될 수 있으니, 비순환 그래프를 그리고 명확하게 계층 구조를 보여준다.
  • Design Patterns

15.5 Testing yourself

Exercise 15.1 Data Types

Exercise 15.2 Prediction of Transmembrane Helices

Exercise 15.3 Create a Scaffolding

Exercise 15.4 Implement the Program

Exercise 15.5 Run pylint


In [1]:
l = range(10)
l


Out[1]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [2]:
from itertools import islice

In [4]:
list(islice(l, 3))


Out[4]:
[0, 1, 2]

In [5]:
islice??

In [6]:
from collections import deque

In [9]:
deque(l, maxlen=2)


Out[9]:
deque([8, 9], maxlen=2)

In [ ]: