"Elapsed decorator."

import datetime

def elapsed(func):
    "Elapsed decorator"
    def _wrapper(*args, **kwargs):
        "Decoration function"
        start = datetime.datetime.now()
        ret = func(*args, **kwargs)
        print("Elapsed time", datetime.datetime.now() - start)
        return ret
    return _wrapper

"""
Explanation: Java vs Python
Who am I?

Cesare Placanica
Cisco Photonics NMTG group.
Java developer since 2010 (Java 1.5/1.7, Spring, Hibernate and Tomcat).
Previously C/C++ embedded RTOSes.
Python Wannabe since 2004.

Agenda

Round 1 AOP vs decorators.
Round 2 Generics vs Duck Typing.

Round 1 Aspect Oriented Programming vs Decorators
http://stackoverflow.com/questions/4551457/python-like-decorators-in-java
Problem Definition
Encapsulation of cross-cutting concers. Logging, AAA, Caching, Profiling.
<img src="burger.png" alt="Drawing" style="width: 400px;">
Java
A Spring Aspect Oriented Programming example.
https://github.com/keobox/dojokaffe/tree/master/fp/spring-elapsed
Code walkthrough in Eclipse.
Python
End of explanation
"""


# Usage:

import time

@elapsed
def task():
    "A difficult task."
    print("Processing...")
    time.sleep(2)

task()

"""
Explanation: The former was both a Closure and a High Order Function. Did you heard about Functional Programming?
End of explanation
"""


class Duck:
    def quack(self):
        print("Just a crazy, darn fool duck!")
        
class Man:
    def quack(self):
        print("Are you crazy?!")
        
def porky_pig_shoots_a(quacker):
    quacker.quack()

duffy = Duck()
cesare = Man()

porky_pig_shoots_a(duffy)
porky_pig_shoots_a(cesare)


"""
Explanation: The previous was the built-in "Decorator" syntax.
Battery Included!!
Round 2 Generics vs Duck Typing
Problem definition
Write "Generic" code.
OK, less naive, in Java, write code in terms of types to-be-specified-later that are checked at compile time when instantiated by the client of a "parametrized" class.
Duck Typing it's a sort of Generic Programming where object's suitability is determined by the presence of certain methods and properties, rather than the actual type of the object.
End of explanation
"""


