Ejemplo n.º 1
0
def test_on_locally_defined_class():
    
    ###########################################################################
    # Testing for locally defined class:
    
    
    if python_toolbox.__version_info__ <= (0, 6, 11, 'release'):
        raise nose.SkipTest("This test doesn't pass in `python_toolbox` "
                            "version 0.6.11 and below, because `describe` :"
                            "doesn't support nested classes yet.")
    
    result = describe(A.B)
    assert result == prefix + 'A.B'
    assert resolve(result) is A.B
    
    result = describe(A.C.D.deeper_method)
    assert result == prefix + 'A.C.D.deeper_method'
    assert resolve(result) == A.C.D.deeper_method
    
    result = describe(A.C.D.deeper_method, root=A.C)
    assert result == 'C.D.deeper_method'
    assert resolve(result, root=A.C) == A.C.D.deeper_method
    
    result = describe(A.C.D.deeper_method, root='A.C.D')
    assert result == 'D.deeper_method'
    assert resolve(result, root='A.C.D') == A.C.D.deeper_method
Ejemplo n.º 2
0
def test_python_toolbox():
    '''Test `resolve` on `python_toolbox` modules.'''
    
    result = resolve('python_toolbox.caching')
    import python_toolbox
    assert python_toolbox.caching is result
    
    ###########################################################################
    #                                                                         #
    result_0 = resolve('python_toolbox.persistent.cross_process_persistent.'
                     'CrossProcessPersistent.personality')
    result_1 = resolve('persistent.CrossProcessPersistent.personality', 
                       namespace=python_toolbox)
    result_2 = resolve('persistent.CrossProcessPersistent.personality',
                       root=python_toolbox.persistent,
                       namespace='email') # Namespace is red herring.
    assert result_0 is result_1 is result_2 is python_toolbox.persistent. \
                    cross_process_persistent.CrossProcessPersistent.personality
    #                                                                         #
    ###########################################################################
    
    ###########################################################################
    #                                                                         #
    result_0 = resolve('caching.cached_property.CachedProperty',
                       root=python_toolbox.caching)
    result_1 = resolve('caching.CachedProperty',
                       root=python_toolbox.caching)
    result_2 = resolve('caching.CachedProperty', namespace='python_toolbox')
    assert result_0 is result_1 is result_2 is \
                          python_toolbox.caching.cached_property.CachedProperty
    #                                                                         #
    ###########################################################################
    
    import email
    assert resolve('python_toolbox', namespace={'e': email}) == python_toolbox
Ejemplo n.º 3
0
def get_event_code_from_name(name, evt_handler_type):
    '''
    Get an event code given a `name` and an `evt_handler_type`.
    
    For example, given a `name` of `left_down` this function will return the
    event code `wx.EVT_LEFT_DOWN`.
    
    If `evt_handler_type` has an `.event_modules` attribute, these modules will
    be searched for event codes in precedence to `wx` and the event handler
    type's own module.
    '''
    processed_name = 'EVT_%s' % string_tools.case_conversions. \
                                camel_case_to_lower_case(name).upper()
    raw_event_modules = \
        (evt_handler_type.event_modules if
         sequence_tools.is_sequence(evt_handler_type.event_modules) else 
         [evt_handler_type.event_modules])
    event_modules = raw_event_modules + [
        address_tools.resolve(evt_handler_type.__module__),
        wx
    ]
    for event_module in event_modules:
        try:
            return getattr(event_module, processed_name)
        except AttributeError:
            pass
    else:
        raise LookupError("Couldn't find event by the name of '%s'." %
                          processed_name)
 def bind(self, evt_handler):
     assert isinstance(evt_handler, wx.EvtHandler)
     event_handler_bound_method = types.MethodType(    
         self.event_handler_self_taking_function,
         evt_handler,
         self.evt_handler_type
     )
     if len(self.parsed_words) >= 2:
         closer_evt_handler = address_tools.resolve(
             '.'.join(('window',) + self.parsed_words[:-1]),
             namespace={'window': evt_handler}
         )
     else:
         closer_evt_handler = None
     last_word = self.parsed_words[-1]
     component_candidate = getattr(closer_evt_handler or evt_handler,
                                   last_word, None)
     if component_candidate is not None and \
        hasattr(component_candidate, 'GetId'):
         component = component_candidate
         event_codes = get_event_codes_of_component(component)
         for event_code in event_codes:
             evt_handler.Bind(
                 event_code,
                 event_handler_bound_method,
                 source=component
             )
             
     else:
         evt_handler.Bind(
             get_event_code_from_name(last_word,
                                      self.evt_handler_type),
             event_handler_bound_method,
         )
