HOME | Python | ���� |
��ƣ��Ĺǯ������ץȸ���Ȥ��� awk �� Lisp ��ȤäƤ��ޤ����� Perl �Ȥ�����Τ����ꡢ����ϥ֡���ˤʤäƤ����Τ��ΤäƤ����ΤǤ����� �ɤ���ʤ���ʤ��ä��Τǡ��ۤȤ�ɻȤ��ޤ���Ǥ����� �ǶᡢPython �� Ruby �Ȥ��ä�������Υ�����ץȸ��줬�����Ȥ���褦�ˤʤä��Τǡ� �������Ƥߤ뤳�Ȥˤ��ޤ�����
���ߤ� Python ��ȤäƤ���Τǡ�Python ������ӤˤʤäƤ��뤳�Ȥ�ǰƬ�ˤ������ɤ�Ǥ��������� �ޤ�������ʸ�����ȽŪ�ʰո��⤢��Τǡ� ������⻲�ͤˤ��Ƥ���������
����������Ҳ𤷤� ��ǥ����ˤ�������ե������ϡ��ɥǥ���������¸���륹����ץȤǤ��� �¤ϲ椬�ȤΥѥ�����ϤۤȤ�ɥ���Х�Ȳ����Ƥ��ꡢ��ǥ�������ϡ��ɥǥ������˰ܤ�������ץȤ� FireFox �ʳ��Ǥϻ�β�²�˰������Ѥ���Ƥ���ץ������Ǥ���
01: #! perl 02: 03: use strict; 04: use File::Copy; 05: use File::Compare; 06: use File::Find; 07: use Cwd; 08: 09: ## global parameters 10: my $DOC_DIR = 'D:/doc'; 11: my $MEDIA ='G:/'; 12: my $PHOTO_VIEWER = 'D:/WBIN/linar160/linar.exe'; 13: 14: # getting the string of "year(NN)-month(NN)" 15: sub get_year_month{ 16: my ($m, $y) = (localtime)[4,5]; 17: sprintf ("%02d-%02d", $y-100, $m + 1); 18: } 19: 20: # getting the starting number of photoNN, the directory in which photos should be saved 21: # This function should be called when the program is in the month directory. 22: sub get_first_photo_dir_number{ 23: my @pdirs = (glob "photo[0-9][0-9]"); 24: @pdirs ? 1+ substr($pdirs[-1], -2) : 1; 25: } 26: 27: #move into the directory "$DOC_DIR/y-m" 28: sub move_into_dir_of_month{ 29: my $dir_of_month = &get_year_month; 30: unless ($DOC_DIR eq cwd){ 31: chdir $DOC_DIR or die "Cannot move to $DOC_DIR: $!"; 32: } 33: unless (-d $dir_of_month){ 34: mkdir $dir_of_month or die "cannot create $dir_of_month: $!"; 35: } 36: chdir $dir_of_month or die "Cannot move to $dir_of_month: $!"; 37: "$DOC_DIR/$dir_of_month" ; 38: } 39: 40: #archive photos in the media into the HD 41: # This function should be called when the program is in the month directory. 42: sub archive_photos{ 43: my $photo_dir_number = shift; 44: my %dhash; 45: find({ 46: wanted => sub{push @{$dhash{$File::Find::dir}}, $_ if -f}, 47: }, $MEDIA); 48: for my $dir_from (sort keys %dhash){ 49: my $n = @{$dhash{$dir_from}}; 50: my $i = 0; 51: my $dir_to = sprintf("photo%02d", $photo_dir_number++); 52: mkdir $dir_to or die "cannot create $dir_to: $!"; 53: print "\n$dir_from ==> $dir_to\n"; 54: for my $fname (@{$dhash{$dir_from}}){ 55: my $copy_from = "$dir_from/$fname"; 56: my $copy_to = "$dir_to/$fname"; 57: copy($copy_from, $copy_to) or die "cannot make a copy for $copy_from: $!"; 58: if(0 == compare($copy_from, $copy_to)){ 59: unlink $copy_from; 60: print ++$i, "/$n\r"; 61: }else{ 62: die "an error occurs during coping $copy_from"; 63: } 64: } 65: } 66: %dhash; 67: } 68: 69: #main 70: my $dir_of_month = &move_into_dir_of_month; 71: my $first_photo_dir_number = &get_first_photo_dir_number; 72: if(&archive_photos($first_photo_dir_number)){ 73: exec (sprintf "%s %s/photo%02d", $PHOTO_VIEWER, $dir_of_month, $first_photo_dir_number); 74: }else{ 75: print "No photos in the media!\nGive Return:"; 76: < STDIN> 77: }���� 80 �Ԥ��餤�Υ����ɤˤʤ�ޤ��� �ꥹ�Ȥ� Perl �Υϥå���ɽ���ͤˤϤʤ�ʤ��Τǥꥹ�Ȥ��ͤȤ�������С� �ꥹ�ȤΥ�ե����Ȥ�ɬ�פ�����ޤ��� ���ݤȤ��Ƥϡ�awk �� sed ������Τ�Ŭ���ϰϳ��ޤdz�ĥ���������Ȥ��������Ǥ��� ���������Υ���ߤ����ʴ����ǡ����ޤ����� ���������饤�֥���ͥ���ǡ��¹�®�٤�®���Ǥ���
���� python �� ruby ������Τǡ����ޤ��鿷���� perl ��ؤ�ɬ�פ�̵���Ȼפ��ޤ���
������ץȸ���Ȥ��Ƥη������ȡ�
���줫�顢Python �ˤ�
���å⡼�ɤ����ꡢ�ġ��δؿ���ƥ��ȤǤ��ޤ���(��1��
���ε�ǽ���礭��Υץ�������Ȥ��������Ǥ���
main ������������ʬ��
if __name__=='__main__': �Υ֥��å�������뤳�Ȥˤ�äơ�
Python �ˤ�ä�ľ���ɤ߹��ޤ줿�Ȥ��ʳ���ư��ʤ��褦�ˤ��뤳�Ȥ�����ޤ�������2�� 3.2. ���� Python
Python �ǽȼ��Τ褦�ˤʤ�ޤ������ǤΤϤ����Ƥʤ��Τǽ�ľ���ޤ�����
001: #! /usr/bin/env python
002:
003: r"""
004: script to achive photos in the removal media into HD
005: by T.Shido
006: June 26, 2007
007: """
008:
009: import os, os.path, filecmp, shutil, re, sys, operator
010: from datetime import date
011:
012: # global parameters
013: HD = '/home/pub/photos/'
014: MEDIA = '/media/usbdisk/'
015:
016: REG_FILE = re.compile(r"\.(gif|bmp|jpe?g|tiff?|wav|mov)$", re.I)
017: REG_DIR = re.compile(r'^photo[0-9][0-9]$')
018:
019: def n_photo_dir(d):
020: """return the max NN of photoNN in directory of the month"""
021:
022: return \
023: reduce(max, \
024: [ int(x[-2:]) for x in os.listdir(d) \
025: if (os.path.isdir(os.path.join(d,x)) and REG_DIR.match(x))], \
026: 0)
027:
028: def search_media(d):
029: """search Media and returns a hash
030: whose keys are directory name and the values are lists of photo files."""
031: def search_sub(d):
032: os.chdir(d)
033: ls_d = os.listdir(d)
034: ls_f = [x for x in ls_d if os.path.isfile(x) and REG_FILE.search(x)]
035: if ls_f:
036: h[d] = ls_f
037: for d1 in [os.path.join(d,x) for x in ls_d if os.path.isdir(x)]:
038: search_sub(d1)
039:
040: h={}
041: search_sub(d)
042: return h
043:
044: def move_photos(d0, d1):
045: r"""
046: Moveing photo files from media into HD,
047: """
048: dir_of_month = os.path.join(d1, date.today().strftime("%y-%m"))
049: h = search_media(d0)
050: total_files = reduce(operator.__add__, [len(v) for v in h.itervalues()], 0)
051:
052: if total_files==0:
053: print "No photos in the media: give return"
054: sys.stdin.readline()
055: sys.exit()
056:
057: if not os.path.isdir(dir_of_month):
058: os.mkdir(dir_of_month)
059:
060: i_dir = n_photo_dir(dir_of_month)
061: count=0
062:
063: for d, ls_files in h.iteritems():
064: i_dir += 1
065: d_to = os.path.join(dir_of_month, "photo%02d" % i_dir)
066: os.mkdir(d_to)
067:
068: for f in ls_files:
069: f_from=os.path.join(d,f)
070: f_to=os.path.join(d_to, f)
071: shutil.copyfile(f_from, f_to)
072: if not filecmp.cmp(f_from, f_to):
073: print f_from + " and " + f_to + " are not same!"
074: sys.exit()
075: os.remove(f_from)
076: count+=1
077: print "%d/%d\n" % (count, total_files),
078:
079: if __name__=='__main__':
080: move_photos(MEDIA, HD)
���� 80 �Ԥˤʤ�ޤ���Ĺ�������� Perl �ǽ���Τ�Ʊ���Ǥ���
�������θ��ɤ��ϤȤƤ��ɤ���
����ǥ�Ȥǥ֥��å���ɽ������Ȥ��������ǥ����������Ƥ���Ȼפ��ޤ���
�����Τ褦��ʷ�ϵ��Ǥ��ä���Ȥ��Ƥ��ޤ��������ǥ��μ�ͳ�٤����ʤ��Τǡ�
�ʣ�ǯ���μ�ʬ��ޤ��ï���Ƥ�Ʊ���褦�ʥ����ɤˤʤꡢ�ɤ��Τ��ưפǤ���
���Τ��ᡢ�����Ȥ��̤⾯�ʤ��ƺѤߡ��ѿ�̾��������˻Ȥ�ɬ�פ⤢��ޤ���
�ѿ��ϥǥե���ȤǶɽ��ѿ��Ȥʤ�Τǡ� Perl �Τ褦�� my ���������ɬ�פϤ���ޤ���
�ޤ����ꥹ�Ȥ�����ɽ���� Lisp ��
mapcar �� remove-if-not ��Ʊ���˽����Τ������Ǥ���
�饤�֥���ͥ���ǡ��¹�®�٤� Perl ���®���������ޤ���
������ޤ���
�ġ��δؿ��Υƥ��Ȥϼ��Τ褦�ˤ��ޤ���
���� | Perl | Python | Ruby |
---|---|---|---|
�䤹�� | �� | �� | �� |
�ɤߤ䤹�� | �� | �� | �� |
�饤�֥� | �� | �� | �� |
�¹�®�� | �� | �� | �� |
�ɥ������ | �� | �� | �� |
�桼������ | �� | �� | �� |
���ߤο͵���̵�뤷�ơ����줽�Τ�ΤΤ褷������ͤ���ȡ� Python �� Ruby �Ϥܸۤ߳ѤǤ��礦�������������ΤȤ��� Python �������� �¹�®�٤�®�����饤�֥�꤬˭�٤ʤΤǡ��Ȥꤢ���� Python ��Ȥ����Ȥˤ��ޤ��� �ޤ���Python �ˤ����å⡼�ɤ�����Τ� Lisp �˴��줿�ʹ֤ˤȤäƤϤ��꤬�����Ǥ��� Ruby �Ϻ���η�Ʈ�˴��Ԥ��ޤ���
Perl �� Ruby �Ͻ��ʥ�����ץȸ���ǡ��ʤ������ʤ���礭�ʥץ��������褦�ˤϤʤäƤ����ΤΡ� �ץ�������û�����뤳�Ȥ˼�㤬�֤���Ƥ��ޤ��� ������Python �ϥ�����ץȸ���Ȥ��Ƥ�Ĥ������絬�ϥץ�������������ǡ��ǥХå����ưפ��˼�㤬 �֤���Ƥ��ޤ����ץ�����ߥ����Ŭ���ϰϤ������ۤɳؤֲ��ͤ�����Τǡ� ���ΰ�̣�� Python ��ؤ֤��Ȥ� Perl �� Ruby ��ؤ֤��ͭ�Ѥ��Ȼפ��ޤ��� �ʤ��Τ��Ȥ��� google �� Perl �� Ruby �Ǥʤ��� Python ��ȤäƤ��� ��ͳ��ʬ����褦�ʵ������ޤ�����
�����ˤ������Ƥϰ���Python ��ᤰ���Ƚ�ʣ���Ƥ��ޤ��� Python �Υݥꥷ���ˤĤ��Ƥ� A morality tale of Perl versus Python �������� ��The Zen of Python �������ˤƤ���������
������Ǥ�����μ¿��ξ�硢����ʿ�������֤��� [-3,-2,-1,0,1,2,3] �� [0.0, 1.0, 1.4142135623731, 1.73205080756888]
01: # Perl 5 02: my @ls0=(-3,-2,-1,0,1,2,3); 03: my @ls1=(); 04: for (@ls0){ 05: push @ls1, sqrt($_) if $_ >= 0; 06: } 07: print "$_\n" for (@ls1); 08: 09: # Ruby 10: p [-3,-2,-1,0,1,2,3].select{|x| x>=0}.map{|x| Math.sqrt(x)} 11: 12: # Python 13: import math 14: print [math.sqrt(x) for x in [-3,-2,-1,0,1,2,3] if x>=0]
������ѵ��������� �ʿ�n���ꡢ�ֿ�i���ä�n��i�������ä������������ä����ͤ��֤��ؿ��פ��֤��褦�ʴؿ���
01: # Perl 5 02: sub foo { 03: my ($n) = @_; 04: sub {$n += shift} 05: } 06: 07: # Python 08: class foo: 09: def __init__(self, n): 10: self.n = n 11: def __call__(self, i): 12: self.n += i 13: return self.n 14: 15: # Ruby 16: def foo (n) 17: lambda {|i| n += i } 18: end������
01: >>> a=foo(10) 02: >>> a(3) 03: 13 04: >>> a(5) 05: 18
[walk_dir.py]
001: #!/usr/bin/env python 002: # coding:shift_jis 003: 004: from __future__ import with_statement 005: import os, os.path, sys 006: 007: 008: def walk_dir(f, d, exp=''): 009: u'''�ǥ��쥯�ȥ��Ƶ�Ū�ˤ��ɤä����ƤΥե�����˴ؿ� f ��Ŭ�Ѥ��ޤ�''' 010: 011: for p0 in os.listdir(d): 012: p1 = os.path.join(d, p0) 013: if os.path.isfile(p1) and p1.endswith(exp): 014: f(p1) 015: elif os.path.isdir(p1): 016: walk_dir(f, p1, exp) 017: 018: 019: def head(fname): 020: u'''�ǽ�� 5 �Ԥ�ɽ������''' 021: print '==== %s ====' % (fname,) 022: with file(fname) as f: 023: for i, line in enumerate(f): 024: if i==5: break 025: print line 026: 027: 028: # ������ץȤ�ľ�ܸƤФ줿�Ȥ��˰ʲ��Υ֥��å����¹Ԥ���ޤ��� 029: if __name__=='__main__': 030: walk_dir(head, os.getcwd(), '.py')
[walk_dir.rb]
001: #! ruby 002: 003: 004: # walk directories recursively 005: # and apply proc if the element is a file 006: def walk_dir(proc, d=nil, exp='') 007: Dir.foreach(d){|s| 008: if s != '.' and s != '..' then 009: p=File.join(d,s) 010: case File.ftype(p) 011: when "file" 012: if(exp.length==0 or File.extname(p) == exp) then 013: proc.call(p) 014: end 015: when "directory" 016: walk_dir(proc, p, exp) 017: end 018: end 019: } 020: end 021: 022: # show first 5 lines of a file 023: def head(fname) 024: print "\n\n==== " + fname + " ===\n" 025: i=0 026: open(fname) do |f| 027: f.each{|line| 028: if i==10 then break end 029: print line 030: i+=1 031: } 032: end 033: end 034: 035: #----- 036: 037: walk_dir(lambda{|fname| head(fname)}, Dir.pwd, '.rb')
HOME | Python | ���� |