This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Scripting 101

Chat log Class 11

hemanth hm's picture
Sat, 2010-10-23 18:48

<hemanth> SourRust, the man who is always on time :)
<SourRust> yup :)
<hemanth> * Sharp on time
<hemanth> :)
* hemanth has changed the topic to: BASH functions
<hemanth> Hello all, today we are doing functions
<hemanth> "A subroutine or subprogram (also called procedure, method, function, or routine) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code."
<hemanth> Lets first see, how do define functions
<hemanth> function name { COMMANDS ; } or name () { COMMANDS ; }
<hemanth> That's how we define  a shell function.
<hemanth> to invoke the function, we just say the name of function
<hemanth> lets see an example
<hemanth> mul()
<hemanth> {
<hemanth> echo "$1 + $2 = $(($1 * $2))"
<hemanth> }
* hemanth rather echo "$1 * $2 = $(($1 * $2))"
<hemanth> as you would have guessed, it will print the product of $1 and $2
<hemanth> if you all, remember, we had long back seen those $0,$1,$#,$*
<hemanth> so on...
<hemanth> recalling, they are nothing but positional parameters
<hemanth> to the function mul takes arguments
<hemanth> works on them, and gives us some output
<hemanth> Input ===> |=| ====> Output
<hemanth> that's my symbolic rep of function
<hemanth> |=| is our function logic :)
<SourRust> mkay
<hemanth> mul 7 2
<hemanth> would echo 14 :)
<hemanth> 'type mul'
<hemanth> would shows use the logic/body/definition of the function
<hemanth> on of the most common function that a bash geek will use, would be something like open <file>
<hemanth> #!/bin/bash
<hemanth> open() {
<hemanth>     case "$1" in
<hemanth>         *.mp3|*.ogg|*.wav|*.flac|*.wma) xmms "$1";;
<hemanth>         *.jpg|*.gif|*.png|*.bmp)        display "$1";;
<hemanth>         *.avi|*.mpg|*.mp4|*.wmv)        mplayer "$1";;
<hemanth>     esac
<hemanth> }
<hemanth> for file; do
<hemanth>     open "$file"
<hemanth> done
* hemanth assumes that mac already has some function like open, not so sure, but remember seeing it once
<SourRust> it does
<hemanth> :)
<hemanth> can you try 'type open' ;) ?
<hemanth> not sure, what it might say
<SourRust> doesnt print function out 'open is /usr/bin/open'
<hemanth> hmmm, closed source, binary ?
<SourRust> i guess so
<hemanth> type $(whereis open)
<hemanth> must answer it ;)
<SourRust> '/usr/bin/open is /usr/bin/open' i bit redundant
<SourRust> its a bit*
<hemanth> he he ok, cat /usr/bin/open ;)
<SourRust> yea its a binary
<hemanth> has to be, as its in /usr/bin :)
<hemanth> okies let move on
<hemanth> to unset the defined function
<hemanth> we say, 'unset function_name'
<hemanth> "Unset values and attributes of shell variables and functions"
<hemanth> try usent mul ; mul
<hemanth> * unsent mul; mul
<hemanth> grr so many typo today
<hemanth> unset mul ;mul
* hemanth first time these many typos
<hemanth> that must say => mul: command not found
<SourRust> yup
<hemanth> so, that's how we define functions and unset them
<hemanth> indeed functions are just like tiny little scripts
<hemanth> but they can take in inputs and return some values, within the script or even in the shell
<hemanth> they are very useful in maintenance and reuse-ability
<hemanth> so any queries so far?
<SourRust> nope
<hemanth> okies moving on
* hemanth TOPIC FUNCTIONS PYTHON
<hemanth> So in python, we def[ine] a function as
<hemanth> def <function_name>:
<hemanth>     blah blah
<hemanth>     return
<hemanth> note the indentation, which is very much required
<hemanth> so for example:
* PaBLoX (c8686260@gateway/web/freenode/ip.200.104.98.96) has joined #p2pu-webcraft/scripting-101
<hemanth> def hi():
<PaBLoX> hi! :P
<hemanth>     print Hi :P
<PaBLoX> sorry for being late
<hemanth>     print "Hi :P"
<hemanth>     print "Its ok PaBLoX"
<hemanth>     return
* hemanth sends logs to PaBLoX
<hemanth> so to invoke the function its  function_name() here
<hemanth> hi()
<hemanth> will invoke the hi function
<hemanth> functions take parameters, as in bash, but here its bit different
<hemanth> function_name( param1, param2 ) so on
<hemanth> where param1, param2 are passed to the function at the time of invocation
<hemanth> for example :
<hemanth> >>> def hi(name):
<hemanth> ...     print "hello "+name
<hemanth> ...
<hemanth> >>> hi("SourRust")
<hemanth> hello SourRust
<hemanth> python functions, can return values
<hemanth> in bash the last echo'd line would be the return
<hemanth> in python, we say return val1,val2,val3 so on
<hemanth> python can return, single or multiple values of any type
<hemanth> for example
<hemanth> def giveMeList(string):
<hemanth>     return list(string)
<hemanth> giveMeList("PaBlox SourRust Hobs Lakshmi")
<hemanth> ['P', 'a', 'B', 'l', 'o', 'x', ' ', 'S', 'o', 'u', 'r', 'R', 'u', 's', 't', ' ', 'H', 'o', 'b', 's', ' ', 'L', 'a', 'k', 's', 'h', 'm', 'i']
<hemanth> giveMeList("PaBlox SourRust Hobs Lakshmi")[::-1]
<hemanth> ['i', 'm', 'h', 's', 'k', 'a', 'L', ' ', 's', 'b', 'o', 'H', ' ', 't', 's', 'u', 'R', 'r', 'u', 'o', 'S', ' ', 'x', 'o', 'l', 'B', 'a', 'P']
<hemanth> that is the reverse ;)
<SourRust> sweet
<hemanth> we, can also have default argument, defined, in the function header as
<PaBLoX> Sorry, I've a conceptual question
<hemanth> myfun(x="420",y,z="66")
<hemanth> so with myfun, the user might invoke it as, myfun(6) or myfun(6,7) or myfun(testing,fun,works)
<hemanth> so on...
<hemanth> python, borrows, lambda notations from LIPS
<hemanth> i meant LISP :)
<hemanth> Lambda Expressions || Anonymous Functions
<hemanth> p = lambda x: x**2
<hemanth> p(3) will print 9
<hemanth> those or one line, mini functions, which are indeed fun
<hemanth> even = filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
<hemanth> to get even numbers from list :)
<hemanth> even will have [2, 4, 6, 8]
<hemanth> so if a function returns  something, you need to receive it, as some_variable = function_name()
<hemanth> queries ??
<SourRust> pablox had one a few minutes ago
<PaBLoX> * is trying to understand lambda ~_~
<hobs> select from awesome where name is 'hemanth'
<hobs> ;
<hobs> is my query
<hemanth> hobs, wb :)
<hobs> yeah I had a slow start at work today so I realized we would be going on
<hemanth> :)
<hemanth> <PaBLoX> function arguments or parameteres is th same?
<hemanth> parameters is what we pass to the function, function arguments are that which the function takes :)
<hemanth> so both may be considered the same, but depends if the language is pass by value or pass by reference
* hemanth suggest to read more about programming paradigms on functions
<hemanth> issues?
<SourRust> nope
<hobs> looks good
<hemanth> okies
<hemanth> So lets move on
* hemanth has changed the topic to: Functions RUBY
<hemanth> def function_name(variable)
<hemanth>    return <value>
<hemanth> end
<hemanth> def hi(name)
<hemanth>    return "Hello " + name
<hemanth> end
<hemanth> to invoke its we call the function_name(param)
<hemanth> def hi(name,Mr="Mr.")
<hemanth> we also have default params, as ^
<hemanth> clear ?
<SourRust> yup
<hemanth> tell = lambda {|this_thing| puts this_thing}
<hemanth> tell.call("black board")
<hemanth> tell["that this"]
<hemanth> is how lambda works in rb
<hemanth> fine so far?
<hobs> im still a little hazy on lamba in general but I will read back
<hemanth> ok :)
<hemanth> lets move on? 5 more mins
* hemanth has changed the topic to: PERL FUNCTION
<hemanth> sub function_name
<hemanth> {
<hemanth>    function_body
<hemanth> }
<hemanth> that how we define functions in perl
<hemanth> sub print_hi
<hemanth> {
<hemanth>    print "Hello";
<hemanth>    exit 1;
<hemanth> }
<hemanth> sub can also return values
<hemanth> sub sort_array
<hemanth> {
<hemanth>   @unsorted_array = (-3, 3, 43, -5, 7, 11, 13);
<hemanth>   @sorted = sort(@unsorted_array);
<hemanth>   return @sorted;
<hemanth> }
<hemanth> will return sorted array
<hemanth> all variables in the function are global, one can use 'my' keyword to make a var local
<hemanth> that is like my  @unsorted_array = (-3, 3, 43, -5, 7, 11, 13);
<hemanth> All arguments passed to any function are stored in an array @_
<hemanth> for($i=0;$i<=$#_;$i++){
<hemanth>     print "Argument $i: $_[$i]\n";
<hemanth> is how, we can use them in a function
<hemanth> my ($scalar, @array) = @_;
<hemanth> doing that, we get scalar and arrays that are passed to the function
<hemanth> So, this should make things more clear
<hemanth> the best way to learn programing is programing
<hemanth> so, i hope you all will push the code which you experiment on while learning
<hemanth> :)
<PaBLoX> I don't like perl ) :
<hemanth> he he, list your reasons in a thread in the course room, so it will help others :P
<hemanth> so, if no more quires, i will logout for the day :)
* hemanth : Sorry for taking extra 15mins! of ur time
<SourRust> its ok
<SourRust> and cya
<hemanth> :)
<hemanth> logs will be up in mins