Ejemplo n.º 5
0
def test_on_local_modules():
    '''Test `describe` on local, relatively-imported modules.'''
    import python_toolbox
    
    from .sample_module_tree import w
    
    z = resolve('w.x.y.z', root=w)

    result = describe(z, root=w)
    assert result == 'w.x.y.z'
    
    result = describe(z, shorten=True, root=w)
    assert result == 'w.y.z'
    
    result = describe(z, shorten=True, root=w)
    assert result == 'w.y.z'
    
    result = describe(z, shorten=True, root=w, namespace='email')
    assert result == 'w.y.z'
    
    result = describe(z, shorten=True, root=python_toolbox, namespace=w)
    assert result == 'y.z'
    
    result = describe(z, shorten=True, root=w.x)
    assert result == 'x.y.z'
    def __init__(self, function):
        """
        Construct the `TempFunctionCallCounter`.
        
        For `function`, you may pass in either a function object, or a
        `(parent_object, function_name)` pair, or a `(getter, setter)` pair.
        """

        if cute_iter_tools.is_iterable(function):
            first, second = function
            if isinstance(second, basestring):
                actual_function = getattr(first, second)
            else:
                assert callable(first) and callable(second)
                actual_function = first()  # `first` is the getter in this case.

        else:  # not cute_iter_tools.is_iterable(function)
            assert callable(function)
            actual_function = function
            try:
                address = address_tools.object_to_string.get_address(function)
                parent_object_address, function_name = address.rsplit(".", 1)
                parent_object = address_tools.resolve(parent_object_address)
            except Exception:
                raise Exception(
                    "Couldn't obtain parent/name pair from "
                    "function; supply one manually or "
                    "alternatively supply a getter/setter pair."
                )
            first, second = parent_object, function_name

        self.call_counting_function = count_calls(actual_function)

        TempValueSetter.__init__(self, (first, second), value=self.call_counting_function)
Ejemplo n.º 7
0
def test_on_python_toolbox():
    '''Test `describe` for various `python_toolbox` modules.'''
    
    import python_toolbox.caching
    result = describe(python_toolbox.caching.cached_property.CachedProperty)
    assert result == 'python_toolbox.caching.cached_property.CachedProperty'
    assert resolve(result) is \
                          python_toolbox.caching.cached_property.CachedProperty
    
    result = describe(python_toolbox.caching.cached_property.CachedProperty,
                      shorten=True)
    assert result == 'python_toolbox.caching.CachedProperty'
    assert resolve(result) is \
                          python_toolbox.caching.cached_property.CachedProperty
    
    import python_toolbox.nifty_collections
    result = describe(python_toolbox.nifty_collections.weak_key_default_dict.
                                                            WeakKeyDefaultDict,
                      shorten=True,
                      root=python_toolbox.nifty_collections.
                                                         weak_key_default_dict)
    assert result == 'weak_key_default_dict.WeakKeyDefaultDict'
    assert resolve(
        result,
        root=python_toolbox.nifty_collections.weak_key_default_dict
        ) is python_toolbox.nifty_collections.WeakKeyDefaultDict
    
    result = describe(python_toolbox.caching.cached_property.CachedProperty,
                      shorten=True,
                      namespace=python_toolbox)
    assert result == 'caching.CachedProperty'
    assert resolve(result, namespace=python_toolbox) is \
                                          python_toolbox.caching.CachedProperty
    
    result = describe(python_toolbox.caching.CachedProperty, shorten=True,
                      namespace=python_toolbox.__dict__)
    assert result == 'caching.CachedProperty'
    assert resolve(result, namespace=python_toolbox.__dict__) is \
           python_toolbox.caching.CachedProperty
    
    result = describe(python_toolbox.caching.CachedProperty, shorten=True,
                      namespace='python_toolbox')
    assert result == 'caching.CachedProperty'
    assert resolve(result, namespace='python_toolbox') is \
                                          python_toolbox.caching.CachedProperty
    
    result = describe(python_toolbox.caching.CachedProperty, shorten=True,
                      namespace='python_toolbox.__dict__')
    assert result == 'caching.CachedProperty'
    assert resolve(result, namespace='python_toolbox.__dict__') is \
           python_toolbox.caching.CachedProperty
    
    result = describe(python_toolbox.caching.cached_property.CachedProperty,
                      root=python_toolbox)
    assert result == 'python_toolbox.caching.cached_property.CachedProperty'
    assert resolve(result, root=python_toolbox) is \
                          python_toolbox.caching.cached_property.CachedProperty
