Exemplo n.º 1
0
class HasIdentity(object):
    '''
    An object that has a persistent identity.
    
    When you make deepcopies of this object using `DontCopyPersistent()`, the
    new copies will have the same "identity" as the original object:
    
        >>> class A(HasIdentity):
        ...     pass
        >>> a0 = A()
        >>> a1 = copy.deepcopy(a0, DontCopyPersistent)
        >>> a0.has_same_identity_as(a1)
        True
        >>> a0 & a1 # Shortcut for `has_same_identity_as`
        True
        
    (`DontCopyPersistent` is available as
    `python_toolbox.persistent.DontCopyPersistent`)
    '''
    def __init__(self):
        self.__identity = CrossProcessPersistent()
        '''The object's persistent identity.'''

    def has_same_identity_as(self, other):
        '''Does `other` have the same identity as us?'''
        if not isinstance(other, HasIdentity):
            return NotImplemented
        return self.__identity.has_same_uuid_as(other.__identity)

    __and__ = has_same_identity_as

    personality = misc_tools.ProxyProperty(
        '._HasIdentity__identity.personality',
        doc='''Personality containing a human name and two colors.''')
Exemplo n.º 2
0
def test_personality():
    '''Test that a `CrossProcessPersistent` has a `.personality`.'''
    a = A()
    cross_process_persistent = CrossProcessPersistent()
    assert isinstance(a.personality, persistent.Personality)
    assert isinstance(cross_process_persistent.personality,
                      persistent.Personality)
Exemplo n.º 3
0
class HasIdentity:
    """
    An object that has a persistent identity.
    
    When you make deepcopies of this object using `DontCopyPersistent()`, the
    new copies will have the same "identity" as the original object:
    
        >>> class A(HasIdentity):
        ...     pass
        >>> a0 = A()
        >>> a1 = copy.deepcopy(a0, DontCopyPersistent)
        >>> a0.has_same_identity_as(a1)
        True
        >>> a0 & a1 # Shortcut for `has_same_identity_as`
        True
        
    (`DontCopyPersistent` is available as
    `python_toolbox.persistent.DontCopyPersistent`)
    """

    def __init__(self):
        self.__identity = CrossProcessPersistent()
        """The object's persistent identity."""

    def has_same_identity_as(self, other):
        """Does `other` have the same identity as us?"""
        if not isinstance(other, HasIdentity):
            return NotImplemented
        return self.__identity.has_same_uuid_as(other.__identity)

    __and__ = has_same_identity_as

    personality = misc_tools.ProxyProperty(
        "._HasIdentity__identity.personality", doc="""Personality containing a human name and two colors."""
    )
def test():
    '''Test the basic workings of `Personality`.'''
    cpp_1 = CrossProcessPersistent()
    cpp_2 = CrossProcessPersistent()
    cpp_3 = CrossProcessPersistent()
    
    personality_1 = cpp_1.personality
    personality_2 = cpp_2.personality
    personality_3 = cpp_3.personality
    
    human_name_1 = personality_1.human_name
    human_name_2 = personality_2.human_name
    human_name_3 = personality_3.human_name
    
    assert isinstance(human_name_1, basestring)
    assert isinstance(human_name_2, basestring)
    assert isinstance(human_name_3, basestring)
    
    light_color_1 = personality_1.light_color
    light_color_2 = personality_2.light_color
    light_color_3 = personality_3.light_color
    dark_color_1 = personality_1.dark_color
    dark_color_2 = personality_2.dark_color
    dark_color_3 = personality_3.dark_color
    
    for light_color in (light_color_1, light_color_2, light_color_3):
        hls_value = colorsys.rgb_to_hls(*light_color)
        lightness = hls_value[1]
        assert 0.85 < lightness < 0.95
        
    for dark_color in (dark_color_1, dark_color_2, dark_color_3):
        hls_value = colorsys.rgb_to_hls(*dark_color)
        lightness = hls_value[1]
        assert 0.25 < lightness < 0.35
    
    assert (human_name_1, light_color_1, dark_color_1) != \
           (human_name_2, light_color_2, dark_color_2) != \
           (human_name_3, light_color_3, dark_color_3) != \
           (human_name_1, light_color_1, dark_color_1)
    
    
Exemplo n.º 5
0
def test_helpful_warnings_for_old_protocols():
    '''
    Test that helpful errors are given when trying to pickle with old protocol.
    '''
    pickle_modules = [pickle, cPickle]
    cross_process_persistents = [A(), CrossProcessPersistent()]
    old_protocols = [0, 1]

    iterator = itertools.product(pickle_modules, cross_process_persistents,
                                 old_protocols)

    for pickle_module, cross_process_persistent, old_protocol in iterator:
        yield (_check_helpful_warnings_for_old_protocols, pickle_module,
               cross_process_persistent, old_protocol)
Exemplo n.º 6
0
 def __init__(self):
     self.__identity = CrossProcessPersistent()
     '''The object's persistent identity.'''
Exemplo n.º 7
0
 def __init__(self):
     self.__identity = CrossProcessPersistent()
     '''The object's persistent identity.'''