2013年1月27日日曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の6章(複雑なデータ構造の操作), 6.8(練習問題)1を解いてみる。

その他参考書籍

1.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use Storable;
use Data::Dumper;

my %total_bytes;
my $file = "coconet_total.dat";
my $all = "all";
if (-e $file) {
    my $dat =  retrieve $file;
    %total_bytes = %$dat;
}
while (<>){
  next if /^#/;
  my ($source, $destination, $bytes) = split;
  $total_bytes{$source}{$destination} += $bytes;
  $total_bytes{$source}{$all} += $bytes;
}

store \%total_bytes, $file;

for my $source (sort { 
        $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all}
        } keys %total_bytes){
    printf "%-34s%10d bytes\n",
        $source, $total_bytes{$source}{$all};
    for my $destination ( sort {
            $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a}
            } keys %{ $total_bytes{$source} }) {
        next if $destination eq $all;
        printf "     => %-25s" .
          " %10d bytes\n",
          $destination, $total_bytes{$source}{$destination};
    }
    print "\n";
}

入出力結果(Terminal)

$ ./sample.pl coconet.dat
lovey.howell.hut                     1139833 bytes
     => laser3.copyroom.hut           212456 bytes
     => professor.hut                 175065 bytes
     => ginger.girl.hut               158084 bytes
     => maryann.girl.hut              155080 bytes
     => thurston.howell.hut           141790 bytes
     => skipper.crew.hut              119766 bytes
     => fileserver.copyroom.hut        98926 bytes
     => gilligan.crew.hut              78666 bytes

gilligan.crew.hut                     983860 bytes
     => laser3.copyroom.hut           240327 bytes
     => maryann.girl.hut              145361 bytes
     => skipper.crew.hut              118827 bytes
     => lovey.howell.hut              107152 bytes
     => professor.hut                 101219 bytes
     => thurston.howell.hut            97398 bytes
     => fileserver.copyroom.hut        90369 bytes
     => ginger.girl.hut                83207 bytes

ginger.girl.hut                       968495 bytes
     => laser3.copyroom.hut           179803 bytes
     => lovey.howell.hut              170948 bytes
     => maryann.girl.hut              152634 bytes
     => professor.hut                 110300 bytes
     => thurston.howell.hut           105940 bytes
     => skipper.crew.hut              103592 bytes
     => fileserver.copyroom.hut        79551 bytes
     => gilligan.crew.hut              65727 bytes

maryann.girl.hut                      846806 bytes
     => laser3.copyroom.hut           161843 bytes
     => lovey.howell.hut              145271 bytes
     => ginger.girl.hut               126902 bytes
     => thurston.howell.hut           123456 bytes
     => professor.hut                 109526 bytes
     => skipper.crew.hut               75155 bytes
     => fileserver.copyroom.hut        63807 bytes
     => gilligan.crew.hut              40846 bytes

professor.hut                         821931 bytes
     => laser3.copyroom.hut           172882 bytes
     => lovey.howell.hut              123396 bytes
     => ginger.girl.hut               113685 bytes
     => maryann.girl.hut               94786 bytes
     => thurston.howell.hut            94363 bytes
     => skipper.crew.hut               86642 bytes
     => gilligan.crew.hut              82825 bytes
     => fileserver.copyroom.hut        53352 bytes

fileserver.copyroom.hut               770174 bytes
     => lovey.howell.hut              160986 bytes
     => professor.hut                 146975 bytes
     => ginger.girl.hut               103478 bytes
     => maryann.girl.hut               99261 bytes
     => skipper.crew.hut               88261 bytes
     => thurston.howell.hut            86084 bytes
     => gilligan.crew.hut              85129 bytes

thurston.howell.hut                   619077 bytes
     => laser3.copyroom.hut           129628 bytes
     => maryann.girl.hut              105137 bytes
     => lovey.howell.hut               82972 bytes
     => professor.hut                  78576 bytes
     => skipper.crew.hut               66538 bytes
     => ginger.girl.hut                58580 bytes
     => gilligan.crew.hut              56658 bytes
     => fileserver.copyroom.hut        40988 bytes

