ó
ç™xTc           @   sÛ   d  Z  d d l m Z d d l Z d d l Z d d d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d „  Z d „  Z d e f d „  ƒ  YZ d S(   s'    A library of object-oriented patterns iÿÿÿÿ(   t   divisionNt   injectorc           B   s$   e  Z d  Z d e f d „  ƒ  YZ RS(   sL   Injection of new methods into an existing class

  * synopsis *

  class some_descriptive_text(injector, some_existing_class,
                                        another_existing_class, ...):

    def new_method(self, ...): ...

    def another_new_method(self, ...): ...

  obj = some_existing_class(...)
  obj.new_method(...)
  obj = another_existing_class(...)
  obj.new_method(...)
  # class 'some_descriptive_text' is an empty shell with no use by itself.

  * motivation *

  The traditional way to add methods to an existing class involves typing
  three times the same information:

  def new_method(self, ...): ...
  existing_class.new_method = new_method

  or to defer the naming to after the definition:

  def foo(self, ...): ...
  existing_class.new_method = foo

  A bit of metaclass trickery results in a cleaner syntax.
  t   __metaclass__c           B   s   e  Z d  „  Z RS(   c         C   s�   xz | d D]n } xe | j  ƒ  D]W \ } } | d k r< q n  t | | ƒ se t d | j | f ƒ ‚ t | | | ƒ q Wq Wd  S(   Ni   t
   __module__t   __doc__s#   class %s already has attribute '%s'(   s
   __module__s   __doc__(   t   itemst   hasattrt   AssertionErrort   __name__t   setattr(   t   clst	   classnamet   basest	   classdictt   target_classt   namet	   attribute(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __init__(   s     (   R   R   R   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   '   s   (   R   R   R   t   typeR   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR      s    t   memoizec           B   s    e  Z d  Z d „  Z d „  Z RS(   s+    Memoize the result returned by a function c         C   s%   i  |  _  | |  _ |  j j |  _ d  S(   N(   t   cachedt   funcR   (   t   selfR   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   5   s    		c         G   s\   y |  j  | SWnF t k
 r= |  j | Œ  |  j  | <} | St k
 rW |  j | Œ  SXd  S(   N(   R   t   KeyErrorR   t	   TypeError(   R   t   argst   result(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __call__:   s    (   R   R   R   R   R   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   2   s   	t   memoize_methodc           B   s#   e  Z d  Z d „  Z d d „ Z RS(   sh    Memoize the result returned by a bound method.
      This is to be used with immutable objects only.
  c         C   s   d | j  |  _ | |  _ d  S(   Ns   _memoized_%s(   R   t   cachet   meth(   R   R   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   I   s    c         C   s}   | d  k r |  Sy t | |  j ƒ SWnR t k
 rx t j |  j t j | ƒ | ƒ } t	 | ƒ } t
 | |  j | ƒ | SXd  S(   N(   t   Nonet   getattrR   t   AttributeErrort   newt   instancemethodR   t   weakreft   proxyR   R	   (   R   t   objR   R   t   memoized(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __get__M   s    !N(   R   R   R   R   R   R(   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   D   s   	t   nullc           B   sb   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   c         O   s   d  S(   N(    (   R   R   t   kwds(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   `   s    c         C   s   |  S(   N(    (   R   t   a(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __getattr__b   s    c         C   s   |  S(   N(    (   R   R+   t   v(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __setattr__c   s    c         C   s   |  S(   N(    (   R   R+   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __delattr__d   s    c         O   s   |  S(   N(    (   R   R   R*   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   f   s    c         C   s   |  S(   N(    (   R   t   i(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __getitem__h   s    c         C   s   |  S(   N(    (   R   R0   R-   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __setitem__i   s    c         C   s   |  S(   N(    (   R   R0   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __delitem__j   s    c         C   s   d S(   Ns   null()(    (   R   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __repr__l   s    c         C   s   t  S(   N(   t   False(   R   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   __nonzero__n   s    (   R   R   R   R,   R.   R/   R   R1   R2   R3   R4   R6   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR)   ^   s   									R%   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   subject(   R   R7   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   s   s    c         C   s   t  |  j | ƒ S(   N(   R    R7   (   R   t   attr(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR,   v   s    (   R   R   R   R,   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR%   q   s   	t   journal_mixinc           B   s5   e  Z d  Z g  Z d Z d „  Z d „  Z d „  Z RS(   si    An easy way to store the history of an attribute as it changes
      through the course of a routine.
  t   _historyc         C   sS   | |  j  k r | |  j } n | } | |  j k rC |  j | d St | ƒ ‚ d  S(   Niÿÿÿÿ(   t   __journal__t   __journal_suffix__t   __dict__R!   (   R   R   t   key(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR,   ‚   s    c         C   sf   | |  j  k rU | |  j } | |  j k r> | g |  j | <qb |  j | j | ƒ n | |  j | <d  S(   N(   R;   R<   R=   t   append(   R   R   t   valueR>   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR.   ‹   s    c         C   s3   | |  j  k r | |  j } n | } |  j | =d  S(   N(   R;   R<   R=   (   R   R   R>   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR/   •   s    (   R   R   R   R;   R<   R,   R.   R/   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR9   z   s   			
c         C   s   |  j  ƒ  |  _  t |  _ |  j  S(   N(   t   datat   retrieve_statet   state(   t   holder(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   calculate_state�   s    	c         C   s   |  j  S(   N(   RA   (   RD   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyRB   ¤   s    t   lazy_initializationc           B   s    e  Z d  Z d „  Z d „  Z RS(   sG   
  Defers initialization until the value is accessed (state pattern)
  c         C   s   | |  _  t |  _ d  S(   N(   RA   RE   RC   (   R   t   calculation(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   ®   s    	c         C   s   |  j  d |  ƒ S(   NRD   (   RC   (   R   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyR   ´   s    (   R   R   R   R   R   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyRF   ©   s   	(    (   R   t
   __future__R    R"   R$   R   t   objectR   R   R)   R%   R9   RE   RB   RF   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/object_oriented_patterns.pyt   <module>   s   ,	#		