2013年1月29日火曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の7章(サブルーチンへのリファレンス), 7.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 File::Find;
use Time::Local;

my $target_dow = 2;
my @starting_directories = (".");

my $seconds_per_day = 24 * 60 * 60;
my($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime;
my $start = timelocal(0, 0, 0, $day, $mon, $yr);
while ($dow != $target_dow) {
    $start -= $seconds_per_day;
    $dow += 7 if --$dow < 0;
}

my $stop = $start + $seconds_per_day;
my($gather, $yield) = gather_mtime_between($start, $stop);
find($gather, @starting_directories);
my @files = $yield->();

for (@files) {
    my $mtime = (stat $_)[9];
    my $when = localtime $mtime;
    print "$when: $_\n";
}

sub gather_mtime_between{
    my($start, $stop) = @_;
    my @files;
    my $gather = sub {
        my $timestamp = (stat $_)[9];
        unless (defined $timestamp) {
            warn $!;
            return;
        }
        push @files, $File::Find::name if $timestamp >= $start and $timestamp <= $stop;
    };
    my $yield = sub {
        @files;
    };
    ($gather, $yield);
}

入出力結果(Terminal)

$ ./sample.pl
Too many levels of symbolic links at ./sample.pl line 39.
Tue Jan 29 12:13:00 2013: .
Tue Jan 29 14:09:40 2013: ./perl_kamimura_blog.html
Tue Jan 29 12:30:49 2013: ./sample.pl
$

pythonの場合。

sample.py

コード(BBEdit)

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

import glob, time, os, datetime

target_dow = 1
starting_directories = glob.glob("*")

seconds_per_day = 24 * 60 * 60

now = datetime.datetime.now()
start = datetime.datetime(now.year, now.month, now.day, 0, 0, 0)
while start.weekday() != target_dow:
    start = start.replace(day = start.day - 1)

stop = start.replace(day = start.day + 1)

def gather_mtime_between(start, stop):
    files = []
    def gather(o):
        for file in o:
            if os.path.isfile(file):
                d = datetime.datetime.fromtimestamp(os.stat(file).st_mtime)
                if d >= start and d <= stop:
                    files.append(os.path.relpath(file))
    def f():
        return files
    return (gather, f)

gather, f = gather_mtime_between(start, stop)
gather(starting_directories)
files = f()
for file in files:
    when = datetime.datetime.fromtimestamp(os.stat(file).st_mtime)
    print("{0}: {1}".format(when, file))

入出力結果(Terminal)

$ ./sample.py
2013-01-29 14:12:39: perl_kamimura_blog.html
2013-01-29 12:30:49: sample.pl
2013-01-29 15:54:07: sample.py
$

ちょっとperlの場合と結果が違うけど、良しとしてとりあえず次に進むことに。

0 コメント:

コメントを投稿