Friday, November 18, 2016

What is the use of enumerate function in python

enumerate() is one of the built-in Python functions. It returns an enumerate object. In our case that object is a list of tuples (immutable lists), each containing a pair of count/index and value. 

>>> choices = ['pizza', 'pasta', 'salad', 'nachos']
>>> list(enumerate(choices))
=> [(0, 'pizza'), (1, 'pasta'), (2, 'salad'), (3, 'nachos')]
So, in the for index, item in enumerate(choices): expressionindex, item is the pair of count, value of each tuple: (0, 'pizza'), (1, 'pasta'), ...
We may easily change the start count/index with help ofenumerate(sequence, start=0)
for index, item in enumerate(choices, start = 1):
    print index, item
or simply with a number as the second parameter
for index, item in enumerate(choices, 1):
    print index, item
Try the following code:

>>>with open ('/etc/passwd') as f1:
...    ab = f1.readlines()
...     for i,v in enumerate(ab, 1):        
...      print i,v

Thursday, November 17, 2016

File handling in Python Cheatsheet: All modes

  • r

    Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
  • rb

    Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
  • r+

    Opens a file for both reading and writing. The file pointer will be at the beginning of the file. 
    (cannot truncate a file)
  • rb+

    Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
  • w

    Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  • wb

    Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  • w+

    Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
    (can truncate a file)
  • wb+

    Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  • a

    Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  • ab 

    Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  • a+

    Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  • ab+

    Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.


Important


Always make sure you explicitly close each open file, once its job is done and you have no reason to keep it open. Because - There is an upper limit to the number of files a program can open. If you exceed that limit, there is no reliable way of recovery, so the program could crash. - Each open file consumes some main-memory for the data-structures associated with it, like file descriptor/handle or file locks etc. So you could essentially end-up wasting lots of memory if you have more files open that are not useful or usable. - Open files always stand a chance of corruption and data loss.




Tuesday, November 15, 2016

Unable to connect to RHN network - ping works but nslookup fails

You should have proper entries in /etc/hosts and /etc/nsswitch.conf

Usually, you'll want its hosts line to look like(in /etc/nsswitch.conf):

hosts:      files dns
Antoher file to check is /etc/resolv.conf
check the permissions of all these files.

Read: http://superuser.com/questions/704785/ping-cant-resolve-hostname-but-nslookup-can

Monday, November 7, 2016

Installing Python modules using pip

pip is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI). Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip (pip3 forPython 3) by default.

Install steps: https://pip.pypa.io/en/stable/installing/

Wget https://bootstrap.pypa.io/get-pip.py

Then run the following:

python get-pip.py

Install paramiko using pip


For reference, paramiko which we install has a hard dependency on cryptography which states:
For Debian and Ubuntu, the following command will ensure that the required dependencies are installed:
$ sudo apt-get install build-essential libssl-dev libffi-dev python-dev
For Fedora and RHEL-derivatives, the following command will ensure that the required dependencies are installed:
$ sudo yum install gcc libffi-devel python-devel openssl-devel
#pip install paramiko