skipper.crew.hut                      481951 bytes
     => professor.hut                 108779 bytes
     => laser3.copyroom.hut            89505 bytes
     => ginger.girl.hut                77621 bytes
     => maryann.girl.hut               71880 bytes
     => lovey.howell.hut               43427 bytes
     => thurston.howell.hut            41726 bytes
     => gilligan.crew.hut              29142 bytes
     => fileserver.copyroom.hut        19871 bytes

$ ./sample.pl coconet.dat
lovey.howell.hut                     2279666 bytes
     => laser3.copyroom.hut           424912 bytes
     => professor.hut                 350130 bytes
     => ginger.girl.hut               316168 bytes
     => maryann.girl.hut              310160 bytes
     => thurston.howell.hut           283580 bytes
     => skipper.crew.hut              239532 bytes
     => fileserver.copyroom.hut       197852 bytes
     => gilligan.crew.hut             157332 bytes

gilligan.crew.hut                    1967720 bytes
     => laser3.copyroom.hut           480654 bytes
     => maryann.girl.hut              290722 bytes
     => skipper.crew.hut              237654 bytes
     => lovey.howell.hut              214304 bytes
     => professor.hut                 202438 bytes
     => thurston.howell.hut           194796 bytes
     => fileserver.copyroom.hut       180738 bytes
     => ginger.girl.hut               166414 bytes

ginger.girl.hut                      1936990 bytes
     => laser3.copyroom.hut           359606 bytes
     => lovey.howell.hut              341896 bytes
     => maryann.girl.hut              305268 bytes
     => professor.hut                 220600 bytes
     => thurston.howell.hut           211880 bytes
     => skipper.crew.hut              207184 bytes
     => fileserver.copyroom.hut       159102 bytes
     => gilligan.crew.hut             131454 bytes

maryann.girl.hut                     1693612 bytes
     => laser3.copyroom.hut           323686 bytes
     => lovey.howell.hut              290542 bytes
     => ginger.girl.hut               253804 bytes
     => thurston.howell.hut           246912 bytes
     => professor.hut                 219052 bytes
     => skipper.crew.hut              150310 bytes
     => fileserver.copyroom.hut       127614 bytes
     => gilligan.crew.hut              81692 bytes

professor.hut                        1643862 bytes
     => laser3.copyroom.hut           345764 bytes
     => lovey.howell.hut              246792 bytes
     => ginger.girl.hut               227370 bytes
     => maryann.girl.hut              189572 bytes
     => thurston.howell.hut           188726 bytes
     => skipper.crew.hut              173284 bytes
     => gilligan.crew.hut             165650 bytes
     => fileserver.copyroom.hut       106704 bytes

fileserver.copyroom.hut              1540348 bytes
     => lovey.howell.hut              321972 bytes
     => professor.hut                 293950 bytes
     => ginger.girl.hut               206956 bytes
     => maryann.girl.hut              198522 bytes
     => skipper.crew.hut              176522 bytes
     => thurston.howell.hut           172168 bytes
     => gilligan.crew.hut             170258 bytes

thurston.howell.hut                  1238154 bytes
     => laser3.copyroom.hut           259256 bytes
     => maryann.girl.hut              210274 bytes
     => lovey.howell.hut              165944 bytes
     => professor.hut                 157152 bytes
     => skipper.crew.hut              133076 bytes
     => ginger.girl.hut               117160 bytes
     => gilligan.crew.hut             113316 bytes
     => fileserver.copyroom.hut        81976 bytes

skipper.crew.hut                      963902 bytes
     => professor.hut                 217558 bytes
     => laser3.copyroom.hut           179010 bytes
     => ginger.girl.hut               155242 bytes
     => maryann.girl.hut              143760 bytes
     => lovey.howell.hut               86854 bytes
     => thurston.howell.hut            83452 bytes
     => gilligan.crew.hut              58284 bytes
     => fileserver.copyroom.hut        39742 bytes


pythonの場合。

sample.py

コード(BBEdit)

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

import sys, re, pickle, os

file_name = 'coconet_total_2.dat'
total_bytes = {}
if os.path.isfile(file_name):
    with open(file_name, 'rb') as f:
        total_bytes = pickle.load(f)