Ejemplo n.º 8
0
def test_address_in_expression():
        
    result = resolve('[object, email.encoders, marshal]')
    import email, marshal, python_toolbox
    assert result == [object, email.encoders, marshal]
    
    assert resolve('[email.encoders, 7, (1, 3), marshal]') == \
           [email.encoders, 7, (1, 3), marshal]
    
    result = \
         resolve('{email: marshal, object: 7, python_toolbox: python_toolbox}')
    import python_toolbox
    assert result == {email: marshal, object: 7,
                      python_toolbox: python_toolbox}
    
    assert resolve('{email: marshal, '
                   'object: 7, '
                   'python_toolbox: python_toolbox}') == \
                    {email: marshal, object: 7, python_toolbox: python_toolbox}
    
    assert resolve('{CachedProperty: cache}',
                   namespace=python_toolbox.caching) == {
        python_toolbox.caching.CachedProperty: python_toolbox.caching.cache
    }
    
    assert resolve('{caching.CachedProperty: cute_testing}',
                   root=python_toolbox.caching,
                   namespace=python_toolbox) == \
          {python_toolbox.caching.CachedProperty: python_toolbox.cute_testing}

    assert resolve('python_toolbox if 4 else e', namespace={'e': email}) is \
           python_toolbox
Ejemplo n.º 9
0
def test_on_stdlib():    
    '''Test `resolve` on stdlib modules.'''
    
    result = resolve('email')
    import email
    import marshal
    assert result is email
    
    assert resolve('email') is \
           resolve('email.email') is \
           resolve('email.email.email') is \
           resolve('email.email.email.email') is email
    
    result = resolve('email.base64mime.a2b_base64')
    assert result is email.base64mime.a2b_base64
    
    #result = resolve('email.email.encoders.base64.b32decode')
    #assert result is email.encoders.base64.b32decode
    
    #result = resolve('base64.b32decode',
                        #root='email.email.encoders.base64')
    #assert result is email.encoders.base64.b32decode
    
    #result = resolve('base64.b32decode',
                        #namespace='email.email.encoders')
    #assert result is email.encoders.base64.b32decode
    
    #result = resolve('base64.b32decode', root=marshal,
                        #namespace='email.email.encoders')
    #assert result is email.encoders.base64.b32decode
    
    assert resolve('object') is object
Ejemplo n.º 10
0
def test_on_stdlib():
    '''Test `describe` for various stdlib modules.'''
    
    import email.encoders
    result = describe(email.encoders)
    assert result == 'email.encoders'
    assert resolve(result) is email.encoders
    
    result = describe(email.encoders, root=email.encoders)
    assert result == 'encoders'
    assert resolve(result, root=email.encoders) is email.encoders
    
    result = describe(email.encoders, namespace=email)
    assert result == 'encoders'
    assert resolve(result, namespace=email) is email.encoders
    
    result = describe(email.encoders, root=email.encoders, namespace=email)
    assert result == 'encoders'
    assert resolve(result, root=email.encoders, namespace=email) is \
           email.encoders
Ejemplo n.º 11
0
 def __get__(self, obj, our_type=None):
     if obj is None:
         # We're being accessed from the class itself, not from an object
         return self
     else:
         if '.' in self.attribute_name:
             from python_toolbox import address_tools
             return address_tools.resolve('obj.%s' % self.attribute_name,
                                          namespace={'obj': obj})
         else:
             return getattr(obj, self.attribute_name)
Ejemplo n.º 12
0
def test_python_toolbox():
    '''Test `resolve` on `python_toolbox` modules.'''
    
    result = resolve('python_toolbox.caching')
    import python_toolbox
    assert python_toolbox.caching is result
    
    ###########################################################################
    #                                                                         #
    result_0 = resolve('caching.cached_property.CachedProperty',
                       root=python_toolbox.caching)
    result_1 = resolve('caching.CachedProperty',
                       root=python_toolbox.caching)
    result_2 = resolve('caching.CachedProperty', namespace='python_toolbox')
    assert result_0 is result_1 is result_2 is \
                          python_toolbox.caching.cached_property.CachedProperty
    #                                                                         #
    ###########################################################################
    
    import email
    assert resolve('python_toolbox', namespace={'e': email}) == python_toolbox
