Writing about Clojure


It’s been a while since I wrote on this blog. Been busy in many things. Recently I decided to start learning Clojure. I thought the best way to keep at it would be to start writing about it. So I decided that I am going to start writing posts every now and then about anything I learn in Clojure.  Inspired by Groovy Goodness I’ll try to work on “Clojure-snips”. Hopefully I will improve with time as I learn Clojure.

Getting Started

Clojure features

I’ll also keep a list of references here. Whatever I referred or helped me in learning Clojure.

Making GUI in Python – Choosing a framework


Making Graphical User Interface(GUI) can be really attractive to many people. I had also been really interested in making GUIs and after using Visual Studio Express briefly for a project I could not forget the ease with which it allowed someone to make GUIs. So in Python I was interested in making GUIs. I had looked at many choices starting with Tkinter the default Python GUI toolkit.  Believe me you don’t want to use it as it does not have a decent WSIWYG editor for building the GUIs. After failing to get anything from TkInter I looked at wxPython but it supported Python 2 only and last time I checked it’s Python 3 support was a Work-In-Progress.

PySide and PyQt looked promising as PyQt Designer is a decent WYSIWYG editors but PyQt needs a commercial license and PySide is kinda dead – community support and tutorials.

Kivy is said to be cross-platform and it has a good tutorial – official documentation has a tutorial for making a game and it has a WIP editor also. I think it needs a little time to mature a bit but using it even now can give you a decent toolkit for making GUI. As per the website it supports Android also so many will find it really attractive.

Django is one other choice. Although it is a web framework but using it can also give you a decent GUI. Just check out its tutorial for the admin website.

Python 3 limits


I had decided that I will port my code from Python 2 to Python 3 and with the help of PyCharm the refactoring that I wanted to do was going completely smoothly. I decided in the middle to try something new. I thought when I was restarting Python then maybe try something new.

While doing this I hit Python 3 limits again. The library support is still not as good as for Python 2.  The first problem that I faced was with mechanize library. That was when I was trying to get some automation scripts done for logging into websites as per a schedule. Everyone wastes a lot of time online and I wanted to control it. Had the schedule but just opening websites by webbrowser module was not good enough. Why not use Python to login also? Mechanize was just what was needed. But it didn’t support Python 3. Shit. I found a solution to that. Using Selenium I did a test run and was able to login into http://projecteuler.net/ using my username and password. That I can work with now. But still mechanize might have been easier to work with.

Just 2 days back I had the idea of trying a bit of AI. Maybe a small bot or something? Natural Language processing for intelligent answers maybe? Maybe some simple  image recognition. Just the flight of thoughts that come and go every now and then. These are not like anything that I have tried before but what’s the problem with trying? I decided to go with Natural Language processing. Shit again. NLTK – the library needed for this had support for Python 2 only.

I was left thinking whether the advice that I got on Python mailing list for using Python 3 for new software actually good? Why actually did I change to Python 3? UNICODE is not something that I have even messed with once in Python. So why? I installed Python 2 but pip was missing. That’s a good thing that they did in Python 3,4, not that it actually helps with half of things not supporting Py3.4 yet.

Anyways I just noted down the ideas that came and decided to thing a bit more before going further. Hope someone reads it and decides to not go with “newest and latest” unless they have the time to port libraries as well.

Trying to learn Haskell and feeling weird


I had started Python with the main aim of breaking into functional programming. Had read someplace wrong that Python is functional programming language. The time spent on learning it and using it was great. I didn’t get what I started out for but learned the ease of using a high level language and a dynamic one. Coming from mainly C background that changed my perspective a lot about programming.

I decided to try it once more, functional programming. This time I am going with Haskell which is said to be the ultimate functional programming language. Let’s see how far I can get. For the 1-2 days that I have actually tried using Haskell it has been a weird experience. In a good way. It’s different somehow. Couldn’t place how.

Placed project C Programming Made a Game on github


I placed the project for which I had made my old blog on github. I had the versions backed up but not via git. Back then I didn’t know about versioning software like git and mercurial. I was cleaning up my codebase and thought that converting the code dumps of previous versions of the software into git would make it much easier to manage the whole thing if I needed.

I also added the C++ programs’s source code which I had used for the project into the github repository. They had helped me back then take care of repetitive tasks a lot. That was kinda the project where I understood the real value of automating tasks and one of the reasons I started using Python.

I don’t think I am going to update it anymore but good to have that on github. It gets backed up. I am thinking about adding the setup files that I have for various versions on github or not. Still not decided on that.

If I ever thought of continuing the project I’ll do that from scratch in Python/PySide which I have been learning for some time.

If anyone’s interested in seeing what the project was just have a look at the old blog.

Automating tasks in Python – Mass renaming of folders present in zip


So Python is getting useful to me for avoiding manual repetitive tasks. I used it to rename a lot of folders. There were around 700 folders in 28 zip files which I needed to rename and re-zip. That would have been really cumbersome if not for Python.

The folders’ name were like  0001 – abc 1 . Only that the numbers didn’t match. The first number was used for arrangement and it was the second number according to which I had to change the first number. Doing that for 700 folders would have been a real pain. Luckily I knew Python and wrote a script for doing this.
Here’s the Python script that I used.

import zipfile
import os
import shutil

CUR_DIR = os.getcwd()
FILES_IN_CUR_DIR = os.listdir(CUR_DIR)

def get_new_name(original_name):
    num = 0
    started= False
    for c in original_name[7:]:
        if not c.isdigit():
            if started is True:
                break
            continue
        else:
            started = True
            num *= 10
            num += int(c)
    return str(num).zfill(4) + original_name[4:]

def extract_files(zip_f, cur_path):
    with zipfile.ZipFile(zip_f, 'r') as zip_file:
        zip_file.extractall(cur_path)

def rename_files(cur_path):
    for files in os.listdir(cur_path):
        new_name = get_new_name(files)
        os.rename(os.path.join(cur_path, files),
                  os.path.join(cur_path, new_name))

def main():    
    files = [f for f in FILES_IN_CUR_DIR
             if f.endswith('.zip')]

    for f in files:
        cur_path = os.path.join(CUR_DIR, f[:-4])
        print('Extracting ' + f)
        extract_files(f, cur_path)
        print('Started renaming')
        rename_files(cur_path)
        print('Removing zip file ', f)
        os.remove(os.path.join(CUR_DIR, f))
        print('Writing back to ' + f)
        shutil.make_archive(cur_path, 'zip', cur_path)
        print('\n')if __name__ == "__main__":
    main()

This is just the first version that I used to get it to work. It creates temporary folders that I needed to delete but that was just one select and delete. Not a big thing. I plan to take care of the temporary folders too. The new one you can find in my github -> general -> Python33 -> Utilities maybe in a few days.