Search

21 December, 2018

Python Global Variables

The keyword "global" has a special purpose . If you look into the help docstring of this keyword, you must notice this line :

 It would be impossible to assign to a global variable without "global " 

The word to note is "Assign"

Now . In python , if you declare a variable outside the scope of all functions or classes, it is AVAILABLE GLOBALLY. Meaning I can use it anywhere anytime.

But what is NOT clear , is , you CANNOT change the value\contents of this variable.

Bringing statefulness into your program (e.x an integrating counter ), might need you to keep updating some variable's value . That means, functions must be able to change that value.

Hence, the keyword "global" .

04 December, 2018

Production server launch using Django and Apache and mod wsgi

Some of us develop Django applications which can be used inside Intranet for various purposes.
Some ideas are:
  • HRMS apps
  • Interview Management Tools
  • Grievance Management Tool
  • Financial Report Generators. 

These apps running on intranet, generally don't face too much traffic at a time, but they do need to compute or face heavy queries which is why, it's a good idea to release them on a proper environment . Typically they are light on UI. I choose to use bootstrap which is enough to achieve a decent look . 
Still you should serve them using something which is good at serving static content. So I chose Apache. Lets get started. 

31 October, 2018

Migrating your Django Project database from SQlite3 to MySQL

Django comes with an in-build default setup for SQLLite3 which is good for learning and minor static page based websites. Like blogs which is a standard example . But as soon you decide to create projects that might entail multiple users performing different operations at the same time, SQLLite is not recommended.

Hence the decision to migrate to a better DB system must be as early as possible. I had to under go a similar experience recently . It look a lot of time, trials to finally get it working . I am going to share you the complete steps assuming you don't even have a MySQL setup .

22 October, 2018

Quick guide to Logging to a file using Python

This is for setting up of a file logging system using Logging module


Step 1: Import the logging module.



import logging


Step 2: Create a logger for yourself. 


logger = logging.getLogger(__name__)


19 September, 2018

Working with context processors and WITH statement

We use. We forget. This is for myself so I remember the magic called Context Manager. You already must have used the "with statement" for file handling, threads, socket and other operations. I am going to talk about creating you OWN context managers.

The magic operator to be used is : @contextmanager. It can be imported from the module "contextlib"

To create a context manager, use the decorator contextlib.contextmanager


@contextlib.contextmanager
def getListCounts(x):
    try:
        from collections import Counter
        yield Counter(x)
    finally:
        print 'No of elements in the list : %d' % len(x)

11 September, 2018

Regular Expressions. Lets not be greedy.

Recently faced a situation when I wanted to extracted parts of a html code. But then I realized, I have no control over the amount of information regex is giving me back.

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 .

A better example of Class Inheritance in Python

Lots of people ask me, why should they use classes at all ? Well. I think its about control, and ease . It solves the headache of data sharing and accessibility. I am going to show you an example , which hopefully proves that classes and fundamentals like inheritance  are brilliant.

29 August, 2018

Python: Saving regular expression results in variables directly

Python Regular expressions has a powerful tools under it's regular expressions module which lets you create variables and save your search results directly.

It is called named groups . When you search for something and you want to save it in a variable when found, this is what you should use.

Setting your first Jenkins project.

Setup Jenkins on your local PC in minutes


Since you are here, I am assuming that you are trying to find out ,  how to start with Jenkins and you have no clue about it . If it's true, then you are in the best place possible .

This is a blog on Jenkins setup for those guys who have absolutely no idea what to do or where to start.

24 August, 2018

Regular Expressions: Look ahead and Look behind

Regular expressions are fun. There are two important concepts that are quite often sought for. Find something based on content that follows our search pattern . OR. Find something based on content that lies ahead. Let's see some examples to see what I mean.

LOOK AHEAD

LOOK AHEAD means , I want to see what lies ahead first, and then decide, to do something now.


Syntax of regex pattern:  pattern1(?=pattern2)


14 August, 2018

