Posts

Showing posts from March, 2016

Project Holy_Python_Grail_Quest - To insert multiple values in multiple keys of an Ordered Dictionary

So, I have been progressing on my Pythonic Quest. On this quest, I have learnt and found about many libraries and functions. Also, I have come across some interesting problems like the one mentioned in the title. It serves as a temporary stop gap in my application until I implement sql or something better in my application. So, my requirement is an ordered dictionary having multiple keys with multiple values. This ordered dictionary acts an SQL table having primary key. I hope you get an idea about what I'm talking about. So, given below is the way I implemented it:  from collections import OrderedDict  #First initialize OrderedDict using its constructor  a = OrderedDict({'name':['James','Bond'], 'id':[101,102']}) #Define keys key1 = "name" a.setdefault(key1,[]) a[key1].append( 'Max') key2 = "id" a.setdefault(key2,[]) a[key2].append(103) Output will be: OrderedDict ( [ ( 'id', [101,

Project Holy_Python_Grail_Quest - Argparse

In this post, I'll be sharing my notes on Argparse module on python. I've come across argparse module while working on my project. After googling, I've found this great tutorial at official python website:  https://docs.python.org/2/howto/argparse.html . If you go through this, you'll get a very good idea about argparse and its usage. I'm just posting this as a summarized version of what I've understood from the tutorial. Anyone can go through this for quick reference. Introducing Positional Arguments :  import argparse parser = argparse . ArgumentParser () parser . add_argument ( "square" , help = "display a square of a given number" , type = int ) args = parser . parse_args () print args . square ** 2 add_argument - To specify command line arguments the program is willing to accept parse_args() - returns some data from the options specified help - gives an idea about what the argument does a

Project pyCountVids - Python Script To Count Total Videos in a Directory

So, I've started working on my long awaited project by implementing it in small increments. First, I've decided to write a python script that counts total videos in a directory and its sub-directory. #!/usr/bin/env python2 import os import sys rootDir = sys.argv[1] totalVideoCounter = 0 videoExtension = [".3g2", ".3gp", ".asf", ".asx", ".avi", ".flv", \                         ".m2ts", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", \                         ".rm", ".swf", ".vob", ".wmv", "m4v"] for dirPath, subdirList, fileList in os.walk(rootDir,topdown=False):     dirName = dirPath.split(os.path.sep)[-1]     videoCounter = 0         for fname in fileList:       if fname.endswith(tuple(videoExtension)):             videoCounter +=1             totalVideoCounter +=1     if (videoCounter > 0)