Skip to content

Commit eee2e81

Browse files
committed
[SPARK-XXXXX][PYTHON] Remove unnecessary .keys() in dict iterations
Iterating over `dict.keys()` is redundant when iterating the dict directly suffices. Similarly, `len(dict.keys())` can be simplified to `len(dict)`. Co-authored-by: Isaac
1 parent 83a0267 commit eee2e81

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

python/pyspark/memory_profiler_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def items(self) -> Iterator[Tuple[str, Iterator[Tuple[int, Any]]]]:
123123
measures = self[code]
124124
if not measures:
125125
continue # skip if no measurement
126-
line_iterator = ((line, measures[line]) for line in measures.keys())
126+
line_iterator = ((line, measures[line]) for line in measures)
127127
yield (filename, line_iterator)
128128

129129
class UDFLineProfiler(LineProfiler):

python/pyspark/pandas/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def __setattr__(self, key: str, val: Any) -> None:
509509
canonical_key = prefix + key
510510

511511
candidates = [
512-
k for k in d.keys() if all(x in k.split(".") for x in canonical_key.split("."))
512+
k for k in d if all(x in k.split(".") for x in canonical_key.split("."))
513513
]
514514
if len(candidates) == 1 and candidates[0] == canonical_key:
515515
set_option(canonical_key, val)
@@ -528,7 +528,7 @@ def __getattr__(self, key: str) -> Union["DictWrapper", Any]:
528528
canonical_key = prefix + key
529529

530530
candidates = [
531-
k for k in d.keys() if all(x in k.split(".") for x in canonical_key.split("."))
531+
k for k in d if all(x in k.split(".") for x in canonical_key.split("."))
532532
]
533533
if len(candidates) == 1 and candidates[0] == canonical_key:
534534
return get_option(canonical_key)
@@ -549,7 +549,7 @@ def __dir__(self) -> List[str]:
549549
candidates = d.keys()
550550
offset = 0
551551
else:
552-
candidates = [k for k in d.keys() if all(x in k.split(".") for x in prefix.split("."))]
552+
candidates = [k for k in d if all(x in k.split(".") for x in prefix.split("."))]
553553
offset = len(prefix) + 1 # prefix (e.g. "compute.") to trim.
554554
return [c[offset:] for c in candidates]
555555

python/pyspark/pandas/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7072,7 +7072,7 @@ def pivot_table(
70727072
index_map[colname] = None
70737073
internal = InternalFrame(
70747074
spark_frame=sdf,
7075-
index_spark_columns=[scol_for(sdf, col) for col in index_map.keys()],
7075+
index_spark_columns=[scol_for(sdf, col) for col in index_map],
70767076
index_names=list(index_map.values()),
70777077
column_label_names=[columns],
70787078
)
@@ -9778,7 +9778,7 @@ def astype(self, dtype: Union[str, Dtype, Dict[Name, Union[str, Dtype]]]) -> "Da
97789778
applied = []
97799779
if is_dict_like(dtype):
97809780
dtype_dict = cast(Dict[Name, Union[str, Dtype]], dtype)
9781-
for col_name in dtype_dict.keys():
9781+
for col_name in dtype_dict:
97829782
if col_name not in self.columns:
97839783
raise KeyError(
97849784
"Only a column name can be used for the key in a dtype mappings argument."

python/pyspark/pandas/namespace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ def pandas_to_datetime(
17841784
if isinstance(arg, Series):
17851785
return arg.pandas_on_spark.transform_batch(pandas_to_datetime)
17861786
if isinstance(arg, DataFrame):
1787-
unit = {k: _unit_map[k.lower()] for k in arg.keys() if k.lower() in _unit_map}
1787+
unit = {k: _unit_map[k.lower()] for k in arg if k.lower() in _unit_map}
17881788
unit_rev = {v: k for k, v in unit.items()}
17891789
list_cols = [unit_rev["year"], unit_rev["month"], unit_rev["day"]]
17901790
for u in ["h", "m", "s", "ms", "us"]:

python/pyspark/pipelines/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def unpack_pipeline_spec(spec_data: Mapping[str, Any]) -> PipelineSpec:
165165
"libraries",
166166
}
167167
REQUIRED_FIELDS = ["name", "storage"]
168-
for key in spec_data.keys():
168+
for key in spec_data:
169169
if key not in ALLOWED_FIELDS:
170170
raise PySparkException(
171171
errorClass="PIPELINE_SPEC_UNEXPECTED_FIELD", messageParameters={"field_name": key}

python/pyspark/sql/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,14 +2595,14 @@ def _int_size_to_type(
25952595
}
25962596

25972597
# compute array typecode mappings for signed integer types
2598-
for _typecode in _array_signed_int_typecode_ctype_mappings.keys():
2598+
for _typecode in _array_signed_int_typecode_ctype_mappings:
25992599
size = ctypes.sizeof(_array_signed_int_typecode_ctype_mappings[_typecode]) * 8
26002600
dt = _int_size_to_type(size)
26012601
if dt is not None:
26022602
_array_type_mappings[_typecode] = dt
26032603

26042604
# compute array typecode mappings for unsigned integer types
2605-
for _typecode in _array_unsigned_int_typecode_ctype_mappings.keys():
2605+
for _typecode in _array_unsigned_int_typecode_ctype_mappings:
26062606
# JVM does not have unsigned types, so use signed types that is at least 1
26072607
# bit larger to store
26082608
size = ctypes.sizeof(_array_unsigned_int_typecode_ctype_mappings[_typecode]) * 8 + 1

python/pyspark/testing/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,9 +1063,9 @@ def compare_vals(val1, val2):
10631063
return all(compare_vals(x, y) for x, y in zip(val1, val2))
10641064
elif isinstance(val1, dict) and isinstance(val2, dict):
10651065
return (
1066-
len(val1.keys()) == len(val2.keys())
1066+
len(val1) == len(val2)
10671067
and val1.keys() == val2.keys()
1068-
and all(compare_vals(val1[k], val2[k]) for k in val1.keys())
1068+
and all(compare_vals(val1[k], val2[k]) for k in val1)
10691069
)
10701070
elif isinstance(val1, float) and isinstance(val2, float):
10711071
if abs(val1 - val2) > (atol + rtol * abs(val2)):

0 commit comments

Comments
 (0)