/home_Python/apple_/Body/body.py

INDEX Python.use(better)

》作業中です《

#! /usr/bin/env python
# coding: utf-8
## ----------------------------------------
##
## (C) Copyright 2000-2009, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
##
## ----------------------------------------
# ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
"""
>>> None

>>> ## ----------------------------------------
>>> None
version: #1.0.28
"""
## ----------------------------------------
## ----------------------------------------
def step00():
    """open(file,"w")"""

    def body(file):
        s = open(file,"w")
        return s

    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step01():
    """s.write(...)"""

    def body(file):
        s = open(file,"w")
        try:
            for e in "ABC":
                s.write("%s - %d\n"%(e,ord(e)))
        finally:
            s.close()
        return s

    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step02():
    """open(file,"r")"""

    def body(file):
        with open(file,"w") as s:
            for e in "ABC":
                s.write("%s - %d\n"%(e,ord(e)))

        with open(file,"r") as s:
            for e in s:
                print(e,end="")

    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step03():
    """for i,e in enumerate(f):"""

    def body(file):
        with open(file,"w") as s:
            for e in "ABC":
                s.write("%s - %d\n"%(e,ord(e)))

        with open(file,"r") as s:
            for i,e in enumerate(s):
                n = i+1
                print("%d: %s"%(n,e),end="")

    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step04x():
    """f.readlines()"""

    def body(file):
        with open(file,"w") as s:
            for e in "ABCDEFGHIJ":
                s.write("%s - %d\n"%(e,ord(e)))

        with open(file,"r") as s:
            s = s.readlines()
            print("name=%r (%d lines)"%(file,len(s)))

            format = "%d: %s"
            for i,e in enumerate(s):
                n = i+1
                print(format%(n,e),end="")

    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step05():
    """print(format%(n,e),end="")"""

    def body(file):
        with open(file,"w") as s:
            for e in "ABCDEFGHIJ":
                s.write("%s - %d\n"%(e,ord(e)))

        with open(file,"r") as s:
            s = s.readlines()
            n = len(s)
            print("name=%r (%d lines)"%(file,n))

            format = "%%%dd: %%s"%len("%d"%n)
            for i,e in enumerate(s):
                n = i+1
                print(format%(n,e),end="")
        
    ## ----------------------------------------
    local = locals()
    ex_body0(local, "temp")

## ----------------------------------------
def step06():
    """def body(file):"""

    def body(file):
        with open(file,"r") as s:
            s = s.readlines()
            format = "%%%dd: %%s"%len("%d"%len(s))
            for i,e in enumerate(s):
                n = i+1
                print(format%(n,e),end="")

    ## ----------------------------------------
    local = locals()
    ex_body1(local, "temp")

## ----------------------------------------
def step07():
    """def body(file, end=None):"""

    def body(file, end=None):
        with open(file) as s:
            s = s.readlines()
            format = "%%%dd: %%s"%len("%d"%len(s))
            s = s[:end]
            for i,e in enumerate(s):
                n = i+1
                print(format%(n,e),end="")

    ## ----------------------------------------
    local = locals()
    ex_body2(local, "temp")

## ----------------------------------------
def step08():
    """def body(file, start=1, end=None):"""

    def body(file, start=1, end=None):
        with open(file) as s:
            s = s.readlines()
            format = "%%%dd: %%s"%len("%d"%len(s))
            s = s[start-1:end]
            for i,e in enumerate(s):
                n = start+i
                print(format%(n,e),end="")

    ## ----------------------------------------
    local = locals()
    ex_body3(local, "temp")

## ----------------------------------------
def step09():
    """print("".join(s),end="")"""

    def body(file, start=1, end=None):
        with open(file) as s:
            s = s.readlines()
            format = "%%%dd: %%s"%len("%d"%len(s))
            s = [format%(start+i,e)
                for i,e in enumerate(s[start-1:end])]
            print("".join(s),end="")

    ## ----------------------------------------
    local = locals()
    ex_body3(local, "temp")