Calling Flask URL from a script tag

The goal is , to call a FLASK route without reloading the page, when a button is pressed. The content of a div must change with the response text received from the flask route call.

13 August, 2018

How to use Python unit test module and it's methods?

Unit test module is an in-build library in python, used to write test scripts . It gives some helpful methods while helps us designing out test in a way , that emulates a real production environment.

While writing test, we need to keep these things in mind

1. Test should start with a clean slate. Meaning the subject under test must not hold any info or be in a state, that might affect the test results.

2. Every tests should test one and only one feature . Clubbing multiple tests in one test,  is a bad idea.

Unit test provides some special methods. These can be used to handle the flow of tests as per scenario. Below is a generic template of a unit test showing all methods and their tasks.

16 July, 2018

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.

25 June, 2018

How to test POST call using Python?


I got into a situation recently where I needed to test if my POST function was indeed working. 
    Its easy to test GET method . You just visit the URL and that's it . But for POST, you an use Plugins. But unfortunately I can't do that , because my browser (admin restrictions ) doesn't allow me to install plugins . Fortunately if you have Python , you can easily do that .


How to work on a Python Project when modules versions needed are different? Use virtualenv



Gone are the days when you need to create a VM for working on a different set of tools.  People work on different projects which might need a special version of a module. (Or python itself) . The problem aggravates when  multiple users work on a single shared machine (Test environments or Build machines ) .Virtual Environments help solve problem . And its easy to use and share among fellow teammates.

12 June, 2018

Exploring Numpy

What can we really do with Numpy? Why should we use it at all ?

Start with : import numpy

1. We can create arrays .


method: numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)


>>> a = numpy.array(range(10))
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.dtype
dtype('int32')

In the above array, there are 10 columns and 1 row.

Note : The concept of rows and columns applies when you have a 2D array. However, the array numpy.array([1,2,3,4]) is a 1D array and so has only one dimension, therefore shape rightly returns a single valued iterable.

Refer this link


01 June, 2018

Python List sorting with key argument explored




Everybody uses lists as an array to store values. List provide a lot of in-build features.

  • Sorting
  • Membership
  • Indexing
  • Iteration
  • reversing
  • Adding/Removing
  • Popping
  • Count

Lots of reasons to use lists. I think, one of the most used features is sorting. Internally , python uses Merge sort technique to sort the array items. But the sort method can be used in many other ways to have more control.


29 May, 2018

SVN commands you must know as an Test Automation Engineer


In the course of automation and continuous integration , you will someday come across a stage where you have to perform svn operations . And obviously, using command line (forget fancy UIs to do your business) using command line. And then you will have to start digging in,  which will take a substantial amount time ...believe me.

25 May, 2018

How to use argparse module to create command line tools with Python?



Its been decades I have been using sys.argv for simple tasks related to argument parsing. But the thing is, sometimes we don't realize how good something is unless we use it . Like the argparse module.

The official document got me totally confused. So I wrote my own tutorial. Let's get started.

02 May, 2018

Exploring COLLECTIONS module from Python


The 'collections' module has some interesting methods that give us an edge over generic inbuilt tools . I want to explore them here today and show how you can use them .

namedtuple


Tuples are represented as (item1, item2, item2, ...) . They support indexing and one of the ways to use them is :


>>> from collections import namedtuple
>>> Car = namedtuple('Car','Price Mileage Colour Class')
>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')
>>> print xyz
Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y')
>>> print xyz.Class
Y

They turn tuples into convenient containers for simple tasks. With namedtuples, you don’t have to use integer indices for accessing members of a tuple.

28 March, 2018

How to import and use a font in your website ?



Most people like to use the default available list of fonts . But what if we liked some other font and we want to use it our own website .

There are free downloadable fonts on the web. When you download a zip of it , you generally would receive files of the format 'ttf', 'otf' etc. You can visit the site https://www.fontsquirrel.com and convert them into a format 'woff' .

16 March, 2018

How to use Docker with Python projects



