Dropbox As Wormhole Between Ubuntu & Google Drive!

I use Ubuntu on multiple computers everyday. It is important to keep various files synced across them. I keep most of the public data in github repositories and it is very easy to sync across multiple machines.
Google Drive gives 15GB of space with free account. I have multiple accounts and that space is enough to backup my data. I have been storing lots of files in Google Drive from past few years. The main problem is that Google Drive doesn't have a Linux client. To add files, I need to open a web page and then select files to upload and wait until its done. There are few 3rd party clients and none of them work well. So, it is very difficult to sync them across multiple computers.
Dropbox gives 5GB of space with free account. It has Linux client and it works very well. But 5GB of space is not sufficient to store my data.
It is quite difficult to pick one among them as I need benefits of both for free.
Even though I have lot of data, I dont want all of it to sync to all computers everyday. Nearly 90% of data is just backup. Only 10% of data needs to be synced between multiple computers. So I decided to use dropbox as a frontend to google drive.
I have created 2 folders, say Daily & Backup in Dropbox.
Whatever I add to Daily folder will be in Dropbox and it will sync across multiple machines.
There is an amazing service called IFTTT which will help you to connect two products/apps. I found a IFTTT recipe which will add dropbox files in one folder to Google drive.
I have connected Backup folder in Dropbox to Google Drive. Whenever I add a file to Backup folder in Dropbox, it gets added to Google Drive. Once in a week, I will delete files in Backup directory so that Dropbox won't run out of space. So Backup folder will serve as a wormhole to transfer files into Google Drive from my system.

Since I have multiple Google accounts, I have created multiple folders in Dropbox and all folders will be synced to corresponding Google Drive folders.
This setup works well for me as I have Dropbox's Linux client and sufficient storage to backup data to Google Drive.

Book Review - A Psycho Path By Haris Ibrahim K. V.

I stumbled on haris blog and I am thrilled to find that he wrote a book called A Psycho Path. Since ebook was free, I downloaded it and started reading.
I skipped ForewardPreface and skimmed through the first story Etsuko’s picnic.
The second story in the book is A surprising blood test. The story is quite impressive and it hooked me in. Before that story I was lying on the bed and reading. After reading it I got up, sat properly and started reading. Among all the stories, I liked it the most.
Sound relations is another article which I found very fascinating. I read the article many times... 10 times? 20 times? may be more. Long before reading the book, me & my friends had a similar discussion while discussing about impact of music. This article gave an interesting conclusion to our discussion.
Narration of Quito San is interesting.
The book has ~20 stories/articles. Most stories are short stories(2-4 pages). The Prophecy of Amelyah and Quito San are medium sized stories(~20 pages).
Overall the book is very good.
I got a print book after reading ebook. I would recommend getting a get print book from Pothi or Amazon. You can also download free ebook here.

PyCon India 2015 - First PyCon Experience!

PyCon India 2015 was my first PyCon as an attendee, speaker & volunteer.

When I first heard about PyCon in BangPypers meetup, I thought PyCon India is just like a big meetup with a little bit of planning & preparation. But by the end of conference, I have realised that organising PyCon is a very very huge task. It needs a great deal of preparation, planning up things and dedicated volunteers who are interested and patient enough to spend lots of time to run it.

Nicholas Tollervey was keynote speaker. He came to venue on October 1st(two days before conference) and helped volunteers with some tasks. He did the same on next day too. I was wondering whether he came for key note speaking or volunteering.

I have been talking to many people on mailing lists, github and other channels. Finally I was able to meet & talk with many of them in person.

I was not able to attend all sessions but many people said this


It was good to see some of 2nd & 3rd year B.Tech students, who were familiar with django & were contributing to many upstream packages. Some students were even using Emacs as their editor.

Toddlers were also interested to attend PyCon



I have planned to make Python logo mosaic with rubik cubes. I thought it will take ~10 hours to complete it. When I got there with cubes, Vignesh U, Apoorva, Ram Mohan, Sayan Chowday and many other volunteers came forward and completed it in an hour.




I was eagerly waiting for PyCon India from past few months. PyCon India was just like a cool breeze! Ah...!!

Update:

How the hell did I forget about swag kit marathon?

Day before conference, we had to prepare swag kits for participants. I don't know whose idea it was to prepare it like this, that but it was brilliant and most fun part.



Set Emacs As Default File Manager In Ubuntu!

In this post, lets see how to do this


I am using Ubuntu and it has nautilus as default file manager. You might need to use some other commands based on your OS.

File Manager:

