Monday, November 27, 2006

My New Homepage.


Check my new homepage at  http://vaibhav.gupta.googlepages.com/
You can download all the scripts from the given page.
My stumble page http://vaibhav.stumbleupon.com/


Vaibhav.
 

Friday, November 24, 2006

[perl] Create dir and links for a given dir.

#!/usr/bin/perl
#written by vaibhav gupta
#Creates a new directory for a given directory and creates links for files.
$dir = $ARGV[0];
&usages unless $dir ;
©
sub usages {
        print "Usages: <Dirname> \n";
        exit;
}
sub copy {
        @result = `find $dir -type d`;
        $dirname = `echo "$dir"  | rev | cut -d"/" -f 1 | rev`;
        chomp $dirname;
        $desination = "$dirname";
        foreach $d(@result) {
                chomp $d;
                ($a,$b) = split(/$dirname/,$d);
                print "Creating $desination$b\n";
#               print "[$a -- $b]";
                @res = `mkdir -p $desination$b`;
                print @res;
        }
        @result = `find $dir -type f `;
        foreach $d(@result) {
                chomp $d;
                ($a,$b) = split(/$dirname/,$d);
                print "Copying $d\n";
                @res = `ln -s  $d $desination$b`;
                print @res;
        }
}

[perl] Popup window

#!/usr/bin/perl
# written by vaibhav gupta
use Gtk2 '-init';
$x=100;$y=100; #default Cordinates
$blue = 0xffff ;
$label = "Hello there";
$label = $ARGV[0] if $ARGV[0];
$delay = 5 ;
$delay = $ARGV[1] if $ARGV[1];
$x = $ARGV[2] if $ARGV[2];
$y = $ARGV[3] if $ARGV[3];
$blue = $ARGV[4] if $ARGV[4] ;

sub usages {
        print "\n $ARGV[0] <label> <delay> <x> <y> <color>\n";
}

$window = Gtk2::Window->new("popup");
$window->set_title("Hello");
$window->signal_connect( destroy => sub {Gtk2->main_quit}) ;
$label = Gtk2::Label->new($label);
$label->modify_fg('normal',Gtk2::Gdk::Color->new(0,0,$blue) );
$window->add($label);
$window->move($x,$y);
#$window->resize(300,30);
$window->show_all();
Glib::Idle->add( sub { Gtk2->main_quit; 0 } );
#$window->begin_move_drag(0,100,100,1);
Gtk2->main;
print `sleep $delay`;

[python] Start Python

#!/usr/bin/python
import sys
import os
import __builtin__

def helloworld():
    print "Dare to Challenge http://www.pythonchallenge.com/"
    print "Hello World\n";
    print "This is system Path : "
    print sys.path #this is search Path
    sys.path.append("this/is/test/path");
    print "This is system Path after appending test path "
    print sys.path
    filename = os.environ.get('PYTHONSTARTUP')
    print filename
    if filename and os.path.isfile(filename):
        execfile(filename)
    li = []
    print "Method of list ", dir(li) # list all the method of list
    info(__builtin__, 20) # gives the list of built in function

#Factorial Function Start
def fact(n):
    if n > 1:
        return n * fact(n-1)
        else:
        return 1
def info(object, spacing=10, collapse=1):
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    # note
    methodList = [e for e in dir(object) if callable(getattr(object, e))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(["%s %s" %
                     (method.ljust(spacing),
                      processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])

        
def datatypes():
    str = "This is a string."
    print str
    dict = {"server":"mpilgrim", "database":"this will be deleted"}
    print "Currnt Dictionary: ", dict # NOte that it printed in a sorted way.
    dict["server"] = "CHANGED"
    dict["uid"] = "added"
    dict["Uid"] = "case sensitive"
    dict[2] = "mixed data"
    dict["mixed"] = 123
    del dict["database"]
    print "Now Dictionary   : " , dict
    dict.clear() # To clear all the contents
    list = ["a", "b", "mpilgrim", "removethis", "example","pop"]
    print "Current List   :" , list
    print "Length of List : ", len(list)
    print "list[-1]   :", list[-1] #"li[-n] == li[len(li) - n]"
    print "Slicing    :", list[1:-1]
    print "Pop Element: ", list.pop()
    list.append("append")
    list.extend(["extend1","extend2"]) #note the arg is a list.  Concatenates
    list.insert(2,"inserthere")
    list.remove("removethis");
    list += ["Operators"]
    print "Now List   : ",list
    print "Searching extend1 (first occurence): Found at location ", list.index("extend1")
    print "Found c in list:" , "c" in list
    list = ["1","2"]
    print "List Operator  : ", list * 10
    tuple = ("son","mon","tue","wed","thu","fri","sat") # Ordered, fast, But can not be modified
    print tuple
    (a,b,c,d,e,f,g) = range(7)
    print "rnage d = ", d
    #3.6

#Calling main Functions.
helloworld()
#print fact(500)
#datatypes();
#info(list)



"""  Notes
---------- and-or Trick  ----------

>>> a = "first"
>>> b = "second"
>>> 1 and a or b
'first'
>>> 0 and a or b
'second'
---------- and-or Trick Safely ----------
>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0]
---------- split With No Arguments ----------
>>> s = "this    is\na\ttest"
>>> print s
this   is
a       test
>>> print s.split()
['this', 'is', 'a', 'test']
>>> print " ".join(s.split())
'this is a test'

"""

The Fastest Language

Well,
Though this post is a little digression from what this forum is meant
for. It is really an interesting one because many a times we think

Which language is the fastest?

Considering the same hardware/environments are provided, it will all
depend on how well a compiler/interpreter make use of underlying
hardware/environment to generate native/interpreted code.

In a computer language shootout [Survey]
differrent compiler for different languages tested against their
ability to translate code for Ackermann Function
[Ackermann_function], Fibonacci numbers[Fibonacci_number] and
Tak functions [TAKFunction.html].

I was really happy to see that my favourite compiler gcc rules :-)

-by sbjoshi