Skip to main content


Quick guide to Logging in different files using Python

This is for quick setup of a logging system . 
Step 1: import logging 
Logging is the build-in module for implementing logging in Python.

Step 2: Create a logger for yourself. 
logger = logging.getLogger(__name__)

Step 3: Set Logger level

Step 4: Create a handler. 
hdlr = logging.FileHandler(settings.BASE_LOG_FILE, mode='w')

Step 5: Create a formatter
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

Step 6: Set formatter and handler


When and why to use Classes in Python

Note: All programming done in Python 2.7

Why to make a class?

When we need to use some common variables across other functions quite often . If we only one or 2 functions to write , we should not need to write a class. But if we are going to have 4-5 functions that need to use common objects between themselves, then its better to use classes.

What are class methods?

Class methods are methods that you can call on the class itself

A good example of classes . Lets have a BankAccount class for managing Bank Accounts and its related operations.

classBankAccount(object):#Class methods are methods that you can call on the class itselfdef__init__(self,balance=0):self.balance=balancedefdeposit(self,amount):self.balance+=amountdefwithdraw(self,amount):self.balance-=amount
The __init__ functionis automatically called when a new instance is created. We have two functions that add or subtract  money from account.

Ok....So lets create 2 accounts

Now lets …

Generator functions. Why and when should we use them.

You probably have heard of them. But as a beginner python programmer, we tend to ignore it's values because we fail to find uses of it. When we have loops and itertools and other fancy toys offered, why should we bother to understand them. If you want to take your programming  or understanding of Python to the next level, you must understand and use generators more and more.

Before we start, I want to make a simple program that prints square of (hence generates and delivers) 5 numbers from 1 to 5. So what is the first idea that pops up ? Let's use a forloop.

defsquare(N): for i inrange(1, N+1): print i**2>>>square(5) 1491625
Let's say, you need all the values generated for some other calculation. I know, above example is too lame . But imagine after some complicated calculations, you have generated some result.
So you probably will return all values together in an array. And to that, the first thought in our minds is, using a LIST. Let's use it. 

Now ou…

Python: Running executables using subprocess module

There are many ways to run a bat/exe file but the most widely used is the subprocess module.
And I would like to clearly show, how exactly to do that. When should you use what arguments.
I am going to specifically talk about the check_output module .

Note : This function is used when we want the output of the bat file collected inside a variable.

Condition 1: The exe file is in the same location as of the python script that wants to run it.

You are in a Folder > MyPrograms  . And the bat/exe file lies inside this folder. (Refer picture below)

Content of the hello.bat file

@echooffecho"This is a test bat"

My script contents:

importsubprocess result = subprocess.check_output('hello.bat') print result

Now if you run it , the result will be a:

"This is a test bat"
This is easy. Let's look at another condition.

Condition 2: The exe file is in a different location from the python script that wants to run it.

No change in contents of hello.bat file…

Regular Expressions with Python: Look ahead and Look behind

These two important tools have been bugging me every time I come up with a situation. Because I never remember this one. Blame my memory.
So it's time I cement this idea somewhere . And no better place than a blog page.

LOOK AHEADLOOK AHEAD means , I want to see what lies ahead first, and then decide, to do something now.
Syntax of regex pattern:pattern1(?=pattern2)

Where Pattern1 is the pattern for the part that we ACTUALLY want to capture. Pattern2 is the pattern which needs to be found as MANDATORY. Logically speaking, 
IFPATTERN2isFOUND, then print/get/capture/show PATTERN1

Example: Let there be a string "Hello World"
Aim: I want to find 'Hello' ...ONLY IF its followed by 'World''\w+(?= World)', 'Hello World').group()
Result : >> 'Hello'
 The \w+ is Pattern1 The 'World' is Pattern2 r'\w+(?= World)' means, Find anything which fits \w+ IF it is followed by 'World'
The Pattern2 …

Kerala : How to plan for a lifetime's experience

I have been planning this for months and it finally came along in the best time. A full stretch Kerala trip with Kanyakumari as an added bonus. I will not tell you what to do, but HOW to do it. How to plan your dream Kerala Trip. 

Caution: Mine was too comprehensive. Needs high energy and will be quite tiring. 

I chose the monsoon theme..I wanted to be in the middle of the action. This is the only place in India which offers monsoon as a tourism opportunity. I was lucky. I expected super heavy rains. Instead. I ended up right in between the rainy season and summer which was Perfect.

Advantages in travelling in mid AugustRainfall almost over.Waterfalls at their fullest.Greenery all over the state.Backwaters, beach sides and rivers all very clean.Abundant coconuts to enjoy.Hotel rates brought down.Less crowd everywhere.Festive Preparation going on.Shopping becomes pocket friendly.Our route map:

Day 1 Day 2 Day 3 Day 4 Day 5 Day 6

Regular Expressions with Python: Examples of common functions

Regular Expressions are tools to filter and grab information from a heap of strings. Python has got a lot of toys to deal with it. The module "re". I am going to give you a quick ride through it. The ones that we use/gonna use the most.

So just immediately launch your favourite IDE and type "import re". Let's get started.

for the object r where r =, subject), r.start() - Gives the start point where the match was found. r.end() - Gives the end point where the match was found. - Returns the full matched string - Returns the matched string for the 1st group - Returns the matched string for the 2nd group
And so on....

Note : is same as

The number of groups available depends on the number of groups used in your pattern.


>>>regex=r"([a-zA-Z]+) (\d+)">>>,'July 4')<_sre.SRE_Matchobjectat0x0000000002E4D140>>>>r=re.s…