Sunday, June 19, 2011

Script mode


Python is a dynamic, interpreted language. Source code does not declare the types of variables or parameters or methods. This makes the code short and flexible, and you lose the compile-time type checking in the source code.

We have already worked with interactive mode Python interpreter. Now we need to experiment with the Script mode. In script mode we have to write a set of statements in a file. The file must have the extension ‘.py’. The .py file can be run on terminal using the command,

python pgmname.py

We have to create a .py file using the vi editor. For that use the command,

            vi pgmname.py

Now we can write the statements and save it using ‘:wq’ in the file and execute it. I got a simple program from Google’s Python class,


# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
  print 'Hello there', sys.argv[1]
  # Command line args are in sys.argv[1], sys.argv[2] ..
  # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()


I wrote these codes in vi editor and executed it.




'python pgmname.py Ajay' is used to pass an argument of ‘Ajay’.

One file of Python code is called a module. The file "binky.py" is also known as the module "binky". A python module can be run directly -- as above "python hello.py Bob" -- or it can be imported and used by some other module. When a python file is run directly, the special variable "__name__" is set to "__main__". Therefore, it's common to have the boilerplate if __name__ ==... shown above to call a main() function when the module is run directly, but not when the module is imported by some other module.

We have the standard sys module that contains some standard system facilities, like the argv list, and exit() function. With the statement "import sys" another module can access the definitions in the sys module. The import does not actively import all the definitions; it just makes them available by their fully-qualified name, e.g. sys.exit().

One unusual Python feature is that the whitespace indentation of a piece of code affects its meaning. A logical block of statements such as the ones that make up a function should all have the same indentation, set in from the indentation of their parent function or "if" or whatever. If one of the lines in a group has a different indentation, it is flagged as a syntax error. Look at the above codes where I used indentation.

Inside the python interpreter, the help() function pulls up documentation strings for various modules, functions, and methods. These doc strings are similar to Java's javadoc. This is one way to get quick access to docs. Here are some ways to call help() from inside the interpreter:

  • help(len) -- docs for the built in len function (note here you type "len" not "len()" which would be a call to the function).
  • help(sys) -- overview docs for the sys module (must do an "import sys" first).
  • dir(sys) -- dir() is like help() but just gives a quick list of the defined symbols.
  • help(sys.exit) -- docs for the exit() function inside of sys.
  • help('xyz'.split) -- it turns out that the module "str" contains the built-in string code, but if you did not know that, you can call help() just using an example of the sort of call you mean: here 'xyz'.foo meaning the foo() method that runs on strings.
  • help(list) -- docs for the built in "list" module.
  • help(list.append) -- docs for the append() function in the list module.
Commonly used standard python modules include:
  • sys -- access to exit(), argv, stdin, stdout etc
  • re -- regular expressions
  • os -- operating system interface, file system
These are preliminary information regarding the script mode execution in Python Interpreter and I will post more about other features soon.

Thanks

AJAY

No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.