for line in open(sys.argv[1]):
    if re.search(r"^#", line): continue
    source, destination, bytes = line.split()
    bytes = int(bytes)
    if source in total_bytes.keys():
        if destination in total_bytes[source].keys():
            total_bytes[source][destination] += bytes
        else:
            total_bytes[source][destination] = bytes
        total_bytes[source]["all"] += bytes
    else:
        total_bytes[source] = {}
        total_bytes[source][destination] = bytes
        total_bytes[source]["all"] = bytes

with open(file_name, 'wb') as f:
    pickle.dump(total_bytes, f)

for source in sorted(total_bytes.keys(), key=lambda x: -total_bytes[x]["all"]):
    print("{0:34s}{1:10d} bytes".format(
        source, total_bytes[source]["all"]
    ));
    for destination in sorted( total_bytes[source].keys(), 
        key=lambda x: -total_bytes[source][x]):
        if destination == "all": continue
        print("     => {0:25} {1:10} bytes".format(
            destination, total_bytes[source][destination]
        ));
    print()

入出力結果(Terminal)

$ ./sample.py coconet.dat
lovey.howell.hut                     1139833 bytes
     => laser3.copyroom.hut           212456 bytes
     => professor.hut                 175065 bytes
     => ginger.girl.hut               158084 bytes
     => maryann.girl.hut              155080 bytes
     => thurston.howell.hut           141790 bytes
     => skipper.crew.hut              119766 bytes
     => fileserver.copyroom.hut        98926 bytes
     => gilligan.crew.hut              78666 bytes

gilligan.crew.hut                     983860 bytes
     => laser3.copyroom.hut           240327 bytes
     => maryann.girl.hut              145361 bytes
     => skipper.crew.hut              118827 bytes
     => lovey.howell.hut              107152 bytes
     => professor.hut                 101219 bytes
     => thurston.howell.hut            97398 bytes
     => fileserver.copyroom.hut        90369 bytes
     => ginger.girl.hut                83207 bytes

ginger.girl.hut                       968495 bytes
     => laser3.copyroom.hut           179803 bytes
     => lovey.howell.hut              170948 bytes
     => maryann.girl.hut              152634 bytes
     => professor.hut                 110300 bytes
     => thurston.howell.hut           105940 bytes
     => skipper.crew.hut              103592 bytes
     => fileserver.copyroom.hut        79551 bytes
     => gilligan.crew.hut              65727 bytes

maryann.girl.hut                      846806 bytes
     => laser3.copyroom.hut           161843 bytes
     => lovey.howell.hut              145271 bytes
     => ginger.girl.hut               126902 bytes
     => thurston.howell.hut           123456 bytes
     => professor.hut                 109526 bytes
     => skipper.crew.hut               75155 bytes
     => fileserver.copyroom.hut        63807 bytes
     => gilligan.crew.hut              40846 bytes

professor.hut                         821931 bytes
     => laser3.copyroom.hut           172882 bytes
     => lovey.howell.hut              123396 bytes
     => ginger.girl.hut               113685 bytes
     => maryann.girl.hut               94786 bytes
     => thurston.howell.hut            94363 bytes
     => skipper.crew.hut               86642 bytes
     => gilligan.crew.hut              82825 bytes
     => fileserver.copyroom.hut        53352 bytes

fileserver.copyroom.hut               770174 bytes
     => lovey.howell.hut              160986 bytes
     => professor.hut                 146975 bytes
     => ginger.girl.hut               103478 bytes
     => maryann.girl.hut               99261 bytes
     => skipper.crew.hut               88261 bytes
     => thurston.howell.hut            86084 bytes
     => gilligan.crew.hut              85129 bytes

thurston.howell.hut                   619077 bytes
     => laser3.copyroom.hut           129628 bytes
     => maryann.girl.hut              105137 bytes
     => lovey.howell.hut               82972 bytes
     => professor.hut                  78576 bytes
     => skipper.crew.hut               66538 bytes
     => ginger.girl.hut                58580 bytes
     => gilligan.crew.hut              56658 bytes
     => fileserver.copyroom.hut        40988 bytes

