viff

changeset 622:2b9d4c9e959b

Documented ShareList with an example.
author Martin Geisler <mg@daimi.au.dk>
date Sat, 29 Mar 2008 19:40:05 +0100
parents bfa550e712c2
children 86aaaa6b9ece
files viff/runtime.py viff/test/test_runtime.py
diffstat 2 files changed, 34 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Sat Mar 29 18:10:44 2008 +0100
     1.2 +++ b/viff/runtime.py	Sat Mar 29 19:40:05 2008 +0100
     1.3 @@ -149,7 +149,33 @@
     1.4  class ShareList(Share):
     1.5      """Create a share that waits on a number of other shares.
     1.6  
     1.7 -    Roughly modelled after the Twisted C{DeferredList} class.
     1.8 +    Roughly modelled after the Twisted C{DeferredList} class. The
     1.9 +    advantage of this class is that it is a L{Share} (not just a
    1.10 +    C{Deferred}) and that it can be made to trigger when a certain
    1.11 +    threshold of the shares are ready. This example shows how the
    1.12 +    C{pprint} callback is triggered when C{a} and C{c} are ready:
    1.13 +
    1.14 +    >>> from pprint import pprint
    1.15 +    >>> from viff.field import GF256
    1.16 +    >>> a = Share(None, GF256)
    1.17 +    >>> b = Share(None, GF256)
    1.18 +    >>> c = Share(None, GF256)
    1.19 +    >>> shares = ShareList([a, b, c], threshold=2)
    1.20 +    >>> shares.addCallback(pprint)           # doctest: +ELLIPSIS
    1.21 +    <ShareList at 0x...>
    1.22 +    >>> a.callback(10)
    1.23 +    >>> c.callback(20)
    1.24 +    [(True, 10), None, (True, 20)]
    1.25 +
    1.26 +    The C{pprint} function is called with a list of pairs. The first
    1.27 +    component of each pair is a boolean indicating if the callback or
    1.28 +    errback method was called on the corresponding L{Share}, and the
    1.29 +    second component is the value given to the callback/errback.
    1.30 +
    1.31 +    If a threshold less than the full number of shares is used, some
    1.32 +    of the pairs may be missing and C{None} is used instead. In the
    1.33 +    example above the C{c} Share arrived later than C{a} and C{b}, and
    1.34 +    so the list contains a C{None} on its place.
    1.35      """
    1.36  
    1.37      def __init__(self, shares, threshold=None):
    1.38 @@ -1106,3 +1132,7 @@
    1.39                  reactor.connectTCP(player.host, player.port, factory)
    1.40  
    1.41      return result
    1.42 +
    1.43 +if __name__ == "__main__":
    1.44 +    import doctest    #pragma NO COVER
    1.45 +    doctest.testmod() #pragma NO COVER
     2.1 --- a/viff/test/test_runtime.py	Sat Mar 29 18:10:44 2008 +0100
     2.2 +++ b/viff/test/test_runtime.py	Sat Mar 29 19:40:05 2008 +0100
     2.3 @@ -37,6 +37,9 @@
     2.4  from viff.test.util import RuntimeTestCase, BinaryOperatorTestCase, protocol
     2.5  
     2.6  
     2.7 +__doctests__ = ['viff.runtime']
     2.8 +
     2.9 +
    2.10  class AddTest(BinaryOperatorTestCase, RuntimeTestCase):
    2.11      operator = operator.add
    2.12