If you are in terminal, to open file manager, you need to run nautilus. If you want to open file manager in specific folder, you can run nautilus /foo/bar
When you have downloaded some files using browser, if you click Show in folder in downloads page, it will open nautilus with that specific folder.
We can also make Emacs to act as file manager in dired mode. If you run
$ emacs -q --eval '(dired "~")'
In terminal, emacs starts with a dired buffer for loggined user home directory.
Now lets write a small bash script to do the same thing.
#!/bin/bash
#!/usr/bin/bash
if [ $# -gt 0 ]; then
dirname=$1
else
dirname=$HOME
fi
emacsclient -c -eval "(dired \"$dirname\")" > /dev/null
Save this file as emacsfm in /usr/local/bin. Now you can run emacsfm in terminal which open home dired buffer or you can run emacsfm /foo/bar which will open /foo/bar dired buffer.
Now lets set, emacsfm as default file manager. For that first we need to install exo-utils.
$ sudo apt-get install exo-utils
and run it
$ exo-preferred-applications
It opens a new window like this.
Go to Utilities -> File manager, select Other and goto /usr/local/bin and select emacsfm from it.
Now if you go to browser downloads and click on show in folder, it will emacs dired.

References:

[Python] Functions, Methods & Attributes!

First, lets take a look at functions & methods and then define attributes for them.

Functions Vs Methods:

In [2]: def f():
...: pass
...:

In [3]: class C:
...: def m(self):
...: pass
...:
We have just defined a function and a class(with a method in it).
In [18]: type(f)
Out[18]: function

In [19]: type(C.m)
Out[19]: function

In [20]: type(C().m)
Out[20]: method

In [21]: set(dir(C().m)) - set(dir(f))
Out[21]: {'__func__', '__self__'}
As seen above, a function binded to an instance of a class is method and an unbound method is a just a function. Also a method has __self__ and __func__ attributes in addition attributes of a function.

Attributes:

Lets add some attributes to function & method and see how they work.
In [21]: setattr(f, 'state', 1)

In [22]: hasattr(f, 'state')
Out[22]: True

In [24]: getattr(f, 'state')
Out[24]: 1
We can do the same thing with unbound method(which is nothing but a function) also
In [31]: setattr(C.m, 'state', 2)

In [32]: hasattr(C.m, 'state')
Out[32]: True

In [33]: getattr(C.m, 'state')
Out[33]: 2
But we cant do the same thing with bound methods.
In [34]: setattr(C().m, 'state', 3)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-34-1379e94f2f48> in <module>()
----> 1 setattr(C().m, 'state', 2)

AttributeError: 'method' object has no attribute 'state'
When users add attributes to a function, they are stored in its __dict__ attribute.
In [40]: f.__dict__
Out[40]: {'state': 1}

In [49]: C.m.__dict__
Out[49]: {'state': 2}
As we have seen in the beginning, method objects just hold reference to its class(__self__) and function(__func__). But they don't have its own __dict__ to hold custom attributes. So we cannot set custom attributes to instance methods.
But we can get the function that is referenced by bound method and set attribute for it.
In [36]: setattr(C().m.__func__, 'state', 3)

In [39]: getattr(C().m.__func__, 'state')
Out[39]: 3
Also methods provide, special __getattr__ which forwards attribute access to function object. So, this will work
In [52]: hasattr(C().m, 'state')
Out[52]: True

In [53]: getattr(C().m, 'state')
Out[53]: 3
So we can just set attributes to functions & unbound methods just like classes but we can't do it for bound methods.

Django Tips & Tricks #5 - Use for...empty in Django templates

It is quite common to send a query set from a view to template, loop over it and display items in it. If no items are there, it is good if we display some message. For that we can do something like this.
{% if books %}
{% for book in books %}
<li>{{ book }}</li>
{% endfor %}
{% else %}
<li>Sorry, there are no books.</li>
{% endif %}
Django {% for %} tag takes an optional {% empty %} clause which will be rendered when queryset is empty. So we can rewrite the above code as
{% for book in books %}
<li>{{ book }}</li>
{% empty %}
<li>Sorry, there are no books.</li>
{% endfor %}
This code is much cleaner than previous one. I tried to profile both code blocks. The first block took 0.9ms and second block took 0.8ms which means it is 11% faster than previous code :)
Reference: Django docs

Django Tips & Tricks #4 - Make Django Development Server Persistent

I use Emacs for writing Python code. I also use real-auto-save to save my files after 10 seconds of inactivity.
While coding, when I stop writing in the middle, after 10 seconds Emacs auto saves the file. Django recognizes this & reloads the development server.
Once I complete writing code, I go to browser & refresh the page. Since the code I was writing was incomplete somewhere in the middle and had some errors, Django development server was stopped and page won't reload. Now I have to go back to terminal and restart the server and again go back to browser to refresh page.
To overcome this, Django server must be made persistent. The easiest way to accomplish this is to use a simple bash script as described here.
$ while true; do python manage.py runserver; sleep 4; done
When Django development server stops, after 4 seconds it tries to start automatically and goes on forever.
Instead of typing that everytime, it better to write this as a shell script and put it in system path, so that it can be used in any project.
while true; do
echo "Re-starting Django runserver"
python manage.py runserver
sleep 4
done
Save this & make it executable(chmod +x), use it is ./filename.sh and to stop use Ctrl-c Ctrl-c