Docker is the modern way to share ready made machines , so other can use it without the usual hassles of configuration and environment setup. I wanted to try it myself first hand, to see what the fuss is about.  I want to share what I found, so you start your project right now.

Before we start, for the absolute beginners who ABSOLUTELY NO IDEA about DOCKER, let me try to explain what it is:

It is a CONTAINER which CONTAINS ALL that you NEED to run your project. 

Dockers are the new generation Virtual Machines which totally remove the need to install another files on a system just to run or test something . They are just CONTAINERS with all the things necessary for our app to work. That's it .

08 March, 2018

Testing GET and POST calls using Flask


I was trying out Flask. In the process, I made a website that behaves like a server. It can help you test GET and POST calls.

It goes like this .

from flask import Flask, request
    app = Flask(__name__)
    
    @app.route('/method', methods=['GET', 'POST'])
    @app.route('/method/<wish>', methods=['GET', 'POST'])
    def method_used(wish=None):
        if request.method == 'GET':
            if wish:
                if wish in dir(request):
                    ans = None
                    s = "ans = str(request.%s)" % wish
                    exec s
                    return ans
                else:
                    return 'This wish is not available. The following are the available wishes: %s' % [method for method in dir(request) if '_' not in method]
            else:
                return 'This is just a GET method'
        else:
            return "You are using POST"


05 March, 2018

Why should we use Class in Python ?


Lots of people ask me, why should they use classes at all ? Well. I think its about control, and ease . It solves the headache of data sharing and accessibility. I am going to show you an example , which hopefully proves that classes and fundamentals like inheritance  are brilliant. Example: I have  two classes in the below program. Person and Manager . Manager has inherited the Person class (because he is a person too....almost (lol)) .


26 February, 2018

GIT Essential Tips


GIT has become one of those tools in e business that you can't ignore anymore. So I suggest you learn to use the basics of the idea called GIT. For my tester friends, as you enter the automation phase, git is going to come around sooner or later. So watch out.



Important everyday command with GIT

1. How to revert to an old commit both on local and remote

git reset --hard HEAD~1

git push -f origin master

2. How to get all branches of a git project

git fetch --all

3. How to delete a branch from local computer

git branch -d name

4. How to delete a remote branch

git push origin --delete branch

5. How to shift changes from one branch to another

git stash
git checkout branch
git stash pop

6. How to create a remote branch from a new local branch

git push -u origin new_branch

Note: -u means, --set-upstream. Refer this.

7. How to see all the existing remote branches

git branch -r

8. How to see all branches (remote & local) 

 git branch -a

9. How to fetch a specific remote branch

This involves multiple steps. Do as follows.

1) First get a list of all remote branches (git fetch -r)
2) Create a branch of that exact remote branch name (git checkout same_branch_name as remote)

10. How to see the diff between two branches

git diff branch_name


11. How to see the diff with names of files only (no details )between two branches

git diff branch_name --name-only


12. How to see last commit details?

git show

13. How to see who changed a file?

git blame filename

14. How to undo a merge?

git merge --abort

15. How to see, what files were changed or created in the last commit ?

git log -p -1 --name-only

16. How to see, what files were changed in a specific commit?

git show --name-only commit_number

(e.g 6021acc151c2b02c364af5fef2bfbbe73c880d38)


17. Remove last commit from branch.

git reset --hard HEAD

18. Remove last to last commit from branch.

git reset --hard HEAD^

19. Make the branch base same as latest master.

git rebase master

20. Retrieve deleted branch from remote

git fetch origin BranchName


21. Force checkout (switch branch ignoring all changes).

git checkout -f other_Branch


22. Solve Merge Conflict (accept your local content or remote)

git checkout --ours filename # For accepting local changes
git checkout --theirs filename # For accepting remote changes

23. Save work without committing and switch to another branch.

In general, if you want to change branch when you have uncommitted changes, git doesn't allow you to do so. For that , you can use something called "stash".

git stash