Ejemplo n.º 13
0
 def __set__(self, obj, value):
     
     # todo: should I check if `obj` is `None` and set on class? Same for
     # `__delete__`?
     
     if '.' in self.attribute_name:
         from python_toolbox import address_tools
         left_segment, right_segment = self.attribute_name.rsplit('.', 1)
         deepest_object = address_tools.resolve('obj.%s' % left_segment,
                                                namespace={'obj': obj})
         setattr(deepest_object, right_segment, value)
     else:
         setattr(obj, self.attribute_name, value)
Ejemplo n.º 14
0
def test_on_locally_defined_class():

    ###########################################################################
    # Testing for locally defined class:

    raise nose.SkipTest("This test doesn't currently pass because `describe` "
                        "doesn't support nested classes yet.")

    result = describe(A.B)
    assert result == prefix + 'A.B'
    assert resolve(result) is A.B

    result = describe(A.C.D.deeper_method)
    assert result == prefix + 'A.C.D.deeper_method'
    assert resolve(result) == A.C.D.deeper_method

    result = describe(A.C.D.deeper_method, root=A.C)
    assert result == 'C.D.deeper_method'
    assert resolve(result, root=A.C) == A.C.D.deeper_method

    result = describe(A.C.D.deeper_method, root='A.C.D')
    assert result == 'D.deeper_method'
    assert resolve(result, root='A.C.D') == A.C.D.deeper_method
Ejemplo n.º 15
0
def test_on_locally_defined_class():
    
    ###########################################################################
    # Testing for locally defined class:
    
    
    raise nose.SkipTest("This test doesn't currently pass because `describe` "
                        "doesn't support nested classes yet.")
    
    result = describe(A.B)
    assert result == prefix + 'A.B'
    assert resolve(result) is A.B
    
    result = describe(A.C.D.deeper_method)
    assert result == prefix + 'A.C.D.deeper_method'
    assert resolve(result) == A.C.D.deeper_method
    
    result = describe(A.C.D.deeper_method, root=A.C)
    assert result == 'C.D.deeper_method'
    assert resolve(result, root=A.C) == A.C.D.deeper_method
    
    result = describe(A.C.D.deeper_method, root='A.C.D')
    assert result == 'D.deeper_method'
    assert resolve(result, root='A.C.D') == A.C.D.deeper_method
Ejemplo n.º 16
0
def test_function_in_main():
    '''Test that a function defined in `__main__` is well-described.'''

    ###########################################################################
    # We can't really define a function in `__main__` in this test, so we
    # emulate it:
    with TempValueSetter((globals(), '__name__'), '__main__'):
        def f(x):
            pass
        
        # Accessing `f.__module__` here so PyPy will calculate it:
        assert f.__module__ == '__main__'
        
    assert f.__module__ == '__main__'
    import __main__
    __main__.f = f
    del __main__
    #
    ###########################################################################
    
    assert describe(f) == '__main__.f'
    assert resolve(describe(f)) is f
Ejemplo n.º 17
0
def test_function_in_main():
    '''Test that a function defined in `__main__` is well-described.'''

    ###########################################################################
    # We can't really define a function in `__main__` in this test, so we
    # emulate it:
    with TempValueSetter((globals(), '__name__'), '__main__'):
        def f(x):
            pass
        
        # Accessing `f.__module__` here so PyPy will calculate it:
        assert f.__module__ == '__main__'
        
    assert f.__module__ == '__main__'
    import __main__
    __main__.f = f
    del __main__
    #
    ###########################################################################
    
    assert describe(f) == '__main__.f'
    assert resolve(describe(f)) is f
Ejemplo n.º 18
0
    def __init__(self, function):
        '''
        Construct the `TempFunctionCallCounter`.

        For `function`, you may pass in either a function object, or a
        `(parent_object, function_name)` pair, or a `(getter, setter)` pair.
        '''

        if cute_iter_tools.is_iterable(function):
            first, second = function
            if isinstance(second, str):
                actual_function = getattr(first, second)
            else:
                assert callable(first) and callable(second)
                actual_function = first() # `first` is the getter in this case.

        else: # not cute_iter_tools.is_iterable(function)
            assert callable(function)
            actual_function = function
            try:
                address = address_tools.object_to_string.get_address(function)
                parent_object_address, function_name = address.rsplit('.', 1)
                parent_object = address_tools.resolve(parent_object_address)
            except Exception as exception:
                raise Exception(
                    "Couldn't obtain parent/name pair from function; supply "
                    "one manually or alternatively supply a getter/setter "
                    "pair."
                ) from exception
            first, second = parent_object, function_name

        self.call_counting_function = count_calls(actual_function)

        TempValueSetter.__init__(
            self,
            (first, second),
            value=self.call_counting_function
        )
