Google Chart API

Jun 30
2008

Charts for a map (in this example I’ve included Spain, France, Italy and Norway):






more about this API

Getting an indication of the number of arguments accepted by a method

Jun 16
2008
class Object
  def arguments?(*methods)
    methods.each do |method|
      begin
        rval = self.method(method).arity
        if rval > 0
          puts "'#{method}' takes fixed (#{rval}) number of argument"
        elsif rval < 0
          puts "'#{method}' takes variable number of arguments"
        elsif rval == 0
          puts "'#{method}' takes no arguments"
        end
      rescue
       puts $!
      end
    end
  end
end
 
String.new.arguments?(:size, :to_i, :scan, :flatten)

www.ruby-doc.org arity

Fun with String class methods

Jun 14
2008
class MyString < String
  # not really useful
  alias + concat
 
  # emulates Array '*' method
  def *(string)
    self.split('').inject('') do |s1,c|
      s1 + c + string
    end
  end
 
  # emulates Array '-' method
  def /(string)
    (self.split('') - string.split('')).to_s
  end
 
  # chars' delimiter
  def delimiter(string)
    self.*(string).sub /.$/, ''
  end
 
end
 
a = MyString.new("abcdef")
b = MyString.new("ABCDEF")
 
puts a * ':'
puts a / (b + "a")
puts a.delimiter(":")
 
#a:b:c:d:e:f:
#bcdef
#a:b:c:d:e:f

downloader

Jun 09
2008
#!/usr/bin/perl
 
use strict;
use warnings;
use WWW::Mechanize;
use LWP::Simple;
use Data::Dumper;
use Time::HiRes qw( usleep tv_interval gettimeofday );
$|++;
my $url  = shift || die "no url given!\n";
my $ext  = shift || 'mp3'; #die "no file extention given!\n";
my $mech = WWW::Mechanize->new(agent=>"Mozilla/5.0");
my $ua   = LWP::UserAgent->new;
my %seen;
 
$mech->get($url);
$ua->timeout(5);
 
my $links = $mech->links;
 
no warnings;
my $c = 0;
for my $link ( @{$links} ) {
        my $url  = $link->url_abs;
        my $res  = $ua->head($url);
        my $http_res   = HTTP::Response->new($res);
        my $abs_name   = $http_res ->{'_rc'}->{'_previous'}->{'_headers'}->{'location'};
        my ($rel_name) = $abs_name =~ /.+\/(.+)$/;
        my $local_name = $rel_name;
 
        $seen{$local_name}++ and next;
 
        if( $rel_name =~ /\Q$ext\E$/i ){
                $rel_name =~ s/\%\d+/ /g;
                $rel_name =~ s/(.{35}).+/$1.../;
                print(pack('A45', "[$rel_name]"), "  is being downloaded ... ");
                my $s_time = q{};
                my $e_time = q{};
                my $flag = [];
 
                for( 1..5 ){
                        local $SIG{ALRM} = sub { die("timeout") };
                        eval {
                           alarm(10);
                           $flag = [head($abs_name)];
                           alarm(0);
                        };
                        next if $@ =~ m|timeout|;
                        last if $@ !~ m|timeout|;
                }
 
                if( $flag->[0] ){
                        $c++;
                        $s_time = [gettimeofday];
                        getstore($abs_name, $c."-".$local_name);
                        $e_time = tv_interval ($s_time, [gettimeofday]);
                }
 
                $e_time =  $e_time ? $e_time . " seconds" : 'less than 1 milisecond';
                print "timeout failure in $e_time seconds :(\n" and next if $@;
                print "done in $e_time\n";
        }
}
 
`ruby -e 'Dir["*"].each{|f| File.rename f, f.sub(/^\d+-/, "")}'`;
`ruby -e 'Dir["*"].each{|f| File.rename f, f.gsub(/%20/, "_")}'`;

Calendar

June 2008
M T W T F S S
« May   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Tags