Python -functions/modules/system calls

lists
opening a file
Exit codes
using functions
import system Linux os calls
create your own module
In build python hello world
Import modules
Check modules
Install new software packages

Lists

if you have a list or a word in memory and you want to use the second item see the following, you can also use [-1] # last character or [-2] # second-last character

#!/usr/bin/python
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
print cats[2]
# ./list
Kitty

opening a file

openfile = open('pathtofile', 'r')
openfile.read()

Exit codes(

sys.exit(1)
exit(0) means a clean exit without any errors / problems

exit(1) means there was some issue / error / problem and that is why the program is exiting.

Using functions

To save yourself repeating code (and meaning you only need to change the code in 1 place if its called many times)

# cat func
#!/usr/bin/python
def hello():
print "hello and welcome"

hello()
hello()
hello()

# ./func
hello and welcome
hello and welcome
hello and welcome

import system Linux os calls

to import the native linux commands:

cat os.system
#!/usr/bin/python
import os
os.system("date")
[root@testautoinstall4 python]# ./os.system
Tue Sep 23 04:48:32 EDT 2014

to check where the os is imported from run
>>> import os
>>> print os.__file__
/usr/lib64/python2.6/os.pyc
>>>
to change the sys path add the code:

sys.path.append('/usr/local/rnt/lib')

that means the python path is imported from here
any following imports i.e

import fstab

will import the fstab.py from that location

create your own module

creating modules rules:

give it a py extension
make sure you don't call it a number
the module doesn't need to be excutable

[root@test-scripting-OCt2015 11wed]# cat test2.py
#!/usr/bin/python
def x():
print "hello and welcome"

test.x()

[root@test-scripting-OCt2015 11wed]# cat ./rem3
#!/usr/bin/python
import test2
test2.x()
test2.x()

[root@test-scripting-OCt2015 11wed]# ./rem3
hello and welcome
hello and welcome

good page on modules http://www.tutorialspoint.com/python/python_modules.htm

In build python hello world

>>> import __hello__
Hello world...

Import modules

Modules allow you to use some of the inbuilt system commands to do more with code

The OS module in Python provides a way of using operating system dependent
functionality, example of usage

cat date.py
#!/usr/bin/python
import os
f = os.popen('date')
now = f.read()
print "Today is ", now

./date.py
Today is Tue Nov 11 11:32:11 EST 2014

More on python modules from
http://www.pythonforbeginners.com/os/pythons-os-module

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences −

The current directory.

If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.

If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

Check modules

to get list of attributes for a module

>>> import terminal
SyntaxError: invalid syntax
>>> dir (terminal)
['Color', 'Command', 'Logger', 'Option', '__author__', '__builtins__', '__doc__', '__file__', '__homepage__', '__name__', '__package__', '__path__', '__version__', 'absolute_import', 'black', 'black_bg', 'blink', 'blue', 'blue_bg', 'bold', 'choose', 'color', 'colorize', 'command', 'conceal', 'confirm', 'cyan', 'cyan_bg', 'faint', 'getpass', 'gray', 'gray_bg', 'green', 'green_bg', 'grey', 'grey_bg', 'hex2ansi', 'inverse', 'is_256color_supported', 'is_color_supported', 'italic', 'log', 'magenta', 'magenta_bg', 'os', 'overline', 'password', 'prompt', 'red', 'red_bg', 'rgb2ansi', 'strike', 'string_type', 'sys', 'underline', 'white', 'white_bg', 'yellow', 'yellow_bg']
>>>

or nicer output
for i in dir(terminal): print i

 

Install new software packages

pip is a package management system used to install and manage software packages written in Python

yum -y install python-pip
pip install pudb
pip help
pip list

pip freeze
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
iniparse==0.3.1
ordereddict==1.2
pudb==2015.4.1
pycurl==7.19.0
Pygments==2.0.2
pygpgme==0.1
terminal==0.4.0
urlgrabber==3.9.1
urwid==1.3.0

more reading at http://dubroy.com/blog/so-you-want-to-install-a-python-package/

to install an old version of pip (when you have more than 2 versions of python running)
# whereis pip
pip: /usr/bin/pip3.4 /usr/bin/pip /usr/bin/pip2.6
pip-2.7 install mybarpackage