From a9d773f5580bd2975c68e09d2731014ba4b8f281 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Sun, 28 Nov 2010 10:37:06 +0100 Subject: Updated the documentation of decorator 3.2.1 --- documentation3.html | 86 ++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'documentation3.html') diff --git a/documentation3.html b/documentation3.html index 2110f8c..fce2187 100644 --- a/documentation3.html +++ b/documentation3.html @@ -83,10 +83,10 @@ E-mail:michele.simionato@gmail.com Version: -3.2.0 (2010-05-25) +3.2.1 (2010-11-28) Requires:Python 2.4+ -Download page:http://pypi.python.org/pypi/decorator/3.2.0 +Download page:http://pypi.python.org/pypi/decorator/3.2.1 Installation:easy_install decorator @@ -184,7 +184,7 @@ that depends on non-hashable arguments):

func.cache = {} def memoize(*args, **kw): if kw: # frozenset is used to ensure hashability - key = args, frozenset(kw.iteritems()) + key = args, frozenset(kw.iteritems()) else: key = args cache = func.cache @@ -211,7 +211,7 @@ general memoize_uw returns a function with a
>>> @memoize_uw
 ... def f1(x):
-...     time.sleep(1) # simulate some long computation
+...     time.sleep(1) # simulate some long computation
 ...     return x
 
@@ -232,10 +232,10 @@ pydoc will tell you that the function accepts a generic signature *args, **kw, but when you try to call the function with more than an argument, you will get an error:

-
>>> f1(0, 1)
+
>>> f1(0, 1)
 Traceback (most recent call last):
    ...
-TypeError: f1() takes exactly 1 positional argument (2 given)
+TypeError: f1() takes exactly 1 positional argument (2 given)
 
@@ -260,7 +260,7 @@ i.e. memoization in this case:

def _memoize(func, *args, **kw):
     if kw: # frozenset is used to ensure hashability
-        key = args, frozenset(kw.iteritems())
+        key = args, frozenset(kw.iteritems())
     else:
         key = args
     cache = func.cache # attributed added by memoize
@@ -289,7 +289,7 @@ decorate to the caller function.

>>> @memoize
 ... def heavy_computation():
-...     time.sleep(2)
+...     time.sleep(2)
 ...     return "done"
 
 >>> print(heavy_computation()) # the first time it will take 2 seconds
@@ -336,8 +336,8 @@ function is called:

It is immediate to verify that f1 works

-
>>> f1(0)
-calling f1 with args (0,), {}
+
>>> f1(0)
+calling f1 with args (0,), {}
 
@@ -351,14 +351,14 @@ function is called:

The same decorator works with functions of any signature:

>>> @trace
-... def f(x, y=1, z=2, *args, **kw):
+... def f(x, y=1, z=2, *args, **kw):
 ...     pass
 
->>> f(0, 3)
-calling f with args (0, 3, 2), {}
+>>> f(0, 3)
+calling f with args (0, 3, 2), {}
 
 >>> print(getargspec(f))
-ArgSpec(args=['x', 'y', 'z'], varargs='args', keywords='kw', defaults=(1, 2))
+ArgSpec(args=['x', 'y', 'z'], varargs='args', keywords='kw', defaults=(1, 2))
 
@@ -390,7 +390,7 @@ For instance, you can write directly

object which can be used as a decorator:

>>> trace
-<function trace at 0x...>
+<function trace at 0x...>
 
@@ -437,17 +437,17 @@ available. For instance:

>>> @blocking("Please wait ...")
 ... def read_data():
-...     time.sleep(3) # simulate a blocking resource
+...     time.sleep(3) # simulate a blocking resource
 ...     return "some data"
 
 >>> print(read_data()) # data is not available yet
 Please wait ...
 
->>> time.sleep(1)
+>>> time.sleep(1)
 >>> print(read_data()) # data is not available yet
 Please wait ...
 
->>> time.sleep(1)
+>>> time.sleep(1)
 >>> print(read_data()) # data is not available yet
 Please wait ...
 
@@ -512,7 +512,7 @@ The implementation is the following:

