Gnumed.pycommon.gmBorg

Package Gnumed :: Package pycommon :: Module gmBorg |
[frames] | no frames]

# Source Code for Module Gnumed.pycommon.gmBorg

     1  #===================================================
     2  # Thanks to Python Patterns !
     3  # ---------------------------
     4  # $Id: gmBorg.py,v 1.7 2009-05-08 07:58:35 ncq Exp $
     5  __version__ = "$Revision: 1.7 $"
     6  __author__ = "Karsten.Hilbert@gmx.net"
     7  __license__ = "GPL"
     8
     9  #===================================================
    10 -class cBorg(object):
    11          """A generic Borg mixin for new-style classes.
    12
    13          - mixin this class with your class' ancestors to borg it
    14
    15          - there may be many instances of this - PER CHILD CLASS - but they all share state
    16          """
    17          _instances = {}
    18
    19 -        def __new__(cls, *args, **kargs):
    20                  # look up subclass instance cache
    21                  if cBorg._instances.get(cls) is None:
    22                          #cBorg._instances[cls] = object.__new__(cls, *args, **kargs)
    23                          cBorg._instances[cls] = object.__new__(cls)
    24                  return cBorg._instances[cls]
    25  #===================================================
    26  if __name__ == '__main__':
    27
    28 -        class A(cBorg):
    29                  pass
    30
    31 -        class B(cBorg):
    32                  pass
    33
    34 -        class C(cBorg):
    35 -                def __init__(self, val='default'):
    36                          self.x = val
    37
    38          print "testing new-style classes borg"
    39          a1 = A()
    40          a2 = A()
    41          a1.a = 5
    42          print a1.a, "==", a2.a
    43          a3 = A()
    44          print a1.a, "==", a2.a, "==", a3.a
    45          b1 = B()
    46          b1.a = 10
    47          print b1.a
    48          print a1.a
    49          b2 = B()
    50          print b2.a
    51
    52          c1 = C(val = 'non-default')
    53          print c1.x
    54          c2 = C(val = 'non-default 2')
    55          print c2.x
    56          c3 = C()
    57          print c3.x
    58
    59  #===================================================
    60  # $Log: gmBorg.py,v $
    61  # Revision 1.7  2009-05-08 07:58:35  ncq
    62  # - __new__ doesn't take args anymore
    63  #
    64  # Revision 1.6  2008/05/21 13:57:57  ncq
    65  # - remove old borg
    66  #
    67  # Revision 1.5  2007/10/23 21:23:30  ncq
    68  # - cleanup
    69  #
    70  # Revision 1.4  2007/09/24 22:05:23  ncq
    71  # - improved docs
    72  #
    73  # Revision 1.3  2007/05/11 14:14:59  ncq
    74  # - make borg per-sublcass
    75  #
    76  # Revision 1.2  2007/05/07 12:30:05  ncq
    77  # - make cBorg an object child so properties work on it
    78  #
    79  # Revision 1.1  2004/02/25 09:30:13  ncq
    80  # - moved here from python-common
    81  #
    82  # Revision 1.3  2003/12/29 16:21:51  uid66147
    83  # - spelling fix
    84  #
    85  # Revision 1.2  2003/11/17 10:56:35  sjtan
    86  #
    87  # synced and commiting.
    88  #
    89  # Revision 1.1  2003/10/23 06:02:38  sjtan
    90  #
    91  # manual edit areas modelled after r.terry's specs.
    92  #
    93  # Revision 1.1  2003/04/02 16:07:55  ncq
    94  # - first version
    95  #
    96