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:
                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)
    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.º 3
0
 def __init__(self, stdout=True, stderr=True):
     self.string_io = io.StringIO()
     
     if stdout:
         self._stdout_temp_setter = \
             TempValueSetter((sys, 'stdout'), self.string_io)
     else: # not stdout
         self._stdout_temp_setter = BlankContextManager()
         
     if stderr:
         self._stderr_temp_setter = \
             TempValueSetter((sys, 'stderr'), self.string_io)
     else: # not stderr
         self._stderr_temp_setter = BlankContextManager()
Ejemplo n.º 4
0
    def __init__(self, window, cursor):
        '''
        Construct the `CursorChanger`.

        `cursor` may be either a `wx.Cursor` object or a constant like
        `wx.CURSOR_BULLSEYE`.
        '''
        assert isinstance(window, wx.Window)
        self.window = window
        self.cursor = cursor if isinstance(cursor, wx.Cursor) \
                      else wx.StockCursor(cursor)
        TempValueSetter.__init__(self, (window.GetCursor, window.SetCursor),
                                 self.cursor,
                                 assert_no_fiddling=False)
Ejemplo n.º 5
0
def test_dict_key():
    '''Test `TempValueSetter` with variable inputted as `(dict, key)`.'''
    a = {1: 2}

    assert a[1] == 2
    with TempValueSetter((a, 1), 'meow'):
        assert a[1] == 'meow'
    assert a[1] == 2

    b = {}

    assert sum not in b
    with TempValueSetter((b, sum), 7):
        assert b[sum] == 7
    assert sum not in b
Ejemplo n.º 6
0
 def __init__(self, window, cursor):
     '''
     Construct the `CursorChanger`.
     
     `cursor` may be either a `wx.Cursor` object or a constant like
     `wx.CURSOR_BULLSEYE`.
     '''
     assert isinstance(window, wx.Window)
     self.window = window
     self.cursor = cursor if isinstance(cursor, wx.Cursor) \
                   else wx.StockCursor(cursor)
     TempValueSetter.__init__(self,
                              (window.GetCursor, window.SetCursor),
                              self.cursor,
                              assert_no_fiddling=False)
Ejemplo n.º 7
0
def test_setter_getter():
    '''Test `TempValueSetter` with variable inputted as `(getter, setter)`.'''
    a = Object()
    a.x = 1
    getter = lambda: getattr(a, 'x')
    setter = lambda value: setattr(a, 'x', value)

    assert a.x == 1
    with TempValueSetter((getter, setter), 2):
        assert a.x == 2
    assert a.x == 1
Ejemplo n.º 8
0
def test_simple():
    '''
    Test `TempValueSetter` with variable inputted as `(obj, attribute_name)`.
    '''
    a = Object()
    a.x = 1

    assert a.x == 1
    with TempValueSetter((a, 'x'), 2):
        assert a.x == 2
    assert a.x == 1
Ejemplo n.º 9
0
def test_active():
    a = Object()
    a.x = 1

    assert a.x == 1
    temp_value_setter = TempValueSetter((a, 'x'), 2)
    assert not temp_value_setter.active
    with temp_value_setter:
        assert a.x == 2
        assert temp_value_setter.active
    assert not temp_value_setter.active
    assert a.x == 1
Ejemplo n.º 10
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