To recover the stashed files, use :

git stash pop

To drop the stash (you don't want these changes):

git stash drop


24. Merge a specific commit from one branch to another.
 
Sometimes , it might happen that, we only need a SPECIFIC COMMIT from a branch into another branch. Although, this is not a good thing to do, but GIT makes it possible .


Steps:

Find the hash_number for the commit in source branch. (You can use "git log" ) .
Switch to target branch ( Use "git checkout target_branch" ) .
git cherry-pick hash_number .


25. How to create a branch and switch to it in a single step.

git checkout -b your_branch

26. How to see all the git configs set.

git config --list

27. How to see if you need to rebase your branch before pushing changes?

When you created your working local branch from master, you were at the same base commit as master. Now after a few day, probably the master branch has moved ahead of you since others might have pushed new commits into it while you were working .

-----master------------------------my_branch....

After a few day's,


Day 6 --------master--------------------------------------
Day 5 -----commit-new----------------------------------------
Day 4 -----commit-new----------------------------------------
Day 3 -----commit-new----------------------------------------
Day 2 -----commit-new----------------------------------------
Day 1 -----master------------------------my_branch....

A quick way to do it:

$hash1=$(git show-ref --heads -s master)

$hash2=$(git merge-base master add-inventory-arindam)


$ [ "${hash1}" = "${hash2}" ] && echo "OK" || echo "Rebase is required"


28. How to see what changed after you pulled in from remote and saw some files changed?

git diff branch_name@{1} branch_name file_name
Note the {1}

Another way to do it , is use the commits message that looks like :
Updating 647a02393..08c607cff


29. Diff after file has been added to staged area?

If you execute "git diff" after adding your file to the staged area (after git add command), you will not see and results. In order to see the changes made, you need to execute:

git diff --staged filename

30. Move a file from staged area to unstaged area.

If you decided to not commit a file, AFTER you have already moved the file to staged area (by git add filename), you can do so:

git reset HEAD filename

31. Rename a local branch.

If you want to change your local branch name, it is possible.

git branch -m new_name.

32. See what is in the stash.

Sometimes we just want to know, what's in the stash before we pop it out. This can be helpful in many situations.

git stash show
git stash list

To see the diff too,

git show stash


33. Change the commit message for the last (not published) commit in local branch.

git commit --amend -m new_message

#This will change the message of the last commit.

34. Copy a file from one branch to another branch.

While you are in the branch in which you want the file to be.

git checkout source_branch somefile.py

35. Rename a file

git mv file_oldname file_newname


36. Find branch with pattern

git branch --list '*fix_*'      # Search in local branches

                git branch --list -a '*fix_*'  # Search in remote & local


11 February, 2018

Python Decorators with simple examples


Everyone who has used python , has sooner or later come across decorators . That's that thing sitting on a function which magically does something special . It looks like :


@mydecorator
def my_function


Today we are going to learn , how to write decorators of our own . How this works:


22 January, 2018

CNTLM in Office


If you are facing a proxy problem , CNTLM is a general option everybody considers. But the working of CNTLM is not always guaranteed. I myself get confused every time I need to use it.

What problem are we actually talking about?


While working in offices and restricted environments where firewalls and other invisible barriers stop your requests from going out, you sometimes need a way to temporarily jump over that barrier to get your job done . Example: You might face them while trying to install using pip in command line, trying to clone a personal git repository. 

14 January, 2018

How to run a Python program on the internet from anywhere?


I want to run a python program whenever I need and get some information out of it. The problem is, I want to call that through internet. And I want to show the content on a web page or some UI container. That's it. Let put this as a requirement.

Problem: Display some information on my web page calculated by a python program

How do we do it on our local computer? We run our program as : python my_program.py.
So maybe I can do something like: www.mywebsite.com/my_program.py . Can we? No idea.

I expect to collect the response in some variable (no idea how I can create a variable in the first place on html) and then display the contents of this variable. Displaying a simple text on html is known, so that should be easy.