How to use Python Decorators

2016-08-04 09:13:22 erogol

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


Posted in: CodeBookProgramming Languages and FrameworksPythonTips & TricksTagged in: codebookcodingpython Read more... 0 comments

Timeout function if it takes too long to finish in Python

2016-06-15 14:35:11 erogol

Posted in: CodeBookCoding as enginner...Tagged in: codecodebookpythontimeout Read more... 0 comments

Setting individual learning rate per layer in Torch

2016-06-02 15:25:15 erogol

Posted in: CodeBookTagged in: codeexamplelearning ratetorch Read more... 0 comments

Pull all repository with all submodules

2016-02-03 11:21:41 erogol

When you use a git repository with submodules, you need to pull all of them at once to keep the unity. Following call does it for you.

git submodule foreach git pull origin master

Posted in: CodeBookTagged in: codecommandgithub Read more... 0 comments

Foreground Extraction and Contour Detection with OpenCV 3

2015-07-16 13:36:28 erogol

Posted in: CodeBookTagged in: codeexampleopencvtutorial Read more... 0 comments