Skip to content

Commit 4de7281

Browse files
More f-strings, less format() (#10505)
1 parent 29b9f5a commit 4de7281

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

xarray/backends/common.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,10 @@ def _infer_dtype(array, name=None):
560560

561561
native_dtypes = set(np.vectorize(type, otypes=[object])(array.ravel()))
562562
if len(native_dtypes) > 1 and native_dtypes != {bytes, str}:
563+
native_dtype_names = ", ".join(x.__name__ for x in native_dtypes)
563564
raise ValueError(
564-
"unable to infer dtype on variable {!r}; object array "
565-
"contains mixed native types: {}".format(
566-
name, ", ".join(x.__name__ for x in native_dtypes)
567-
)
565+
f"unable to infer dtype on variable {name!r}; object array "
566+
f"contains mixed native types: {native_dtype_names}"
568567
)
569568

570569
element = array[(0,) * array.ndim]

xarray/computation/apply_ufunc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ def __repr__(self):
141141
return f"{type(self).__name__}({list(self.input_core_dims)!r}, {list(self.output_core_dims)!r})"
142142

143143
def __str__(self):
144-
lhs = ",".join("({})".format(",".join(dims)) for dims in self.input_core_dims)
145-
rhs = ",".join("({})".format(",".join(dims)) for dims in self.output_core_dims)
144+
comma_separated = ",".join
145+
lhs = comma_separated(
146+
f"({comma_separated(dims)})" for dims in self.input_core_dims
147+
)
148+
rhs = comma_separated(
149+
f"({comma_separated(dims)})" for dims in self.output_core_dims
150+
)
146151
return f"{lhs}->{rhs}"
147152

148153
def to_gufunc_string(self, exclude_dims=frozenset()):

xarray/computation/rolling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __repr__(self) -> str:
132132
"""provide a nice str repr of our rolling object"""
133133

134134
attrs = ",".join(
135-
"{k}->{v}{c}".format(k=k, v=w, c="(center)" if c else "")
135+
f"{k}->{w}{'(center)' if c else ''}"
136136
for k, w, c in zip(self.dim, self.window, self.center, strict=True)
137137
)
138138
return f"{self.__class__.__name__} [{attrs}]"

xarray/core/formatting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,17 @@ def summarize_variable(
340340
first_col = pretty_print(first_col, col_width)
341341

342342
if variable.dims:
343-
dims_str = "({}) ".format(", ".join(map(str, variable.dims)))
343+
dims_str = ", ".join(map(str, variable.dims))
344+
dims_str = f"({dims_str}) "
344345
else:
345346
dims_str = ""
346347

347-
nbytes_str = f" {render_human_readable_nbytes(variable.nbytes)}"
348-
front_str = f"{first_col}{dims_str}{variable.dtype}{nbytes_str} "
348+
front_str = f"{first_col}{dims_str}{variable.dtype} {render_human_readable_nbytes(variable.nbytes)} "
349349

350350
values_width = max_width - len(front_str)
351351
values_str = inline_variable_array_repr(variable, values_width)
352352

353-
return front_str + values_str
353+
return f"{front_str}{values_str}"
354354

355355

356356
def summarize_attr(key, value, col_width=None):

0 commit comments

Comments
 (0)