The joys of Python

Discussion in 'Python' started by Volker Gietz, Apr 2, 2004.

  1. Volker Gietz

    Volker Gietz Guest

    Hello all,

    I have been using Python for almost a year now and by now I do 95% of my
    programming in it except some DSP stuff in C++. I learnt Python's basics
    in one week, yet the language still amazes me every day. Elegant
    solutions just keep cropping up everywhere, it must be one of these
    brain-impedance matches I suppose :)

    Yesterday I came up with this:

    class Channel:
    ## Acts as a function call wrapper, a call to one instance
    ## calls all other wrapped functions on the (broadcast) channel
    def __init__(self, func, other=None):
    self.all = (other and other.all) or []
    self.all.append(self)
    self.func = func

    def __call__(self, *args):
    for item in self.all:
    item.func(*args)

    ## join other channel ("tune in")
    def join(self, other):
    self.all.remove(self)
    self.all = other.all
    self.all.append(self)


    Now I can easily create:

    from UserList import UserList

    class SyncList(UserList):
    def __init__(self):
    UserList.__init__(self)
    self.append = Channel(self.append)
    self.extend = Channel(self.extend)

    def __repr__(self):
    return repr(self.data)

    ## synchronise to other list
    def join(self, other):
    self.append.join(other.append)
    self.extend.join(other.extend)


    And this allows for automatic peer-to-peer list synchronisation:

    >>> l1 = SyncList()
    >>> l2 = SyncList()
    >>> l2.join(l1)
    >>> l1.append(1)
    >>> l1.extend((2, 3, 4))
    >>> l1

    [1, 2, 3, 4]
    >>> l2

    [1, 2, 3, 4]
    >>> l1 is l2

    False
    qed :)

    Of course, all mutating methods need to be wrapped. The "classic"
    approach would have been to have one conrete broadcaster and do

    def append(self, items):
    for child in self.children:
    child.append(items)

    def extend(self, items):
    for child in self.children:
    child.extend(items)

    ... yawn!


    Bottom line is, I like Python a lot! It is certainly the bright side of
    coding.

    Cheers,
    Volker

    ________________________________________________________________________
    ___
    e-mail: |alphir at \/\/eb.de
    Volker Gietz, Apr 2, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Stringer

    The joys of functions and arrays

    Jeremy Stringer, Apr 7, 2005, in forum: VHDL
    Replies:
    2
    Views:
    582
    Jeremy Stringer
    Apr 8, 2005
  2. Roedy Green

    joys of a new mouse

    Roedy Green, Apr 20, 2004, in forum: Java
    Replies:
    0
    Views:
    346
    Roedy Green
    Apr 20, 2004
  3. windandwaves

    the joys of png

    windandwaves, Sep 30, 2005, in forum: HTML
    Replies:
    2
    Views:
    395
    Travis Newbury
    Sep 30, 2005
  4. Replies:
    0
    Views:
    740
  5. Paul Moore
    Replies:
    0
    Views:
    611
    Paul Moore
    Mar 1, 2008
Loading...

Share This Page