Tag Archives: python

How to use Python Decorators

Decorators are handy sugars for Python programmers to shorten things and provides more concise programming.

For instance you can use decorators for user authentication for your REST API servers. Assume that, you need to auth. the user for before each REST calls. Instead of appending the same procedure to each call function, it is better to define decorator and tagging it onto your call functions.

Let's see the small example below. I hope it is self-descriptive.

How to use Decorators:

Decorators are functions called by annotations
Annotations are the tags prefixed by @

### Decorator functions ###
def helloSpace(target_func):
def new_func():
print "Hello Space!"
return new_func

def helloCosmos(target_func):
def  new_func():
print "Hello Cosmos!"
return new_func

@helloCosmos # annotation
@helloSpace # annotation
def hello():
print "Hello World!"

### Above code is equivalent to these lines
# hello = helloSpace(hello)
# hello = helloCosmos(hello)

### Let's Try



Passing multiple arguments for Python multiprocessing.pool

Python is a very bright language that is used by variety of users and mitigates many of pain.

One of the core functionality of Python that I frequently use is multiprocessing module. It is very efficient way of distribute your computation  embarrassingly.

If you read about the module and got used, at some point you will realize, there is no way proposed to pass multiple arguments to parallelized function. Now, I will present a way to achieve in a very Pythonized way.

For our instance, we have two lists with same number of arguments but they need to be fed into the function which is pooling.

Here we have self cover code:


Project Euler - Problem 12

As a 2 years researcher, I feel a bit rusty to code.  I search a good set of execises to hone my abilities again and I stumbled upon Project Euler. This site hosts increasing number of very well formed algorithmic problems and discussions. It ranges very basic problems to very high level ones, requiring profound knowledge and practice.

After that intro. I want to introduce one of the example question from Project Euler. NOTE THAT, IF YOU ALREADY KNOW THE SITE AND YOU TRY TO SOLVE THAT PROBLEM, DO NOT CHEAT YOURSELF.

Here is the problem statement we try to solve. Continue reading Project Euler - Problem 12


Two way python dictionary

Here there is a simple class implementation of Two way Dictionary that uses from native dictionary class of Python. The main idea of that kind of data structure to reach, but side of the data by using other side as the key. It is like bi-directional relation between items.

A dumb use case:

d = TwoWayDict()
d['erogol'] = 13
print d['erogol']
# outputs 13
print d[13]
# outputs erogol

Here is the class implementation. However, keep in mid that this class uses more x2 memory to keep the data with that functionality.

class TwoWayDict(dict):
    def __len__(self):
        return dict.__len__(self) / 2

    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        dict.__setitem__(self, value, key)

Hope this helps in some hinge 🙂


Simple Parallel Processing in Python

Here is a very concise view of Python multiprocessing module and its benefits. It is certainly important module for large scale data mining and machine learning projects and Kaggle like challenges. Therefore take a brief look to that slide to discover how to up-up your project cycle.

For more info refer to :

de-importing a Python module with a simple function

Sometimes it is eluding to de-importing some of the python modules especially dwelling on IPython since you have changes something on the module and IPython does not see those  changes even you import the module again. The solution of such a problem is to de-import the module and import again. Here is the function that de-import the staff.




Setting up cudamat in Ubuntu Machine

cudamat is a python library that makes you available to use CUDA benefits from Python instead of intricate low level approaches. This interface uses also

Before follow these steps please make sure that you installed a working CUDA library.

  1. Download cudamat from
  2. Compile with 'make' in the root downloaded folder /path/to/cudamat
  3. Set the environment variables to include cudamat in PYTHONPATH to be able to imported by any script. Run followings in the command line.
     export PYTHONPATH
  4. You are ready to use cudamat.

Here is a simple code you might test;

 # -*- coding: utf-8 -*-
 import numpy as np
 import cudamat as cm
 # create two random matrices and copy them to the GPU
 a = cm.CUDAMatrix(np.random.rand(32, 256))
 b = cm.CUDAMatrix(np.random.rand(256, 32))
 # perform calculations on the GPU
 c = cm.dot(a, b)
 d = c.sum(axis = 0)
 # copy d back to the host (CPU) and print
 print d.asarray()

Note: If you get any other path problem, it would be related to CUDA installation therefore check environment parameters need to be set for CUDA.