def __call__(self, func, on_success=on_success, on_failure=on_failure, on_closing=on_closing): # every decorated function has its own independent thread counter - func.counter = itertools.count(1) + func.counter = itertools.count(1) func.on_success = on_success func.on_failure = on_failure func.on_closing = on_closing @@ -528,7 +528,7 @@ The implementation is the following:

return func.on_success(result) finally: func.on_closing() - name = '%s-%s' % (func.__name__, next(func.counter)) + name = '%s-%s' % (func.__name__, next(func.counter)) thread = self.threadfactory(None, func_wrapper, name) thread.start() return thread @@ -551,7 +551,7 @@ be locked. Here is a minimalistic example:

... def write(data): ... # append data to the datalist by locking ... with threading.Lock(): -... time.sleep(1) # emulate some long running operation +... time.sleep(1) # emulate some long running operation ... datalist.append(data) ... # other operations not requiring a lock here
@@ -561,14 +561,14 @@ be locked. Here is a minimalistic example:

be no synchronization problems since write is locked.

>>> write("data1")
-<Thread(write-1, started...)>
+<Thread(write-1, started...)>
 
->>> time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
+>>> time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
 
 >>> write("data2")
-<Thread(write-2, started...)>
+<Thread(write-2, started...)>
 
->>> time.sleep(2) # wait for the writers to complete
+>>> time.sleep(2) # wait for the writers to complete
 
 >>> print(datalist)
 ['data1', 'data2']
@@ -595,8 +595,8 @@ were the function is generated by exec. Here i
 ...     print(args, kw)
 
 >>> f1 = FunctionMaker.create('f1(a, b)', 'f(a, b)', dict(f=f))
->>> f1(1,2)
-(1, 2) {}
+>>> f1(1,2)
+(1, 2) {}
 
@@ -679,7 +679,7 @@ source code which is probably not what you want:

(see bug report 1764286 for an explanation of what is happening). -Unfortunately the bug is still there, even in Python 2.6 and 3.0. +Unfortunately the bug is still there, even in Python 2.7 and 3.1. There is however a workaround. The decorator module adds an attribute .undecorated to the decorated function, containing a reference to the original function. The easy way to get @@ -688,10 +688,10 @@ undecorated function:

>>> print(inspect.getsource(factorial.undecorated))
 @tail_recursive
-def factorial(n, acc=1):
+def factorial(n, acc=1):
     "The good old factorial"
-    if n == 0: return acc
-    return factorial(n-1, n*acc)
+    if n == 0: return acc
+    return factorial(n-1, n*acc)
 <BLANKLINE>
 
@@ -774,16 +774,16 @@ objects.

Here is how you apply the upgraded decorator to the good old factorial:

@tail_recursive
-def factorial(n, acc=1):
+def factorial(n, acc=1):
     "The good old factorial"
-    if n == 0: return acc
-    return factorial(n-1, n*acc)
+    if n == 0: return acc
+    return factorial(n-1, n*acc)
 
-
>>> print(factorial(4))
-24
+
>>> print(factorial(4))
+24
 
@@ -794,8 +794,8 @@ frame. Notice also that the decorator will not work on functions which are not tail recursive, such as the following

def fact(n): # this is not tail-recursive
-    if n == 0: return 1
-    return n * fact(n-1)
+    if n == 0: return 1
+    return n * fact(n-1)
 
@@ -844,7 +844,7 @@ longer and more difficult to understand. Consider this example:

>>> @trace
 ... def f():
-...     1/0
+...     1/0
 
@@ -854,11 +854,11 @@ function is decorated the traceback will be longer:

>>> f()
 Traceback (most recent call last):
   ...
-     File "<string>", line 2, in f
-     File "<doctest __main__[22]>", line 4, in trace
+     File "<string>", line 2, in f
+     File "<doctest __main__[22]>", line 4, in trace
        return f(*args, **kw)
-     File "<doctest __main__[51]>", line 3, in f
-       1/0
+     File "<doctest __main__[51]>", line 3, in f
+       1/0
 ZeroDivisionError: int division or modulo by zero
 
-- cgit v1.2.1