Search

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: