2014年5月28日水曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media; )のChapter 2(Sharing your Code: Modules of functions)、EXERCISE(p.67)を解いてみる。

EXERCISE(p.67)

コード(BBEdit)

kpnester.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

"""
This is the nester.py module for nested (nested...) list.
function(s)
    printLoL(list)
"""

def printLoL(the_list, indent=False, level=0):
    """
    print each data in the list( and nested list) print each line
    """
    for each_item in the_list:
        if isinstance(each_item, list):
            printLoL(each_item, indent, level+1)
        else:
            if indent:
                for tag_stop in range(level):
                    print('\t', end='')
            print(each_item)

setup.py

#!/usr/bin/env python3
#-*- coding: utf-8

from distutils.core import setup

setup(
    name = 'kpnester',
    version = '1.1.0',
    py_modules = ['kpnester'],
    author = 'kamimura',
    author_email = 'kamimura@live.jp',
    url = 'http://sitekamimura.blogspot.com',
    description = 'A simple printer of nested lists',
    )

test_sample67.py

#!/usr/bin/env python3
#-*- coding: utf-8

import kpnester

l = ['a', ['b', 'c', ['d', 'e'], ['f', 'g']], ['h', 'i']]

kpnester.printLoL(l)
print('*' * 50)
kpnester.printLoL(l, indent=True)
print('*' * 50)
kpnester.printLoL(l, level=1)
print('*' * 50)
kpnester.printLoL(l, indent=True, level=1)
print('*' * 50)
kpnester.printLoL(l, True, 2)

入出力結果(Terminal)

$ python3 setup.py sdist register
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating kpnester-1.1.0
making hard links in kpnester-1.1.0...
hard linking kpnester.py -> kpnester-1.1.0
hard linking setup.py -> kpnester-1.1.0
Creating tar archive
removing 'kpnester-1.1.0' (and everything under it)
running register
Registering kpnester to https://pypi.python.org/pypi
Server response (200): OK
$ ./test_sample67.py 
a
b
c
d
e
f
g
h
i
**************************************************
a
 b
 c
  d
  e
  f
  g
 h
 i
**************************************************
a
b
c
d
e
f
g
h
i
**************************************************
 a
  b
  c
   d
   e
   f
   g
  h
  i
**************************************************
  a
   b
   c
    d
    e
    f
    g
   h
   i
$

0 コメント:

コメントを投稿