Posts

Showing posts from February, 2018

Python programming language : quick and easy recipes

Something I used sometime and thought it should be shared with others. List only files from a directory - ignore all subdirectories import os destdir='/var/tmp/testdir' files = [ f for f in os.listdir(destdir) if os.path.isfile(os.path.join(destdir,f)) ] Print first colums of a file e.g. 3 colums of /proc/diskstats  DISKSTATS_FILE = '/proc/diskstats' diskstats_file = open(DISKSTATS_FILE, 'r') table = [line.rstrip().split() for line in diskstats_file.readlines()] newtable = [[line[0]]+[line[1]]+[line[2]] for line in table] newtable_3coulms = '' for i in newtable:      if i == newtable[0]:       newtable_3coulms = ' '.join(i)     else:       newtable_3coulms = newtable_3coulms +  '\n' + ' '.join(i) print newtable_3coulms diskstats_file.close()

python - what is decorators

Python decorators  USING FUNCTION AS DECORATOR In simple words, it decorates (beautify)  a callable objects (function, methods or class)! In a technical word, it takes a callable object (say function ) and extends (decorate) behavior of called object. Let us create a decorator function -  new_decorator decorator function 1- take another function as arguments - func_argument 2- have a wrapper function -  wrap_func 2- return wrapper function -  wrap_func Decorator function is preceded with  @ sign when called ( i is called pie syntax). It follow with the function need to be called -  my_function 1 def new_decorator( func_argument ): 2     3    def wrap_func (): 4        print 'Code here before executing function' 5         6        # then execute function that has been passed 7         func_argument () 8 ...

How does yaml data look when converted in python data structure?

You are a python developer and want to know how does yaml data looks when converted to python to understand data in better way, here is some reference PYTHON CODE import yaml from pprint import pprint yaml_data_file = '/home/nansari/data.yaml' read_data = open(yaml_data_file, 'r') python_data = yaml.load(read_data) pprint(python_data, width=8) SAMPLE YAML DATA Add below in file pointing to above yaml_data_file python variable --- filedata:     -          name:              - /home/nansari/puppet_test1              - /home/nansari/puppet_test          owner: daemon          group: pulse          mode: '0644'          ensure: file     -          name:              - ...

snippet of urlib3 code in python3

Python urlib3 is different than urllib and urllib2 Python module. Here is snippet of urllib3 code. import urllib3 def fetch_words(): url = 'http://sixty-north.com/c/t.txt' story_words = [] r = urllib3.PoolManager().request( 'GET' , url) story = r.data for line in story.decode( 'utf-8' ).split( ' \n ' ): line_words = line.split() for word in line_words: story_words.append(word) return story_words def print_words(): for word in fetch_words(): print (word) if __name__ == '__main__' : print_words() Ref : https://urllib3.readthedocs.io/en/latest/