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

Wednesday, 22 October 2014

Getting scrapy to work on ubuntu

This post is for those who are trying to install scrapy on their ubuntu machines.

In order to install scrapy, the following components have to be installed
1)python 2.7
2)pip and setuptools
3)lxml
4)OpenSSl

When you install ubuntu, it also installs python 2.7.X,lxml and OpenSSL along with it (i've tested it atleast on ubuntu 12.04 to 14.04). In certain cases pip is not preinstalled , so you can install it like this:
sudo apt-get install python-pip

One thing that the docs do not talk about is installing the python development package. Trying to install scrapy leads to errors. You must install it prior to installing scrapy by:
sudo apt-get install python-dev


After this you just need to do a
 sudo pip install scrapy

One important point that I would like to mention is that people who connect to Internet by a proxy can still face some problem installing python packages.This problem has an answer here .
TL;DR append the E flag to sudo like sudo -E install scrapy. This makes use of the proxy settings to connect to the package library.

Saturday, 20 September 2014

Accessing dictionary values from a list of dictionaries in django... Wait . What?

This post deals with a question that has already been answered on stackoverflow here . I had to google  a lot, trying to phrase the exact words that would lead to this result. So, I am explaining the answer here as well( apart from the link provided) for myself and the others who find this blog before facing that problem (or it's solution).


I was in a situation in which i had to display the details of some users on the admin page of my django app. The list of users can be obtained in 1 line :

                     users.objects.filter(#myfiltercriteria)

The queryset returned can be transformed to a list of dictionaries with :

                    users.objects.filter(#myfiltercriteria).values()


Now the problem was how to retrieve these values. The simpler way is to retrieving the required values in the view itself and compile it in a list and then use the built in template tag of django for list to access the elements. 

Okay, I maybe wrong depending on the use case. (I was wrong in my case, as I had to retrieve the SAME set of values for all the users.) 

The better and efficient way is to make custom template tags. For the sake of better explanation, let us access the name attribute of the users dictionary passed to a template. 

   So, the custom filter tag is (credits :culebron )
                 
                    @register.filter
                    def get_item(dictionary, key):
                    return dictionary.get(key)

So, inside your template, you would access the names like this :

                   for user in users:
                       {{ user|get_item:item.name }}

This is extremely useful and neat but is not offered by django by default.

For a reference to see how custom template tags are made see this


Thursday, 4 September 2014

Always check the error log

5 hours trying to debug a very simple problem.. I've learned my lesson.

Always check the error log

I cannot emphasize this point more, it can save you lots of time. That's why it is there!

So, I made a web app in Codeigniter framework. All the development took place on my windows computer. Everything went perfect, web app fully functional in on localhost. Then i decided to launch it, on a Linux server (Ubuntu to be precise). I configured the database settings and started the server. The web app uses a login on its first page itself . It displayed the form correctly. I entered the login id and password only to be met b a blank page .I have been greeted many a times by 404 pages, but this was a new guest. A frequent user of web developer console, I quickly found out that it was a 500 Internal Server Problem. After a google search i found that it happens because of some programming  issue on the page/site. This was the first time I thought google was wrog, and this thougt was not baseless.. the same app was running perfectly on my PC. So , i searched here and there and everywhere (:P) . Tried everything , no perfect solution.

Then i posted this question on Stackoverflow here . What i need was just 1 comment (thanks @Prix) .
My error log just threw the syntax error on my face. I was bewildered . Why?
Again google. I found that the code i had written took help of a feature that had been introduced in PHP 5.4 while my server was running PHP 5.2 . The feature is called  function array dereferencing. It means accessing the array values at a particular index, the array being returned by a function. It meant that i had to use a temporary variable to store the returned value of the array ad then access array index from it .

Obviously the other solution was to use PHP 5.4 But to my dismay, the truth was that PHP 5.4 repos are not available for ubuntu 12.04. So i followed the instructions  here . However it did not work and i am still using php 5.2, but without issues.