This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_path_length(x, T, e): | |
"""The function returns the path length h(x) of an instance | |
x in tree `T`. | |
here e is the number of edges traversed from the root till the current | |
subtree T. | |
""" | |
if is_external_node(T): | |
# when T is the root of an external node subtree | |
# we estimate path length and return. | |
# here c is the function which estimates the average path length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int area(int length, int breadth) { | |
return length * breadth; | |
} | |
float area(int radius) { | |
return 3.14 * radius * radius; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def area(radius): | |
return 3.14 * radius ** 2 | |
>>> locals() | |
{ | |
... | |
'area': <function area at 0x10476a440>, | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getfullargspec | |
class Function(object): | |
"""Function is a wrap over standard python function. | |
""" | |
def __init__(self, fn): | |
self.fn = fn | |
def __call__(self, *args, **kwargs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def area(l, b): | |
return l * b | |
>>> func = Function(area) | |
>>> func.key() | |
('__main__', <class 'function'>, 'area', 2) | |
>>> func(3, 4) | |
12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Namespace(object): | |
"""Namespace is the singleton class that is responsible | |
for holding all the functions. | |
""" | |
__instance = None | |
def __init__(self): | |
if self.__instance is None: | |
self.function_map = dict() | |
Namespace.__instance = self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def area(l, b): | |
return l * b | |
>>> namespace = Namespace.get_instance() | |
>>> func = namespace.register(area) | |
>>> func(3, 4) | |
12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def my_decorator(fn): | |
"""my_decorator is a custom decorator that wraps any function | |
and prints on stdout the time for execution. | |
""" | |
def wrapper_function(*args, **kwargs): | |
start_time = time.time() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def overload(fn): | |
"""overload is the decorator that wraps the function | |
and returns a callable object of type Function. | |
""" | |
return Namespace.get_instance().register(fn) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get(self, fn, *args): | |
"""get returns the matching function from the virtual namespace. | |
return None if it did not fund any matching function. | |
""" | |
func = Function(fn) | |
return self.function_map.get(func.key(args=args)) |