Friday, September 25, 2015

Python Hunt Series - Scripts, Modules and Packages

Python Hunt Series - Scripts, Modules and Packages

prev post in the series - Isolated Environment

In this post, let's discuss about scripts, modules and packages in python.

Before getting into packages, lets discuss on modules and scripts. A file with .py extension when ran from the terminal is called as script where as when imported called as module. 

A script can solve any purpose. Lets assume we have a need to start a server instance. we can write a script that starts the server instance. similarly we can also write a script to stop the server instance. In-fact we can write a single script that takes input as either start or stop and does it accordingly


Before going any further into scripts, lets discuss on scope, scope is something which determines what can be accessed at a given instance. Anything outside scope is not accessible.

The above statement can be proved by getting into python interpreter and trying to access a variable which is not defined in current scope.

Type python in the terminal to get into python interpreter



If you see the above snapshot, I tried seeing what's there in the local scope by using locals() inbuilt function.

If you wonder how locals( ) function is there in scope, that's because inbuilt functions are always in local scope. It can be witnessed in the dictionary returned by locals() function call

Then I defined variable 'a' to hold value 10. Now the local scope includes 'a' . When I try to access 'a' , I get the value. When I try to get value of variable 'b' which is not in the local scope. I get NameError

Lets assume you got struck in middle of a trip and have only half a bottle of water. You don't have access to water for next 5 km. At this instance your scope for water is only the half bottle you have. you cant access anything more than that. Similarly, when you write a script you can access to only things that's there inside the scope at a given instance.



Variables defined inside a function lies in the private scope of the function. We cannot use it outside. Where as a variable defined in a global scope can be assessed throughout the script

There is an inbuilt variable __name__ that lies in the local scope and takes value "__main__". When a module is loaded __name__ takes the value of module name where as when a python file alias module is run as a script __name__ takes the value "__main__"

when we want to execute module from the terminal then we need to add a boiler plate template at the end of the file. 

if __name__ == "__main__":
    main(argv[1:])

As said before, when we run a python file as a script from terminal, it is loaded as a "main" module. Thereby the if condition gets validated and main function is called. Now we can add our logic inside main function

We discussed on local scope and global scope just to make sure we understand how __name__ inbuilt variable is set when we import a module and when we run it as script

Now lets see how to learn about  modules and start using it.

Consider we want to know about the re (regular expression) module

Python has a inbuilt function dir( ) which takes module name as input and gives all its attribute as a list

>>> import re
>>> dir(re)

['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

now we can start surfing on the attributes and try using them. 

Assume python library to be a fridge and modules as items inside fridge. We can pick anything we want and start eating alias importing it






Now lets get into python package, a collection of module in a folder with __init__.py  file forms a python package

When we work on projects, logical modules are grouped and placed inside a folder. __init__.py file identifies the folder as python package.

Being said that, we can import the modules inside the package with package name as namespace

something like,

import test_package.test_module

Cheers!

No comments:

Post a Comment