Skip to content

Instantly share code, notes, and snippets.

View lvidarte's full-sized avatar

Leo Vidarte lvidarte

View GitHub Profile
@lvidarte
lvidarte / mula.py
Created December 4, 2009 16:54
aprendiendo a multiplicar
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, random
if len(sys.argv) > 1 and int(sys.argv[1]) > 0:
base = int(sys.argv[1])
else:
base = 0
@lvidarte
lvidarte / fibonacci.py
Created March 19, 2010 14:00
fibonacci recursion
>>> def fibo(a=0, b=1, total=25):
... if total == 0:
... return
... print a
... a, b = b, a + b
... fibo(a, b, total-1)
...
>>> fibo()
0
1
@lvidarte
lvidarte / fibonacci2.py
Created March 26, 2010 18:45
fibonacci recursion 2
#!/usr/bin/python
def fibo1(total=10):
a, b = 0, 1
while total > 0:
print a
total = total - 1
a, b = b, a + b
def fibo2(total=5, start=0):
@lvidarte
lvidarte / decorators1.py
Created June 30, 2010 13:43
decorator with argument
def change_doc(docstring):
def decorator(func):
func.__doc__ = docstring
return func
return decorator
@change_doc('new docstring 1')
def foo():
'''original docstring'''
pass
@lvidarte
lvidarte / decorators2.py
Created June 30, 2010 14:05
deprecated decorator
import warnings
def deprecated(func):
def new_func(*args, **kwargs):
warnings.warn('This function is deprecated', DeprecationWarning, 2)
return func(*args, **kwargs)
return new_func
@deprecated
def sum(a, b):
@lvidarte
lvidarte / decorators3.py
Created June 30, 2010 15:39
decorator with functools.wraps
from functools import wraps
def binary(func):
@wraps(func)
def to_bin(*args, **kwargs):
'''convert result to binary'''
return bin(func(*args, **kwargs))
return to_bin
@binary
@lvidarte
lvidarte / cache.py
Created September 3, 2010 20:56
Django cache_timeout decorator
def cache_timeout(timeout):
def cache(view):
def deco(*args, **kwargs):
response = view(*args, **kwargs)
response.cache_timeout = timeout
return response
return deco
return cache
# #Example
@lvidarte
lvidarte / functools__update_wrapper.py
Created November 24, 2011 18:47
Using update_wrapper() copies or adds attributes from the original function to the partial object
import functools
def foo(a, b=2):
"""Docstring for foo()."""
print "called foo with:", (a, b)
def show_details(name, f):
print "%s:" % name
print "object:", f
print "__name__:",
@lvidarte
lvidarte / context_manager_api.py
Created November 26, 2011 12:32
Context Manager API
class Context(object):
def __init__(self):
print '__init__()'
def __enter__(self):
print '__enter__()'
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print '__exit__()'
with Context():
@lvidarte
lvidarte / bom.py
Created November 28, 2011 13:11
Byte-order mark
import codecs
# By Doug Hellmann
# Multibytes encodings, such as UTF-16 and UTF-32, pose a problem
# when transferring data between different computer systems, either
# by copying a file directly or using network communication. Different
# systems use different ordering of the high- and low-order bytes.
# This characteristic of the data, known as its endianness, depends
# on factors such as the hardware architecture and choices made by