From 8224ba2831eede1cc2f4f32f954af0cdfbc7f87f Mon Sep 17 00:00:00 2001 From: Python3pkg Date: Mon, 22 May 2017 11:29:06 -0700 Subject: [PATCH] Convert to Python3 --- Pythine/__init__.py | 2 +- Pythine/pythine.py | 17 +++++++++-------- tests/test_class_init.py | 7 ++++--- tests/test_decorator.py | 7 ++++--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Pythine/__init__.py b/Pythine/__init__.py index 1f27b7e..ae7c16f 100644 --- a/Pythine/__init__.py +++ b/Pythine/__init__.py @@ -1,5 +1,5 @@ -from pythine import Pythine +from .pythine import Pythine __version__ = '0.0.1' diff --git a/Pythine/pythine.py b/Pythine/pythine.py index d1ce01d..73ca9e5 100644 --- a/Pythine/pythine.py +++ b/Pythine/pythine.py @@ -1,5 +1,6 @@ import threading -from itertools import izip +from functools import reduce + __author = 'zhengxu' @@ -15,10 +16,10 @@ def __init__(self, it): def __iter__(self): return self - def next(self): + def __next__(self): self.lock.acquire() try: - return self.it.next() + return next(self.it) finally: self.lock.release() @@ -51,7 +52,7 @@ def __init__(self, func, thread_num=5): def _thread_worker(self, lock_iter, result_map): while True: try: - task_index, (task_args, task_kwargs) = lock_iter.next() + task_index, (task_args, task_kwargs) = next(lock_iter) task_return = self._func(*task_args, **task_kwargs) result_map[task_index] = task_return except StopIteration: @@ -86,7 +87,7 @@ def _check_list_args(*args, **kwargs): if not isinstance(arg, list): args[i] = [arg] * seq_len # normalize kwargs in place - for i, k in enumerate(kwargs.keys()): + for i, k in enumerate(list(kwargs.keys())): if not isinstance(kwargs[k], list): kwargs[k] = [kwargs[k]] * seq_len return seq_len @@ -103,9 +104,9 @@ def __call__(self, *args, **kwargs): # Make function call transparent if user only give one set of argument return self._func(*args, **kwargs) else: - args_list = zip(*args) or [()] * seq_len - kwargs_list = [dict(zip(kwargs.keys(), v)) for v in izip(*kwargs.itervalues())] or [{}] * seq_len - lock_iter = LockedIterator(enumerate(zip(args_list, kwargs_list))) + args_list = list(zip(*args)) or [()] * seq_len + kwargs_list = [dict(list(zip(list(kwargs.keys()), v))) for v in zip(*iter(kwargs.values()))] or [{}] * seq_len + lock_iter = LockedIterator(enumerate(list(zip(args_list, kwargs_list)))) result_map = LockedList([None] * seq_len) thread_group = [threading.Thread(target=self._thread_worker, args=(lock_iter, result_map)) for _ in range(thread_num)] diff --git a/tests/test_class_init.py b/tests/test_class_init.py index 8d18b03..82d67f4 100644 --- a/tests/test_class_init.py +++ b/tests/test_class_init.py @@ -2,6 +2,7 @@ import requests from Pythine import Pythine +from functools import reduce def _slow_network(url): @@ -19,10 +20,10 @@ def time_slow_network(test_time): t1 = time.time() ret = slow_network([url] * test_time) t2 = time.time() - print "Without thread engine: %f" % (t1-t0) - print "With thread engine: %f" % (t2-t1) + print("Without thread engine: %f" % (t1-t0)) + print("With thread engine: %f" % (t2-t1)) # check result - print reduce(lambda x, y: x and bool(y), ret, True) + print(reduce(lambda x, y: x and bool(y), ret, True)) pass if __name__ == '__main__': diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 6d537af..64dee25 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -3,6 +3,7 @@ import requests from Pythine.pythine import Pythine +from functools import reduce __author__ = 'zhengxu' @@ -20,8 +21,8 @@ def benchmark_slow_network(test_time): t1 = time.time() ret = slow_network([url] * test_time, __pythine_thread_num=8) t2 = time.time() - print "Without thread engine: %f" % (t1-t0) - print "With thread engine: %f" % (t2-t1) + print("Without thread engine: %f" % (t1-t0)) + print("With thread engine: %f" % (t2-t1)) # check result assert reduce(lambda x, y: x and bool(y), ret, True), "Some error occured!" pass @@ -32,7 +33,7 @@ def transparent_call(): t0 = time.time() ret = slow_network(url) t1 = time.time() - print "With thread engine but transparent call: %f" % (t1-t0) + print("With thread engine but transparent call: %f" % (t1-t0)) assert bool(ret), "No output" pass