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

Scripting 101

Chat log Class 7

hemanth hm's picture
Sat, 2010-10-09 19:00

<nmort> hello
<hemanth> hey
<hemanth> hows it going?
<nmort> good, thanks
<hemanth> nmort, did u try redirections?
<nmort> I haven't had the chance to practice
* hemanth finished pulling the source of SourRust
<hemanth> nmort, did u fork the repo?
<nmort> yes, I have to pull the latest changes though
<hemanth> nmort, its fetch
<hemanth> SourRust, nmort hobs so lets start? enough of waiting i feel
<nmort> ok
<SourRust> sure
<hemanth> Or do u thing we need to do some recitals of previous class?
<hemanth>  /class/classes
<hemanth> feels strange seeing few many regular students missing today
<hemanth> SourRust, nmort what say?
<nmort> recitals?
<SourRust> what do you mean by recitals? like a review?
<hemanth> nmort, like recalling stuff, any doubts issues, suggestions
<nmort> sure, it would be good, redirections wasnt too hard for me, but yes
<hemanth> a detailed statement giving facts and figures; "his wife gave a recital of his infidelities"
<hemanth> ;)
<nmort> we could do a quick review
<hemanth> 0,1,2
<hemanth> those three numbers would say a lot in bash
<hemanth> remember them?
<nmort> input, output and error file descriptors
<hemanth> yes correct
<hemanth> now lets start with python redirections
<hemanth> SourRust, there?
<SourRust> yea
* hemanth has changed the topic to: Redirections in Python
<hemanth> So has we had seen in the last class
<hemanth> redirections in bash, how and why there are used
<hemanth> it must be more clear by now, to all about input, output and errors
<hemanth> so the major use of redirection was for ?
<nmort> changing the default input, output or error streams?
<nmort> or redirecting error messages
<hemanth> yes, it was for the easy of exasperating them all
<hemanth> py,rb and pl have strong error handling methods
<hemanth> Errors and Exceptions in itself is a chapter
<hemanth> stderr,stdout and stdin
<hemanth> Python has a built-in module called sys
<hemanth> we can 'import' it to our code, using the import statement
<hemanth> >>> import sys
<hemanth> is how one imports a module
<hemanth> stdin -- standard input file object; used by raw_input() and input()
<hemanth> stdout -- standard output file object; used by the print statement
<hemanth> stderr -- standard error object; used for error messages
<hemanth> clear so far?
<SourRust> yes
<hemanth> if in bash it was all easy to redirect with > file
<hemanth> still the same can be used, say in bash one executes a python script
<hemanth> as python
<hemanth> still the bash redirections can be applied on it
<hemanth> can anyone guess how?
<SourRust> python > somefile.txt
<hemanth> indeed :)
<hemanth> but if the same is to be achieved in a python script
<hemanth> we have ways to do it, lets see them
<hemanth> as said earlier, we need the help of sys module
<hemanth> print >> fileObject
<hemanth> ^^ was there in python so far, but its all deprecated
<hemanth> so beware of that usage, if you come across, don use them anymore
<hemanth> >>> sys.stdout = open('/dev/null', 'w')
<hemanth> >>> print "hi"
<hemanth> what does this print?
<SourRust> nothing
<hemanth> but why?!
<hemanth> ;) ?
<SourRust> its being sent to the blackhole
<hemanth> sys.stdout is the clue
<hemanth> yup!
<hemanth> now in the same lines
<hemanth> >>> sys.stderr = open('err', 'w')
<hemanth> >>> sys.stderr.write("hi")
<hemanth> sys.stdin
<hemanth> can also be achieved
<hemanth> but what is the whole deal of using them in a py script, one might wonder now
<hemanth> for that we need to know a bit more on error handling in py
<hemanth> lets see errors and exceptions
<hemanth> What are errors?
<hemanth> A mistake?
<hemanth> So what is mistake? mistake: a wrong action attributable to bad judgment or ignorance or inattention;
<hemanth> very true in case of coding also!
<hemanth> Syntax and Systematics errors are the types here
<hemanth> *Systematics
<hemanth> type auto spell correction issue grr
<hemanth> its semantics
<hemanth> Syntax errors, also known as parsing errors
<hemanth> fine so far?
<SourRust> yup
<nmort> sys.stdout = open('/dev/null','w')
<nmort> and all the other statements
<nmort> are they supposed to give errors?
<hemanth> correct
<hemanth> no
<hemanth> sys.stdout = open('/dev/null','w')
<hemanth> if that is done
<hemanth> everything u print
<hemanth> does not come to the stdout that is ur monitor
<nmort> I am getting NameError: name 'sys' is not defined
<hemanth> but is redirected to /dev/null
<hemanth> nmort, you forgot to import
<hemanth> :)
<nmort> oops
<hemanth> >>> import sys
<nmort> got it
<hemanth> kool
<hemanth> now, what are Exceptions ?
<nmort> so importing has to be done each time a redirection is made?
<hemanth> when ever one uses, any methods or variables defined in any module
<hemanth> he must import it
<hemanth> here we are using sys. file objects
<hemanth> we must import it
<hemanth> once the import sys statement is used
<hemanth> it's methods and variables can be used anywhere in the script
<hemanth> its something like #includes<stdio.h> in C
<hemanth> assuming most of them are familiar with C
<hemanth> if we had #include<sys.h> => import sys
<hemanth> as other languages like Java for the matter uses this import statement
<hemanth> to import packages
<nmort> I see
<hemanth> such modules, helps use not to reinvent the wheel
<hemanth> but rather reuse them
<hemanth> to make our productivity more and better
<hemanth> ok, let move on...
<hemanth>  /let/lets
<hemanth> Exceptions?
<hemanth> He was an exceptional student!
<hemanth> what can we conclude from that?
<hemanth> exception means?
<nmort> he was different from the rest,
<nmort> a special case
<hemanth> yes, in py its a pretentious way of saying error.
<hemanth> Errors detected during execution are called exceptions and are not unconditionally fatal
<hemanth> >>> while True say 'Hello world'
<hemanth>   File "<stdin>", line 1
<hemanth>     while True say 'Hello world'
<hemanth>                  ^
<hemanth> SyntaxError: invalid syntax
<hemanth> This is an Error
<hemanth> >>> 420 * (0/0)
<hemanth> Traceback (most recent call last):
<hemanth>   File "<stdin>", line 1, in <module>
<hemanth> ZeroDivisionError: integer division or modulo by zero
<hemanth> Is an exception
<hemanth> The last line of the error message => what happened.
<hemanth> There are many such exceptions
<hemanth> clear so far?
<SourRust> yup
<nmort> yes
<nmort> actually
<nmort> what do you mean by unconditionally fatal?
<hemanth> as you saw
<hemanth> as soon as the statement was executed
<hemanth> it banged
<hemanth> and if that were to be done in a script
<hemanth> the script dies then and there itself
<hemanth> its a fatal error
<hemanth> unconditional => categorical => absolutely
<hemanth> clear?
<nmort> so error and exception
<nmort> is like warning and error
<hemanth> no!
<hemanth> warnings are different
* SourRust has quit (Ping timeout: 265 seconds)
<hemanth> warnings normally wont hamper the code flow
<hemanth> like deprecated method warning
<hemanth> is the list of exceptions
* SourRust (473b12d5@gateway/web/freenode/ip. has joined #p2pu-webcraft/scripting-101
<SourRust> weird... got randomly kicked off
<hemanth> SourRust, wow! is it?
<hemanth> someone trying to hack in to the channel then!
<SourRust> >_>
<hemanth> nice, we are becoming famous :)
<nmort> lol
<hemanth> SourRust, is the list of exceptions
<hemanth> So we 'try' something and 'expect' an error
<hemanth> So we have the try, expect block to handle it
<hemanth> we can also raise an exception
<hemanth> So, after all this we 'finally' do something
<hemanth> so the keywords to be noted here is: 'raise','try','expect','finally'
<hemanth> from the docs, i give you an example
<hemanth> >>> def divide(x, y):
<hemanth> ...     try:
<hemanth> ...         result = x / y
<hemanth> ...     except ZeroDivisionError:
<hemanth> ...         print "division by zero!"
<hemanth> ...     else:
<hemanth> ...         print "result is", result
<hemanth> ...     finally:
<hemanth> ...         print "executing finally clause"
<hemanth> ...
<hemanth> >>> divide(2, 1)
<hemanth> result is 2
<hemanth> executing finally clause
<hemanth> >>> divide(2, 0)
<hemanth> division by zero!
<hemanth> executing finally clause
<hemanth> >>> divide("2", "1")
<hemanth> executing finally clause
<hemanth> Traceback (most recent call last):
<hemanth>   File "<stdin>", line 1, in ?
<hemanth>   File "<stdin>", line 3, in divide
<hemanth> TypeError: unsupported operand type(s) for /: 'str' and 'str'
<hemanth> as you can see
<hemanth> we 'try' first
<hemanth> expect next
<hemanth> and 'finally' no matter what happens we do it :)
<hemanth> sounds clear?
<SourRust> yup
<hemanth> >>> try:
<hemanth> ...     raise KeyboardInterrupt
<hemanth> ... finally:
<hemanth> ...     print 'Goodbye, world!'
<hemanth> ...
<hemanth> Goodbye, world!
<hemanth> KeyboardInterrupt
<hemanth> that's how we could raise an exception
<hemanth> So, that's it with py
<hemanth> So if all clear, we shall say bye :)
<nmort> ok, clear
<hemanth> thanks all
<hemanth> do push code like SourRust and Adam :)
<nmort> I can push?
<hemanth> c u all tomo :)
<SourRust> cya
<nmort> bye