Showing posts from September, 2016

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