Git Etiquette: Meaningful Messages & Linear Logs!

Why Elegant Git Logs Matter?

I will show You few commit messages & logs from two different projects. Based only on those messages/logs, You need to decide which project is the best.

Project 1:





Project 2:





I hope that explains why We need  to maintain elegant git logs.

How To Maintain Elegant Logs?


First let's see how to write good commit messages. There are several blog  posts  & guides about this. Here is the summary of all those taken from this article.

1. Separate subject from body with a blank line
2. Limit the subject line to 50 characters
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how

If You are doing something hacky, writing a little message in commit on why you did that will help other developers to understand that a lot better. Even if You don't follow all rules, some of them will help you in writing great commit message next time. If You are mainainter of a project, ask Your team members to write meaningful messages.

The next that need to be taken care is commits. If a team member or someone else has submitted a pull request, once you merge the commit, just rebase it to remove unnecessary merge commits from log. Also make sure to squash  redundant commits and You have almost a linear log.

A side benefit of this is, You can see recent commit there instead of merge commit, if your project is hosted on github.


The two minutes You spent on writing a meaningful message or maintaining linear log will
will help You a  lot few months down the line & will save countless hours and sleepless nights for future contributors.


Read more articles about Technology!

Automatically PEP8 & Format Your Python Code


Even if you don't intend anybody else to read your code, there's still a very good chance that somebody will have to stare at your code and 
figure out what it does: That person is probably going to be you,
twelve months from now[1].
or as Zen of Python says "Readability Counts".

PEP8 Style Guide:

Since code is read more than it is written, it is very important to make sure that the code is readable. Python has coding convetions(PEP8 style guide) to maintain consistency in code and thereby making it more readable.

There are lot of tools (like pep8, flake8, pylint) to check if Your code is in compliance with PEP8. Today most of the IDE/text editors have plugins which check for these errors and report it on the fly. If You are writing code from scratch, You can make sure that You are following coding conventions.

However once in a while You have to read/bug-fix other people's code who don't follow these conventions(or You might even stumble across Your old piece of code). Once You are accustomed to style guide, it will be uneasy to read inconsistent code. Here is a badly formatted Python file opened in Emacs editor(with flymake & flake8 configured)


Custom Style Guide:

If You don't like any of PEP8 rules, You can set Your own style guide for Your project. Most developers find limiting line length to 79 characters annoying. So they set their own line limit. You can even configure PEP8 checkers to take You own guidelines and check the code according to it.

Auto PEP8:

Formatting code to PEP8 style is boring & time consuming process. So instead of manually formatting code, You can use autopep8 package which automatically formats Python code to conform to the PEP 8 style guide.

To install the package run
$ pip install autopep8
If You have a file foo.py as show above, You can run
$ autopep8 --in-place --aggressive --aggressive foo.py
which neatly formats the file like this


Emacs Integration:

As emacs is extremely configurable editor, with just couple of lines of lisp, we can integrate autopep8 to emacs. Just find where autopep8 is installed; write a function which runs autopep8 on current buffer; bind a key to that function; set a hook, so that when ever You save a file, it will be automatically run the function.

This works reasonably well. But I do recommend using Elpy for Python developemnt in emacs. I have just sent a pull request to integrate autopep8 with elpy.

Formatting:

PEP8 formatters like autopep8, pep8ify will remove only lint errors. But they can't beautify code. 
little = more[3:   5]

x = {'a': 37, 'b': 42,
'c': 927}
Above code remains same after pep8ifying also. But the code doesn't look good yet. You can use formatters like yapf, which will format the code even if the code is PEP8 compliant. 
Above code will be formatted to 
little = more[3:5]

x = {'a': 37, 'b': 42, 'c': 927}
Some times this even destroys Your manual formatting. For example 
BAZ = {
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
}
will be converted to 
BAZ = {[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]}
But You can tell it to ignore some parts.
BAZ = {
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
} # yapf: disable

Beyond PEP8:

As we have learnt to auto PEP8 code, it is the time to look beyond PEP8. There is a good talk by Raymond Hettinger on that - Beyond PEP8, Best practices for beautiful intelligible code.

Let Machines take care of PEP8 & Let Humans look Beyond PEP8.

References:

[1] Code is read much more often than it is written, so plan accordingly.
Python - PEP8 style guide.
Autopep8 - formats Python code to conform to the PEP 8.
PEP8ify - modifies python source code to conform to PEP8.
Elpy, the Emacs Lisp Python Environment.


Read more articles about Python!
Read more articles about Emacs!