skipper.crew.hut                      481951 bytes
     => professor.hut                 108779 bytes
     => laser3.copyroom.hut            89505 bytes
     => ginger.girl.hut                77621 bytes
     => maryann.girl.hut               71880 bytes
     => lovey.howell.hut               43427 bytes
     => thurston.howell.hut            41726 bytes
     => gilligan.crew.hut              29142 bytes
     => fileserver.copyroom.hut        19871 bytes

$ ./sample.py coconet.dat
lovey.howell.hut                     2279666 bytes
     => laser3.copyroom.hut           424912 bytes
     => professor.hut                 350130 bytes
     => ginger.girl.hut               316168 bytes
     => maryann.girl.hut              310160 bytes
     => thurston.howell.hut           283580 bytes
     => skipper.crew.hut              239532 bytes
     => fileserver.copyroom.hut       197852 bytes
     => gilligan.crew.hut             157332 bytes

gilligan.crew.hut                    1967720 bytes
     => laser3.copyroom.hut           480654 bytes
     => maryann.girl.hut              290722 bytes
     => skipper.crew.hut              237654 bytes
     => lovey.howell.hut              214304 bytes
     => professor.hut                 202438 bytes
     => thurston.howell.hut           194796 bytes
     => fileserver.copyroom.hut       180738 bytes
     => ginger.girl.hut               166414 bytes

ginger.girl.hut                      1936990 bytes
     => laser3.copyroom.hut           359606 bytes
     => lovey.howell.hut              341896 bytes
     => maryann.girl.hut              305268 bytes
     => professor.hut                 220600 bytes
     => thurston.howell.hut           211880 bytes
     => skipper.crew.hut              207184 bytes
     => fileserver.copyroom.hut       159102 bytes
     => gilligan.crew.hut             131454 bytes

maryann.girl.hut                     1693612 bytes
     => laser3.copyroom.hut           323686 bytes
     => lovey.howell.hut              290542 bytes
     => ginger.girl.hut               253804 bytes
     => thurston.howell.hut           246912 bytes
     => professor.hut                 219052 bytes
     => skipper.crew.hut              150310 bytes
     => fileserver.copyroom.hut       127614 bytes
     => gilligan.crew.hut              81692 bytes

professor.hut                        1643862 bytes
     => laser3.copyroom.hut           345764 bytes
     => lovey.howell.hut              246792 bytes
     => ginger.girl.hut               227370 bytes
     => maryann.girl.hut              189572 bytes
     => thurston.howell.hut           188726 bytes
     => skipper.crew.hut              173284 bytes
     => gilligan.crew.hut             165650 bytes
     => fileserver.copyroom.hut       106704 bytes

fileserver.copyroom.hut              1540348 bytes
     => lovey.howell.hut              321972 bytes
     => professor.hut                 293950 bytes
     => ginger.girl.hut               206956 bytes
     => maryann.girl.hut              198522 bytes
     => skipper.crew.hut              176522 bytes
     => thurston.howell.hut           172168 bytes
     => gilligan.crew.hut             170258 bytes

thurston.howell.hut                  1238154 bytes
     => laser3.copyroom.hut           259256 bytes
     => maryann.girl.hut              210274 bytes
     => lovey.howell.hut              165944 bytes
     => professor.hut                 157152 bytes
     => skipper.crew.hut              133076 bytes
     => ginger.girl.hut               117160 bytes
     => gilligan.crew.hut             113316 bytes
     => fileserver.copyroom.hut        81976 bytes

skipper.crew.hut                      963902 bytes
     => professor.hut                 217558 bytes
     => laser3.copyroom.hut           179010 bytes
     => ginger.girl.hut               155242 bytes
     => maryann.girl.hut              143760 bytes
     => lovey.howell.hut               86854 bytes
     => thurston.howell.hut            83452 bytes
     => gilligan.crew.hut              58284 bytes
     => fileserver.copyroom.hut        39742 bytes

$ ls -l coconet*
-rw-r--r--@ 1 kamimura  staff  88502 10 24  2011 coconet.dat
-rw-r--r--  1 kamimura  staff   2264  1 27 16:20 coconet_total.dat
-rw-r--r--  1 kamimura  staff   2133  1 27 17:01 coconet_total_2.dat
$

0 コメント:

コメントを投稿