Skip to content

Commit 9ecbb8b

Browse files
Use 'zip' and 'map' instead of itertools.izip or imap #295
Python 2.x introduced the itertools module, which defined variants of the global zip(), map(), and filter() functions that returned iterators instead of lists.In Python 3, those global functions return iterators, so those functions in the itertools module have been eliminated.Therefore we use them as 'map','zip' Signed-off-by: Abhishek Kumar <abhishek.kasyap09@gmail.com>
1 parent 9e04eb5 commit 9ecbb8b

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/licensedcode/index.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@
3232
from collections import defaultdict
3333
import cPickle
3434
from functools import partial
35-
from itertools import izip
35+
36+
# Python 2 and 3 support
37+
try:
38+
# Python 2
39+
import itertools.izip as zip
40+
except ImportError:
41+
# Python 3
42+
pass
43+
3644
from operator import itemgetter
3745
import sys
3846
from time import time
@@ -297,7 +305,7 @@ def _add_rules(self, rules, _ranked_tokens=global_tokens_by_ranks,
297305
# assigned randomly here at first by unzipping: we get the frequencies
298306
# and tokens->id at once this way
299307
########################################################################
300-
tokens_by_tid, frequencies_by_tid = izip(*frequencies_by_token.items())
308+
tokens_by_tid, frequencies_by_tid = zip(*frequencies_by_token.items())
301309
self.tokens_by_tid = tokens_by_tid
302310
self.len_tokens = len_tokens = len(tokens_by_tid)
303311
msg = 'Cannot support more than licensedcode.index.MAX_TOKENS: %d' % MAX_TOKENS

src/licensedcode/tokenize.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
from __future__ import unicode_literals
2929

3030
from itertools import islice
31-
from itertools import izip
31+
32+
# Python 2 and 3 support
33+
try:
34+
# Python 2
35+
import itertools.izip as zip
36+
except ImportError:
37+
# Python 3
38+
pass
39+
3240
import re
3341
from zlib import crc32
3442

@@ -168,7 +176,7 @@ def ngrams(iterable, ngram_length):
168176
>>> list(ngrams(tuple([1,2,3,4,5]), 2))
169177
[(1, 2), (2, 3), (3, 4), (4, 5)]
170178
"""
171-
return izip(*(islice(iterable, i, None) for i in range(ngram_length)))
179+
return zip(*(islice(iterable, i, None) for i in range(ngram_length)))
172180

173181

174182
def select_ngrams(ngrams, with_pos=False):

src/plugincode/output.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
from __future__ import unicode_literals
2929

3030
from functools import partial
31-
from itertools import imap
31+
32+
# Python 2 and 3 support
33+
try:
34+
# Python 2
35+
import itertools.imap as map
36+
except ImportError:
37+
# Python 3
38+
pass
3239

3340
from plugincode import CodebasePlugin
3441
from plugincode import PluginManager
@@ -104,7 +111,7 @@ def get_files(cls, codebase, **kwargs):
104111

105112
strip_root = kwargs.get('strip_root', False)
106113
resources = codebase.walk_filtered(topdown=True, skip_root=strip_root)
107-
return imap(serializer, resources)
114+
return map(serializer, resources)
108115

109116

110117
output_plugins = PluginManager(

src/scancode/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@
3636
from collections import defaultdict
3737
from collections import OrderedDict
3838
from functools import partial
39-
from itertools import imap
39+
40+
# Python 2 and 3 support
41+
try:
42+
# Python 2
43+
import itertools.imap as map
44+
except ImportError:
45+
# Python 3
46+
pass
47+
4048
import os
4149
import sys
4250
from time import time
@@ -1120,7 +1128,7 @@ def scan_codebase(codebase, scanners, processes=1, timeout=DEFAULT_TIMEOUT,
11201128
pool.close()
11211129
else:
11221130
# no multiprocessing with processes=0 or -1
1123-
scans = imap(runner, resources)
1131+
scans = map(runner, resources)
11241132

11251133
if progress_manager:
11261134
scans = progress_manager(scans)

0 commit comments

Comments
 (0)