Monday, 3 November 2014

Saving Passwords in Git

For git version >= 1.7.10
Entering the username and password everytime you do a git push is a waste of time. The following commands will save your credentials for a defined time period in memory.

To store your username and password, use the following commands

git config --global credential.helper cache

By default git stores password for 15 minutes. You can change this by

git config --global credential.helper 'cache --timeout=3600'

(If you want a timeout of 1 hour)

"Gotta" Catch'em All exceptions

So, I was recently doing a webscraping project. I had a database of people and I had to lookup and confirm their identity. Identity in the sense that they had either studied or taught in my college. The task can be done manually for a small set, but I had 7000 records!!

So I used xgoogle for this purpose. It is a Python Library for Google Search.
Now, after having completed the task, I can name a minimum 7 exceptions that can occur when you automate web lookups. They are :
SearchError (class in xgoogle that handles exceptions raised by google during searches)
HTTPError  (generally raised when there is some error in authentication, like trying to access a secured page of an organization, or when the url is http when it really should be https)
URLError (being a subclass of OSError, this error is related to system related problems like input/output . This includes error in fetching data from a url)
SocketError (Raised when there is some issue with the server eg. overload, throttle)
ValueError (Raised when the required datatype conversion is not permitted)
IncompleteRead (When the server closes the connection before the chunk of data has benn fully retireved.)
BadStatusLine (Raised when we request a webpage, and the server responds with a code that Python cannot understand)


Well this is not the complete list.

I had to automate the task in such a way  that I start the code before going to bed and I get it completed when I wake up. So, I decided to catch all :

try:
    my code
except KeyBoardInterrupt:
    raise
except:
 #handles all exceptions except KeyBoardInterrupt

This piece of code will ensure that unless you manually try to end your code, it will not stop.

Sunday, 2 November 2014

Testing your android app on your phone

This is something straight from the source(Google's android resources) and very important, How to test your android app on your phone?

Enable USB debugging on your device.
On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
On Android 4.0 and newer, it's in Settings > Developer options.
Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.

Friday, 31 October 2014

Download Naruto

So, the first release is out https://github.com/light94/naruto

The script will download the latest released naruto episode from narutospot.net. You must have beautifulsoup installed. just add the last seen episode number in naruto.txt and run the script.

Thursday, 30 October 2014

Added Utilities to ubuntu

This post will be a wiki for things that you can add to Ubuntu to make it more easier to use.

1)The good Open in Command Prompt utility from Windows.

Often , there are instances when you are searching for some file using your file manager(genearlly Nautilus) and then you need to do some operation on that file. Now imagine that the directory you are in has this path root/Downloads/Chrome/A1/B1/.... (as long as you can imagine). Now would you like to do so many cd commands or a single cd with a very long directory path. Here's something that is much better  than it.
You can add the open in terminal utility for linux using the following steps :

sudo apt-get install nautilus-open-terminal
Exit the terminal and now right click on any directory you are in. You will find an Open in Terminal  option there. 

Tuesday, 28 October 2014

MRO in Python

In Python, calling the super class' __init__ is optional.
However, if you override the __init__ of a class, you must explicitly call the parent class' __init__ with the current parameters.

This is done by :

super( nameofclass, self ).__init__() 



Python however supports Multiple Inheritance ,i.e a class can have more than one super class.

The MRO(Method Resolution Order - the order in which base classes are searched when looking for a method) is as follows .


1)Depth First Left to Right.
2)If any class is repeated in this search, all but the last occurrence of the class will be deleted.
3)Reject any class that has an inconsistent ordering of base classes.

Thursday, 23 October 2014

Cleaning the Python Terminal

This article is about the different methods that can be used to clear the python terminal

The first method is like this,

Ubuntu :
 import os
 os.system('clear')    


Windows:
import os
os.system('cls')




Second Method:

print chr(27) + "[2J" //python2

print(chr(27) + "[2J")  //python3

Note that this method is only for ANSI Terminals.

27 is the decimal representation of the escape key. So ESC[2J is recognized as ANSI clear command.

Third Method

just keep printing new lines like

print '\n' * 80