2014年6月23日月曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 7(Web Development: Putting it all together)、EXERCISE(p.223)を解いてみる。

EXERCISE(p.223)

コード(BBEdit)

athletemodel.py

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

import pickle
import athletes

def put_to_score(file_list):
    all_athletes = {}
    for filename in file_list:
        athlete = athletes.getCoachData(filename)
        all_athletes[athlete.name] = athlete

    try:
        with open('athletes.pickle', 'wb') as f:
            pickle.dump(all_athletes, f)
    except IOError as err:
        print('File error: {0}'.format(err))
        print(type(err), err, err.args)
    except Except as err:
        print(type(err), err, err.args)

    return (all_athletes)
        
def get_from_store():
    all_athletes = {}
    try:
        with open('athletes.pickle', 'rb') as f:
            all_athletes = pickle.load(f)
    except IOError as err:
        print('File error: {0}'.format(err))
        print(type(err), err, err.args)
    except Except as err:
        print(type(err), err, err.args)

    return (all_athletes)

if __name__ == '__main__':
    file_list = ['james2.txt', 'julie2.txt', 'mikey2.txt', 'sarah2.txt']
    put_to_score(file_list)
    print(get_from_store())

入出力結果(Terminal)

$ ./athletemodel.py 
{'James Lee': ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22', '2-01', '2.01', '2:16'], 'Julie Jones': ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21', '3.01', '3.02', '2:59'], 'Mikey McManus': ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38', '2:40', '2.22', '2-31'], 'Sarah Sweeney': ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55', '2:22', '2-21', '2.22']}
$ ls *.txt
james2.txt julie2.txt mikey2.txt sarah2.txt
$

0 コメント:

コメントを投稿