## ----------------------------------------
'''
>>> # -------------------------------------------------- step_body
>>> path
'/Applications/Python 3.1/Extras/Demo/scripts/queens.py'
>>> ex_body(path, end=5)
 1: #! /usr/bin/env python
 2: 
 3: """N queens problem.
 4: 
 5: The (well-known) problem is due to Niklaus Wirth.

>>> ex_body(path, start=81)
81:     q.solve()
82:     print("Found", q.nfound, "solutions.")
83: 
84: if __name__ == "__main__":
85:     main()

>>> ex_body(path, 14, 18)
14: class Queens:
15: 
16:     def __init__(self, n=N):
17:         self.n = n
18:         self.reset()

>>> 
'''
## ----------------------------------------
def tips_body():
    """def body(name, start=1, end=None, number=True):"""

####    def body(name, start=1, end=None, number=True):
####        s = open(name).readlines(); N = len(s)
####        if not end: end = N+1 
####        if number:
####            for n, e in enumerate(s):
####                s[n] = "{{0:{0}d: {1}}}".format(len(str(N))%(n+1, e))      
####        return s[start-1:end]

    def body(name, start=1, end=None):
        seq, n = _body(name, start, end)
        s = "{{0:{0:d}d}}: {{1}}".format(len(str(n)))
        return [s.format(start+i, e) for i, e in enumerate(seq)]

    def _body(name, start, end):
        s = open(name).readlines(); N = len(s)
        if not end: end = N+1
        return s[start-1:end], N

    ## ----------------------------------------
    def ex_body(name, start=1, end=None):
        for e in body(name, start, end):
            print(e,end="")
        print()

    path = "/Applications/Python 3.1/Extras/Demo/scripts/"
    path += "queens.py"

    def ex(local):                              #@:
        source = '''
path
ex_body(path, end=5)
ex_body(path, start=81)
ex_body(path, 14, 18)
'''.split("\n")

        for e in source[1:-1]:
            print_(e, local)

    local = locals(); ex(local)

## ----------------------------------------
## ----------------------------------------
def ex_body0(local, n):
    X = 'body({0!r})'.format(n)
    print_(X, local)

def ex_body(file, seq):
    with open(file,"w") as s:
        for e in seq:
            s.write("%s - %d\n"%(e,ord(e)))

def ex_body1(local, file):
    ex_body(file, "ABCDEFGHIJ")
    X = 'body({0!r})'.format(file)
    print_(X, local)

def ex_body2(local, n):
    ex_body1(local, n)

    source = '''
body({0!r},end=3)
'''.split("\n")

    for e in source[1:-1]:
        print_(e.format(n), local)

def ex_body3(local, n):
    ex_body2(local, n)

    source = '''
body({0!r},start=8)
body({0!r},start=3,end=5)
body({0!r},end=8,start=6)
'''.split("\n")

    for e in source[1:-1]:
        print_(e.format(n), local)

## ----------------------------------------
def print_(source, local, mode="single"):
    print(">>>",source)
    eval(compile(source,"",mode),globals(),local)

## ========================================
s = "step"
DIR = [e for e in dir() if e.startswith(s)]
DIR = dict((i,e) for i,e in enumerate(DIR))
DIR = list(DIR.items()); DIR.sort()

def ex():
    n = (len(DIR)-1)//10+1
    for k,v in DIR:
        source = '''
x = eval("%%s.__doc__"%%v)
print("%%%dd: %%s -- %%s"%%(k,v,x))
'''%n
        eval(compile(source,"","exec"))

def do_all():
    for k,v in DIR: do(k)

def do(k=None):
    if k is None: ex(); return
    try:
        k,v = DIR[k]
        print(">>> #","-"*50,v)
        eval("%s()"%v)
    except IndexError:
        print("(x_x) too bad ... %s"%k)

## ----------------------------------------
from time import ctime
from sys import argv, version

def inform():
    n = 60
    print("="*n)
    print("Version: %s"%version.split("\n")[0])
    print("Module : %s"%argv[0].split("/")[-1])
    print("Date   : %s"%ctime())
    print("="*n)

## ----------------------------------------
from doctest import testmod

if __name__=='__main__':
    inform()
    testmod()

## ========================================

Last updated♪2010/03/13