コード例 #1
0
class UDPClient():
    def __init__(self, host, port):
        self.host = host
        self.port = int(port)

    def send(self, request):
        print('Connecting to server at {}:{}'.format(self.host, self.port))
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(request, (self.host, self.port))
            response, ip = s.recvfrom(BUFFER_SIZE)
            return response
        except socket.error:
            print("Error! {}".format(socket.error))
            exit()

    lru_cache(5)

    def put(self, key, payload):
        bloomFilter.add(key)
        return self.send(payload)

    lru_cache(5)

    def get_request(self, key, payload):
        if bloomFilter.is_member(key):
            return self.send(payload)

    lru_cache(5)

    def delete(self, key, payload):
        if bloomFilter.is_member(key):
            return self.send(payload)
コード例 #2
0
ファイル: test_lru_cache.py プロジェクト: zjaved101/CMPE273
def lru_cache_dec(size):
    # global LRU_CACHE
    # if not LRU_CACHE:
    # LRU_CACHE = lru_cache(size)

    LRU_CACHE = lru_cache(size)

    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # if func.__name__ == 'delete':
            #     print("==DELETE==")
            #     LRU_CACHE.delete(args[0])
            #     return func(*args, **kwargs)

            cache = LRU_CACHE.get(args[0])
            if cache:
                print('==HIT==')
                # print(cache)
                return cache
            else:
                print("==MISS==")
                # if func.__name__ == 'put':
                if len(args) > 1:
                    LRU_CACHE.add(args[0], args[1])
                else:
                    value = func(*args, **kwargs)
                    LRU_CACHE.add(args[0], value)
                    return value

                # return func(*args, **kwargs)

        return wrapper

    return decorator
コード例 #3
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_lru(self):
        function = self.function
        wrapper = lru_cache.lru_cache(max_size=3)(function)
        # queue: []

        function.return_value = 1
        value = wrapper(1)
        function.assert_has_calls([mock.call(1)])
        # queue: [1]

        function.return_value = 2
        value = wrapper(2)
        function.assert_has_calls([mock.call(1), mock.call(2)])
        # queue: [1, 2]

        function.return_value = 3
        value = wrapper(3)
        function.assert_has_calls([mock.call(1), mock.call(2), mock.call(3)])
        # queue: [1, 2, 3]

        function.return_value = 4
        value = wrapper(4)
        function.assert_has_calls([
            mock.call(1), mock.call(2), mock.call(3),
            mock.call(4)])
        # queue: [2, 3, 4]

        function.return_value = 1
        value = wrapper(1)
        function.assert_has_calls([
            mock.call(1), mock.call(2), mock.call(3),
            mock.call(4), mock.call(1)])
        # queue: [3, 4, 1]

        function.return_value = 3
        value = wrapper(3)
        function.assert_has_calls([
            mock.call(1), mock.call(2), mock.call(3),
            mock.call(4), mock.call(1)])
        # queue: [4, 1, 3]

        function.return_value = 2
        value = wrapper(2)
        function.assert_has_calls([
            mock.call(1), mock.call(2), mock.call(3),
            mock.call(4), mock.call(1), mock.call(2)])
        # queue: [1, 3, 2]

        function.return_value = 4
        value = wrapper(4)
        function.assert_has_calls([
            mock.call(1), mock.call(2), mock.call(3),
            mock.call(4), mock.call(1), mock.call(2),
            mock.call(4)])
コード例 #4
0
 def __init__(self, client):
     self.client = client
     self.method_proxies = {}
     for name, orig_method in inspect.getmembers(client, inspect.ismethod):
         if not name.startswith('_'):
             new_method = orig_method
             if client_lru_cache:
                 new_method = lru_cache()(new_method)
             if client_methods_gatekeeper:
                 new_method = client_meth_wrapper(new_method, orig_method)
             self.method_proxies[orig_method.__name__] = new_method
コード例 #5
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_kwargs(self):
        function = self.function
        wrapper = lru_cache.lru_cache()(function)
        function.return_value = 1

        value = wrapper(a=1, b=2)
        self.assertEqual(value, 1)
        function.assert_called_once_with(a=1, b=2)

        value = wrapper(b=2, a=1)
        self.assertEqual(value, 1)
        function.assert_has_calls([mock.call(a=1, b=2)])

        function.return_value = 2
        value = wrapper(c=1, b=2)
        self.assertEqual(value, 2)
        function.assert_called_with(c=1, b=2)
        function.assert_has_calls([mock.call(a=1, b=2), mock.call(c=1, b=2)])
