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
#!/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 |
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 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 |
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
#!/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): |
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 change_doc(docstring): | |
def decorator(func): | |
func.__doc__ = docstring | |
return func | |
return decorator | |
@change_doc('new docstring 1') | |
def foo(): | |
'''original docstring''' | |
pass |
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 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): |
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 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 |
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 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 |
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 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__:", |
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 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(): |
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 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 |
OlderNewer