Ejemplo n.º 19
0
def test_address_in_expression():

    result = resolve('[object, email.encoders, marshal]')
    import email, marshal, python_toolbox
    assert result == [object, email.encoders, marshal]

    assert resolve('[email.encoders, 7, (1, 3), marshal]') == \
           [email.encoders, 7, (1, 3), marshal]

    result = \
         resolve('{email: marshal, object: 7, python_toolbox: python_toolbox}')
    import python_toolbox
    assert result == {
        email: marshal,
        object: 7,
        python_toolbox: python_toolbox
    }

    assert resolve('{email: marshal, '
                   'object: 7, '
                   'python_toolbox: python_toolbox}') == \
                    {email: marshal, object: 7, python_toolbox: python_toolbox}

    assert resolve('{CachedProperty: cache}',
                   namespace=python_toolbox.caching) == {
                       python_toolbox.caching.CachedProperty:
                       python_toolbox.caching.cache
                   }

    assert resolve('{caching.CachedProperty: cute_testing}',
                   root=python_toolbox.caching,
                   namespace=python_toolbox) == \
          {python_toolbox.caching.CachedProperty: python_toolbox.cute_testing}

    assert resolve('python_toolbox if 4 else e', namespace={'e': email}) is \
           python_toolbox
Ejemplo n.º 20
0
def test_on_stdlib():    
    '''Test `resolve` on stdlib modules.'''
    
    result = resolve('email')
    import email
    import marshal
    assert result is email
    
    assert resolve('email') is \
           resolve('email.email') is \
           resolve('email.email.email') is \
           resolve('email.email.email.email') is email
    
    result = resolve('email.base64mime.a2b_base64')
    assert result is email.base64mime.a2b_base64
    
    result = resolve('email.email.encoders.base64.b32decode')
    assert result is email.encoders.base64.b32decode
    
    result = resolve('base64.b32decode',
                        root='email.email.encoders.base64')
    assert result is email.encoders.base64.b32decode
    
    result = resolve('base64.b32decode',
                        namespace='email.email.encoders')
    assert result is email.encoders.base64.b32decode
    
    result = resolve('base64.b32decode', root=marshal,
                        namespace='email.email.encoders')
    assert result is email.encoders.base64.b32decode
    
    assert resolve('object') is object
Ejemplo n.º 21
0
def test_on_locally_defined_class():
    '''Test `resolve` on a locally defined class tree.'''

    assert resolve(prefix + 'A') is A
    assert resolve(prefix + 'A.B') is A.B
    assert resolve(prefix + 'A.method') == A.method
    assert resolve('method', namespace=A) == A.method
    assert resolve(prefix + 'A.B.deep_method') == A.B.deep_method
    assert resolve('B.deep_method', namespace=A) == A.B.deep_method
    assert resolve(prefix + 'A.C.D') is A.C.D
    assert resolve(prefix + 'A.C.D.deeper_method') == \
           A.C.D.deeper_method

    assert resolve('D.deeper_method', root=(prefix + 'A.C.D')) == \
           A.C.D.deeper_method
    assert resolve('D.deeper_method', root=A.C.D, namespace='email') == \
           A.C.D.deeper_method
    assert resolve('A', root=A) == A
Ejemplo n.º 22
0
def test_on_locally_defined_class():
    '''Test `resolve` on a locally defined class tree.'''
    
    assert resolve(prefix + 'A') is A
    assert resolve(prefix + 'A.B') is A.B
    assert resolve(prefix + 'A.method') == A.method
    assert resolve('method', namespace=A) == A.method
    assert resolve(prefix + 'A.B.deep_method') == A.B.deep_method
    assert resolve('B.deep_method', namespace=A) == A.B.deep_method
    assert resolve(prefix + 'A.C.D') is A.C.D
    assert resolve(prefix + 'A.C.D.deeper_method') == \
           A.C.D.deeper_method
    
    assert resolve('D.deeper_method', root=(prefix + 'A.C.D')) == \
           A.C.D.deeper_method
    assert resolve('D.deeper_method', root=A.C.D, namespace='email') == \
           A.C.D.deeper_method
    assert resolve('A', root=A) == A