コード例 #6
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_kwargs(self):
        function = self.function
        wrapper = lru_cache.lru_cache()(function)
        function.return_value = 1

        value = wrapper(a=1, b=2)
        self.assertEqual(value, 1)
        function.assert_called_once_with(a=1, b=2)

        value = wrapper(b=2, a=1)
        self.assertEqual(value, 1)
        function.assert_has_calls([mock.call(a=1, b=2)])

        function.return_value = 2
        value = wrapper(c=1, b=2)
        self.assertEqual(value, 2)
        function.assert_called_with(c=1, b=2)
        function.assert_has_calls([mock.call(a=1, b=2), mock.call(c=1, b=2)])
コード例 #7
0
ファイル: cache_client.py プロジェクト: zjaved101/CMPE273
def lru_cache_dec(size):
    LRU_CACHE = lru_cache(size)

    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            cache = LRU_CACHE.get(args[0])
            if cache:
                print(cache)
                return cache
            else:
                value = func(*args, **kwargs)
                LRU_CACHE.add(args[0], value)
                print(value)
                return value

        return wrapper

    return decorator
コード例 #8
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_args(self):
        function = self.function
        wrapper = lru_cache.lru_cache()(function)
        function.return_value = 1

        value = wrapper(1)
        self.assertEqual(value, 1)
        function.assert_called_once_with(1)

        value = wrapper(1)
        self.assertEqual(value, 1)
        function.assert_called_once_with(1)
        function.assert_has_calls([mock.call(1)])

        function.return_value = 2
        value = wrapper(2)
        self.assertEqual(value, 2)
        function.assert_called_with(2)
        function.assert_has_calls([mock.call(1), mock.call(2)])
コード例 #9
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_args(self):
        function = self.function
        wrapper = lru_cache.lru_cache()(function)
        function.return_value = 1

        value = wrapper(1)
        self.assertEqual(value, 1)
        function.assert_called_once_with(1)

        value = wrapper(1)
        self.assertEqual(value, 1)
        function.assert_called_once_with(1)
        function.assert_has_calls([mock.call(1)])

        function.return_value = 2
        value = wrapper(2)
        self.assertEqual(value, 2)
        function.assert_called_with(2)
        function.assert_has_calls([mock.call(1), mock.call(2)])
コード例 #10
0
ファイル: test_lru_cache.py プロジェクト: dalazx/bb-test
    def test_lru(self):
        function = self.function
        wrapper = lru_cache.lru_cache(max_size=3)(function)
        # queue: []

        function.return_value = 1
        value = wrapper(1)
        function.assert_has_calls([mock.call(1)])
        # queue: [1]

        function.return_value = 2
        value = wrapper(2)
        function.assert_has_calls([mock.call(1), mock.call(2)])
        # queue: [1, 2]

        function.return_value = 3
        value = wrapper(3)
        function.assert_has_calls([mock.call(1), mock.call(2), mock.call(3)])
        # queue: [1, 2, 3]

        function.return_value = 4
        value = wrapper(4)
        function.assert_has_calls(
            [mock.call(1),
             mock.call(2),
             mock.call(3),
             mock.call(4)])
        # queue: [2, 3, 4]

        function.return_value = 1
        value = wrapper(1)
        function.assert_has_calls([
            mock.call(1),
            mock.call(2),
            mock.call(3),
            mock.call(4),
            mock.call(1)
        ])
        # queue: [3, 4, 1]

        function.return_value = 3
        value = wrapper(3)
        function.assert_has_calls([
            mock.call(1),
            mock.call(2),
            mock.call(3),
            mock.call(4),
            mock.call(1)
        ])
        # queue: [4, 1, 3]

        function.return_value = 2
        value = wrapper(2)
        function.assert_has_calls([
            mock.call(1),
            mock.call(2),
            mock.call(3),
            mock.call(4),
            mock.call(1),
            mock.call(2)
        ])
        # queue: [1, 3, 2]

        function.return_value = 4
        value = wrapper(4)
        function.assert_has_calls([
            mock.call(1),
            mock.call(2),
            mock.call(3),
            mock.call(4),
            mock.call(1),
            mock.call(2),
            mock.call(4)
        ])
コード例 #11
0
ファイル: memogen.py プロジェクト: foodhype/memogen
    def __init__(self, cache):
        self.cache = cache

    def __call__(self, func):
        # Decorator to update wrapper to look like func.
        @wraps(func)
        def wrapper(*args, **kwargs):
            # Ensure key is immutable.
            key = dumps((args, kwargs))
            if key not in self.cache:
                self.cache[key] = func(*args, **kwargs)
            return self.cache[key]

        return wrapper


@memo(lru_cache(3))
def fibo(n):
    if n == 0 or n == 1:
        return n
    else:
        return fibo(n - 1) + fibo(n - 2)


def main():
    print fibo(200)


if __name__ == "__main__":
    main()