Skip to content

Commit f40d7da

Browse files
committed
CLN: Replace comprehensions list/set/dict functions with corresponding symbols
1 parent 2dbf2a6 commit f40d7da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+167
-165
lines changed

asv_bench/benchmarks/frame_ctor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def setup(self):
2323
self.some_dict = list(self.data.values())[0]
2424
self.dict_list = [dict(zip(self.columns, row)) for row in self.frame.values]
2525

26-
self.data2 = dict(
27-
((i, dict(((j, float(j)) for j in range(100)))) for i in
28-
range(2000)))
26+
self.data2 = {i: {j: float(j) for j in range(100)}
27+
for i in range(2000)}
28+
2929

3030
def time_frame_ctor_list_of_dict(self):
3131
DataFrame(self.dict_list)

asv_bench/benchmarks/packers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _setup(self):
1717
self.N = 100000
1818
self.C = 5
1919
self.index = date_range('20000101', periods=self.N, freq='H')
20-
self.df = DataFrame(dict(('float{0}'.format(i), randn(self.N)) for i in range(self.C)), index=self.index)
20+
self.df = DataFrame({'float{0}'.format(i): randn(self.N) for i in range(self.C)}, index=self.index)
2121
self.df2 = self.df.copy()
2222
self.df2['object'] = [('%08x' % randrange((16 ** 8))) for _ in range(self.N)]
2323
self.remove(self.f)

asv_bench/benchmarks/replace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class replace_large_dict(object):
2323
def setup(self):
2424
self.n = (10 ** 6)
2525
self.start_value = (10 ** 5)
26-
self.to_rep = dict(((i, (self.start_value + i)) for i in range(self.n)))
26+
self.to_rep = {i: self.start_value + i for i in range(self.n)}
2727
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
2828

2929
def time_replace_large_dict(self):
@@ -35,8 +35,8 @@ class replace_convert(object):
3535

3636
def setup(self):
3737
self.n = (10 ** 3)
38-
self.to_ts = dict(((i, pd.Timestamp(i)) for i in range(self.n)))
39-
self.to_td = dict(((i, pd.Timedelta(i)) for i in range(self.n)))
38+
self.to_ts = {i: pd.Timestamp(i) for i in range(self.n)}
39+
self.to_td = {i: pd.Timedelta(i) for i in range(self.n)}
4040
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
4141
self.df = DataFrame({'A': np.random.randint(self.n, size=(10 ** 3)),
4242
'B': np.random.randint(self.n, size=(10 ** 3))})

ci/lint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ if [ "$LINT" ]; then
9797
fi
9898
echo "Check for use of lists instead of generators in built-in Python functions DONE"
9999

100+
echo "Check for use of comprehensions using set(), dict() or list()"
101+
102+
# Example: Avoid `list(i for i in some_iterator)` in favor of `[i for i in some_iterator]`
103+
#
104+
# Check the following functions:
105+
# list(), set(), dict()
106+
grep -R --include="*.py*" -E "(list|dict|[^frozen]set)\([^\{)=]* for .* in [^}]*\)"
107+
108+
if [ $? = "0" ]; then
109+
RET=1
110+
fi
111+
echo "Check for use of comprehensions using set(), dict() or list() DONE"
112+
100113
else
101114
echo "NOT Linting"
102115
fi

doc/sphinxext/numpydoc/phantom_import.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def import_phantom_module(xml_file):
6060
# Sort items so that
6161
# - Base classes come before classes inherited from them
6262
# - Modules come before their contents
63-
all_nodes = dict((n.attrib['id'], n) for n in root)
64-
63+
all_nodes = {n.attrib['id']: n for n in root}
64+
6565
def _get_bases(node, recurse=False):
6666
bases = [x.attrib['ref'] for x in node.findall('base')]
6767
if recurse:

pandas/_libs/parsers.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ def _concatenate_chunks(list chunks):
22272227
for name in names:
22282228
arrs = [chunk.pop(name) for chunk in chunks]
22292229
# Check each arr for consistent types.
2230-
dtypes = set(a.dtype for a in arrs)
2230+
dtypes = {a.dtype for a in arrs}
22312231
if len(dtypes) > 1:
22322232
common_type = np.find_common_type(dtypes, [])
22332233
if common_type == np.object:

pandas/_libs/tslibs/offsets.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ from np_datetime cimport (pandas_datetimestruct,
3333
_MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
3434
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
3535
_int_to_month = {(k + 1): v for k, v in enumerate(_MONTHS)}
36-
_month_to_int = dict((v, k) for k, v in _int_to_month.items())
36+
_month_to_int = {v: k for k, v in _int_to_month.items()}
3737

3838

3939
class WeekDay(object):

pandas/_libs/tslibs/resolution.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ _ONE_HOUR = 60 * _ONE_MINUTE
5353
_ONE_DAY = 24 * _ONE_HOUR
5454

5555
DAYS = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
56-
_weekday_rule_aliases = dict((k, v) for k, v in enumerate(DAYS))
56+
_weekday_rule_aliases = {k: v for k, v in enumerate(DAYS)}
5757

5858
_MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
5959
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']

pandas/_version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
141141
if verbose:
142142
print("keywords are unexpanded, not using")
143143
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
144-
refs = set(r.strip() for r in refnames.strip("()").split(","))
144+
refs = {r.strip() for r in refnames.strip("()").split(",")}
145145
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
146146
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
147147
TAG = "tag: "
148-
tags = set(r[len(TAG):] for r in refs if r.startswith(TAG))
148+
tags = {r[len(TAG):] for r in refs if r.startswith(TAG)}
149149
if not tags:
150150
# Either we're using git < 1.8.3, or there really are no tags. We use
151151
# a heuristic: assume all version tags have a digit. The old git %d
@@ -154,7 +154,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
154154
# between branches and tags. By ignoring refnames without digits, we
155155
# filter out many common branch names like "release" and
156156
# "stabilization", as well as "HEAD" and "master".
157-
tags = set(r for r in refs if re.search(r'\d', r))
157+
tags = {r for r in refs if re.search(r'\d', r)}
158158
if verbose:
159159
print("discarding '{}', no digits".format(",".join(refs - tags)))
160160
if verbose:

pandas/core/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def map_indices_py(arr):
347347
Returns a dictionary with (element, index) pairs for each element in the
348348
given array/list
349349
"""
350-
return dict((x, i) for i, x in enumerate(arr))
350+
return {x: i for i, x in enumerate(arr)}
351351

352352

353353
def union(*seqs):

0 commit comments

Comments
 (0)