Tag Archives: coding

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!"
target_func()
return new_func

def helloCosmos(target_func):
def  new_func():
print "Hello Cosmos!"
target_func()
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
hello()

 

Share

Some inefficient algorithms you don't need to know!

Here we have some stupidly clever algorithms that are faster in execution but slower in convergence to solution.

First candidate to introduce you is Bogo Sort. Its idea is simpler. Shuffle the numbers until it finds the correct order. Complexity of the algorithm can be approximated as O(n.n!) (efficient hah ?). Its pseudo code can be written as;

while not isInOrder(deck):
    shuffle(deck);

Next one is known as Stooge Sort. The idea is to sort the initial 2/3 part t Continue reading Some inefficient algorithms you don't need to know!

Share

PHP MVC Frameworks

Zend Framework is a good foundation for everything and can be used also as just a library of various functions.  It is also the closest thing to be  an "official" PHP framework so there are a lot of developers who know how to use it.  It is, however, not a framework you would use to prototype something very quickly.

There are several frameworks that could be used as a quick RAD (Rapid Application Development) tools.

A good example is CakePHP, which is a very popular framework.  Fairly easy to learn.  It has a lot of sensible defaults and naming conventions that make your life much easier, but which you can override. Continue reading PHP MVC Frameworks

Share

What is asynchronous Programming?

Thread Execution
Normal Thread Execution

 

 

 

 

 

asynchtomous execution
asynchtomous execution

Start with the comparison (that assumes you know normal threaded execution).

Two main difference between normal threaded system and asynchronous system are:

  • For threaded execution each thread has its own controller, however for asynchronous system there is only one thread controller.
  • Threaded execution does not give the control of ending, starting, changing to user. It is mainly controlled by the operating system internals. On the other side asynchronous execution need some explicit command to interleave one execution to other. It is more in control in the programmer's perspective.

Continue reading What is asynchronous Programming?

Share

Use yield in Ruby

yield function is one of the good stuff in the Ruby that gives different kind of code reuse logic to your code. It just like lambda in Scheme and Lips. when yield is called in a function it cuts the execution of its container function and pass the execution time to the do-end block then when the execution ends with the end keyword, it continues the execution from the last point.

Here is an example code that uses yield function.

def around_staff
  eren = 20
  puts "first step"
  yield(eren)
  puts "last step"
end

def do_something
  around_staff do |eren|
    puts "I did something around"+ eren.to_s
  end
end

do_something
Share