2014年7月24日木曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 11(Dealing with Complexity: Data wrangling)、EXERCISE(p.419)を解いてみる。

EXERCISE(p.419)

コード(BBEdit, Emacs)

sample419.py

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

from find_it import find_closest
from tm2secs2tm import time2secs, secs2time

row_data = {}
with open('PaceData.csv') as paces:
    column_headings = paces.readline().strip().split(',')
    column_headings.pop(0)
    for each_line in paces:
        row = each_line.strip().split(',')
        row_label = row.pop(0)
        inner_dict = {}
        for i in range(len(column_headings)):
            inner_dict[row[i]] = column_headings[i]
        row_data[row_label] = inner_dict

def find_nearest_time(look_for, target_data):
    secs = time2secs(look_for)
    data = [time2secs(t) for t in target_data]
    secs = find_closest(secs, data)
    return secs2time(secs)

if __name__ == '__main__':
    target_data = list(row_data['10k'])
    print(target_data)
    while True:
        try:
            look_for = input('時間を入力(HH:MM:SS) ')
            if look_for == '':
                break
            print(find_nearest_time(look_for, target_data))
        except Exception as err:
            print(type(err), err, err.args)

入出力結果(Terminal, IPython)

$ ./sample419.py
['56:04', '28:45', '37:34', '30:45', '1:16:34', '1:01:17', '58:37', '35:56', '39:16', '46:55', '50:10', '45:54', '35:08', '27:30', '1:13:14', '38:24', '1:08:30', '36:44', '1:05:31', '44:53', '47:59', '57:20', '31:26', '1:18:17', '1:11:37', '1:02:40', '34:22', '51:18', '33:36', '29:24', '41:59', '41:04', '1:20:03', '40:09', '1:07:00', '54:50', '30:04', '28:08', '26:54', '1:10:03', '53:38', '59:56', '49:04', '42:56', '43:54', '1:04:05', '32:52', '1:14:53', '52:27', '32:09']
時間を入力(HH:MM:SS) 1:00:00
00:59:56
時間を入力(HH:MM:SS) 01:00:00
00:59:56
時間を入力(HH:MM:SS) 1:30:00
01:20:03
時間を入力(HH:MM:SS) 1:00:30
00:59:56
時間を入力(HH:MM:SS) 1:00:40
01:01:17
時間を入力(HH:MM:SS) 0:50:00
00:50:10
時間を入力(HH:MM:SS) 0:50:30
00:50:10
時間を入力(HH:MM:SS) 0:50:40
00:50:10
時間を入力(HH:MM:SS) 0:50:50
00:51:18
時間を入力(HH:MM:SS) 1:20:30
01:20:03
時間を入力(HH:MM:SS) 
$

0 コメント:

コメントを投稿