Skip to content

Commit b8989a1

Browse files
authored
Merge pull request #1339 from zm711/f-strings
Switch Neo specific Code to F strings
2 parents 5fe556a + 14a1424 commit b8989a1

14 files changed

+74
-91
lines changed

neo/core/analogsignal.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,9 @@ def _repr_pretty_(self, pp, cycle):
416416
'''
417417
Handle pretty-printing the :class:`AnalogSignal`.
418418
'''
419-
pp.text("{cls} with {channels} channels of length {length}; "
420-
"units {units}; datatype {dtype} ".format(cls=self.__class__.__name__,
421-
channels=self.shape[1],
422-
length=self.shape[0],
423-
units=self.units.dimensionality.string,
424-
dtype=self.dtype))
419+
pp.text(f"{self.__class__.__name__} with {self.shape[1]} channels of length "
420+
f"{self.shape[0]}; units {self.units.dimesionality.string}; datatype "
421+
f"{self.dtype}")
425422
if self._has_repr_pretty_attrs_():
426423
pp.breakable()
427424
self._repr_pretty_attrs_(pp, cycle)
@@ -431,8 +428,8 @@ def _pp(line):
431428
with pp.group(indent=1):
432429
pp.text(line)
433430

434-
_pp("sampling rate: {}".format(self.sampling_rate))
435-
_pp("time: {} to {}".format(self.t_start, self.t_stop))
431+
_pp(f"sampling rate: {self.sampling_rate}")
432+
_pp(f"time: {self.t_start} to {self.t_stop}")
436433

437434
def time_index(self, t):
438435
"""Return the array index (or indices) corresponding to the time (or times) `t`"""
@@ -716,7 +713,7 @@ def concatenate(self, *signals, overwrite=False, padding=False):
716713
raise MergeError(f'Signals are not continuous. Can not concatenate signals with gaps. '
717714
f'Please provide a padding value.')
718715
if padding is not False:
719-
logger.warning('Signals will be padded using {}.'.format(padding))
716+
logger.warning(f'Signals will be padded using {padding}.')
720717
if padding is True:
721718
padding = np.nan * units
722719
if isinstance(padding, pq.Quantity):

neo/core/baseneo.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ def _check_annotations(value):
3333
"""
3434
if isinstance(value, np.ndarray):
3535
if not issubclass(value.dtype.type, ALLOWED_ANNOTATION_TYPES):
36-
raise ValueError("Invalid annotation. NumPy arrays with dtype %s"
37-
"are not allowed" % value.dtype.type)
36+
raise ValueError(f"Invalid annotation. NumPy arrays with dtype {value.dtype.type}"
37+
f"are not allowed")
3838
elif isinstance(value, dict):
3939
for element in value.values():
4040
_check_annotations(element)
4141
elif isinstance(value, (list, tuple)):
4242
for element in value:
4343
_check_annotations(element)
4444
elif not isinstance(value, ALLOWED_ANNOTATION_TYPES):
45-
raise ValueError("Invalid annotation. Annotations of type %s are not"
46-
"allowed" % type(value))
45+
raise ValueError(f"Invalid annotation. Annotations of type {type(value)} are not"
46+
f"allowed")
4747

4848

4949
def merge_annotation(a, b):
@@ -58,8 +58,7 @@ def merge_annotation(a, b):
5858
For strings: concatenate with ';'
5959
Otherwise: fail if the annotations are not equal
6060
"""
61-
assert type(a) == type(b), 'type({}) {} != type({}) {}'.format(a, type(a),
62-
b, type(b))
61+
assert type(a) == type(b), f'type({a})) {type(a)} != type({b}) {type(b)}'
6362
if isinstance(a, dict):
6463
return merge_annotations(a, b)
6564
elif isinstance(a, np.ndarray): # concatenate b to a
@@ -72,7 +71,7 @@ def merge_annotation(a, b):
7271
else:
7372
return a + ";" + b
7473
else:
75-
assert a == b, '{} != {}'.format(a, b)
74+
assert a == b, f'{a} != {b}'
7675
return a
7776

7877

@@ -100,7 +99,7 @@ def merge_annotations(A, *Bs):
10099
# exc.args += ('key %s' % name,)
101100
# raise
102101
merged[name] = "MERGE CONFLICT" # temporary hack
103-
logger.debug("Merging annotations: A=%s Bs=%s merged=%s", A, Bs, merged)
102+
logger.debug(f"Merging annotations: A={A} Bs={Bs} merged={merged}")
104103
return merged
105104

106105

@@ -122,8 +121,7 @@ def intersect_annotations(A, B):
122121

123122
for key in set(A.keys()) & set(B.keys()):
124123
v1, v2 = A[key], B[key]
125-
assert type(v1) == type(v2), 'type({}) {} != type({}) {}'.format(v1, type(v1),
126-
v2, type(v2))
124+
assert type(v1) == type(v2), f'type({v1}) {type(v1)} != type({v2}) {type(v2)}'
127125
if isinstance(v1, dict) and v1 == v2:
128126
result[key] = deepcopy(v1)
129127
elif isinstance(v1, str) and v1 == v2:
@@ -299,7 +297,7 @@ def _repr_pretty_attrs_(self, pp, cycle):
299297
else:
300298
pp.breakable()
301299
with pp.group(indent=1):
302-
pp.text("{}: ".format(key))
300+
pp.text(f"{key}: ")
303301
pp.pretty(value)
304302

305303
def _repr_pretty_(self, pp, cycle):
@@ -366,8 +364,8 @@ def set_parent(self, obj):
366364
according to the type of "obj"
367365
"""
368366
if obj.__class__.__name__ not in self._parent_objects:
369-
raise TypeError("{} can only have parents of type {}, not {}".format(
370-
self.__class__.__name__, self._parent_objects, obj.__class__.__name__))
367+
raise TypeError((f"{self.__class__.__name__} can only have parents of "
368+
f"type {self._parwents_objects}, not {obj.__class__.__name__}"))
371369
loc = self._parent_objects.index(obj.__class__.__name__)
372370
parent_attr = self._parent_attrs[loc]
373371
setattr(self, parent_attr, obj)

neo/core/basesignal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def merge(self, other):
259259
if attr_self == attr_other:
260260
kwargs[name] = attr_self
261261
else:
262-
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
262+
kwargs[name] = f"merge({attr_self}, {attr_other})"
263263
merged_annotations = merge_annotations(self.annotations, other.annotations)
264264
kwargs.update(merged_annotations)
265265

neo/core/container.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def _repr_pretty_(self, pp, cycle):
563563
for container in self._child_containers:
564564
objs = getattr(self, container)
565565
if objs:
566-
vals.append('{} {}'.format(len(objs), container))
566+
vals.append(f'{objs} {container}')
567567
pp.text(', '.join(vals))
568568

569569
if self._has_repr_pretty_attrs_():
@@ -573,7 +573,7 @@ def _repr_pretty_(self, pp, cycle):
573573
for container in self._repr_pretty_containers:
574574
pp.breakable()
575575
objs = getattr(self, container)
576-
pp.text("# {} (N={})".format(container, len(objs)))
576+
pp.text(f"# {container} (N={objs})")
577577
for (i, obj) in enumerate(objs):
578578
pp.breakable()
579579
pp.text("%s: " % i)

neo/core/dataobject.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _normalize_array_annotations(value, length):
7373

7474
if not own_length == val_length:
7575
raise ValueError(
76-
"Incorrect length of array annotation: {} != {}".format(val_length, own_length))
76+
f"Incorrect length of array annotation: {val_length} != {own_length}")
7777

7878
# Local function used to check single elements of a list or an array
7979
# They must not be lists or arrays and fit the usual annotation data types
@@ -264,10 +264,10 @@ def _merge_array_annotations(self, other):
264264

265265
# Warn if keys were omitted
266266
if omitted_keys_other or omitted_keys_self:
267-
warnings.warn("The following array annotations were omitted, because they were only "
268-
"present in one of the merged objects: {} from the one that was merged "
269-
"into and {} from the one that was merged into the other"
270-
"".format(omitted_keys_self, omitted_keys_other), UserWarning)
267+
warnings.warn(f"The following array annotations were omitted, because they were only "
268+
f"present in one of the merged objects: {omitted_keys_self} from the "
269+
f"one that was merged into and {omitted_keys_other} from the one that "
270+
f"was merged into the other", UserWarning)
271271

272272
# Return the merged array_annotations
273273
return merged_array_annotations

neo/core/epoch.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def merge(self, other):
263263
if attr_self == attr_other:
264264
kwargs[name] = attr_self
265265
else:
266-
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
266+
kwargs[name] = f"merge({attr_self}, {attr_other})"
267267

268268
merged_annotations = merge_annotations(self.annotations, other.annotations)
269269
kwargs.update(merged_annotations)
@@ -354,8 +354,8 @@ def time_shift(self, t_shift):
354354

355355
def set_labels(self, labels):
356356
if self.labels is not None and self.labels.size > 0 and len(labels) != self.size:
357-
raise ValueError("Labels array has different length to times ({} != {})"
358-
.format(len(labels), self.size))
357+
raise ValueError(f"Labels array has different length to times "
358+
f"({len(labels)} != {self.size})")
359359
self._labels = np.array(labels)
360360

361361
def get_labels(self):
@@ -365,8 +365,8 @@ def get_labels(self):
365365

366366
def set_durations(self, durations):
367367
if self.durations is not None and self.durations.size > 0 and len(durations) != self.size:
368-
raise ValueError("Durations array has different length to times ({} != {})"
369-
.format(len(durations), self.size))
368+
raise ValueError("Durations array has different length to times "
369+
f"({len(durations)} != {self.size})")
370370
self._durations = durations
371371

372372
def get_durations(self):

neo/core/event.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __new__(cls, times=None, labels=None, units=None, name=None, description=Non
101101
# reference dimensionality
102102
if (len(dim) != 1 or list(dim.values())[0] != 1 or not isinstance(list(dim.keys())[0],
103103
pq.UnitTime)):
104-
ValueError("Unit {} has dimensions {}, not [time]".format(units, dim.simplified))
104+
ValueError(f"Unit {units} has dimensions {dim.simplified}, not [time].")
105105

106106
obj = pq.Quantity(times, units=dim).view(cls)
107107
obj._labels = labels
@@ -204,7 +204,7 @@ def merge(self, other):
204204
if attr_self == attr_other:
205205
kwargs[name] = attr_self
206206
else:
207-
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
207+
kwargs[name] = f"merge({attr_self}, {attr_other})"
208208

209209
print('Event: merge annotations')
210210
merged_annotations = merge_annotations(self.annotations, other.annotations)
@@ -244,8 +244,8 @@ def __getitem__(self, i):
244244

245245
def set_labels(self, labels):
246246
if self.labels is not None and self.labels.size > 0 and len(labels) != self.size:
247-
raise ValueError("Labels array has different length to times ({} != {})"
248-
.format(len(labels), self.size))
247+
raise ValueError(f"Labels array has different length to times "
248+
f"({len(labels)} != {self.size})")
249249
self._labels = np.array(labels)
250250

251251
def get_labels(self):
@@ -353,13 +353,13 @@ def to_epoch(self, pairwise=False, durations=None):
353353
times = self.times[::2]
354354
durations = self.times[1::2] - times
355355
labels = np.array(
356-
["{}-{}".format(a, b) for a, b in zip(self.labels[::2], self.labels[1::2])])
356+
[f"{a}-{b}" for a, b in zip(self.labels[::2], self.labels[1::2])])
357357
elif durations is None:
358358
# Mode 1
359359
times = self.times[:-1]
360360
durations = np.diff(self.times)
361361
labels = np.array(
362-
["{}-{}".format(a, b) for a, b in zip(self.labels[:-1], self.labels[1:])])
362+
[f"{a}-{b}" for a, b in zip(self.labels[:-1], self.labels[1:])])
363363
else:
364364
# Mode 3
365365
times = self.times

neo/core/group.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ def add(self, *objects):
141141
"""Add a new Neo object to the Group"""
142142
for obj in objects:
143143
if self.allowed_types and not isinstance(obj, self.allowed_types):
144-
raise TypeError("This Group can only contain {}, but not {}"
145-
"".format(self.allowed_types, type(obj)))
144+
raise TypeError(f"This Group can only contain {self.allowed_types}, "
145+
f"but not {type(obj)}")
146146
super().add(*objects)
147147

148148
def walk(self):

neo/core/imagesequence.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,19 @@ def _repr_pretty_(self, pp, cycle):
188188
Handle pretty-printing the :class:`ImageSequence`.
189189
"""
190190
pp.text(
191-
"{cls} {nframe} frames with width {width} px and height {height} px; "
192-
"units {units}; datatype {dtype} ".format(
193-
cls=self.__class__.__name__,
194-
nframe=self.shape[0],
195-
height=self.shape[1],
196-
width=self.shape[2],
197-
units=self.units.dimensionality.string,
198-
dtype=self.dtype,
191+
f"{self.__class__.__name__} {self.shape[0]} frames with "
192+
f"width {self.shape[2]} px and height {self.shape[1]} px; "
193+
f"units {self.units.dimensionality.string}; datatype {self.dtype} "
199194
)
200-
)
201195

202196
def _pp(line):
203197
pp.breakable()
204198
with pp.group(indent=1):
205199
pp.text(line)
206200

207201
for line in [
208-
"sampling rate: {!s}".format(self.sampling_rate),
209-
"spatial_scale: {!s}".format(self.spatial_scale),
202+
f"sampling rate: {self.sampling_rate}",
203+
f"spatial_scale: {self.spatial_scale}"
210204
]:
211205
_pp(line)
212206

neo/core/irregularlysampledsignal.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ def __repr__(self):
192192
'''
193193
Returns a string representing the :class:`IrregularlySampledSignal`.
194194
'''
195-
return '<{}({} at times {})>'.format(
196-
self.__class__.__name__, super().__repr__(), self.times)
195+
return (f"<{self.__class__.__name__}({super().__repr__()} "
196+
f"at times {self.times})>")
197197

198198
def __getitem__(self, i):
199199
'''
@@ -286,16 +286,15 @@ def _check_consistency(self, other):
286286
return
287287
# dimensionality should match
288288
if self.ndim != other.ndim:
289-
raise ValueError('Dimensionality does not match: {} vs {}'.format(
290-
self.ndim, other.ndim))
289+
raise ValueError(f'Dimensionality does not match: {self.ndim} vs {other.ndim}')
291290
# if if the other array does not have a times property,
292291
# then it should be okay to add it directly
293292
if not hasattr(other, 'times'):
294293
return
295294

296295
# if there is a times property, the times need to be the same
297296
if not (self.times == other.times).all():
298-
raise ValueError('Times do not match: {} vs {}'.format(self.times, other.times))
297+
raise ValueError(f'Times do not match: {self.times} vs {other.times}')
299298

300299
def __rsub__(self, other, *args):
301300
'''
@@ -307,12 +306,9 @@ def _repr_pretty_(self, pp, cycle):
307306
'''
308307
Handle pretty-printing the :class:`IrregularlySampledSignal`.
309308
'''
310-
pp.text("{cls} with {channels} channels of length {length}; "
311-
"units {units}; datatype {dtype} ".format(cls=self.__class__.__name__,
312-
channels=self.shape[1],
313-
length=self.shape[0],
314-
units=self.units.dimensionality.string,
315-
dtype=self.dtype))
309+
pp.text(f"{self.__class__.__name__} with {self.shape[1]} channels of length "
310+
f"{self.shape[0]}; units {self.units.dimensionality.string}; datatype "
311+
f"{self.dtype}")
316312
if self._has_repr_pretty_attrs_():
317313
pp.breakable()
318314
self._repr_pretty_attrs_(pp, cycle)
@@ -322,7 +318,7 @@ def _pp(line):
322318
with pp.group(indent=1):
323319
pp.text(line)
324320

325-
for line in ["sample times: {}".format(self.times)]:
321+
for line in [f"sample times: {self.times}"]:
326322
_pp(line)
327323

328324
@property
@@ -485,7 +481,7 @@ def merge(self, other):
485481
if attr_self == attr_other:
486482
kwargs[name] = attr_self
487483
else:
488-
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
484+
kwargs[name] = f"merge({attr_self}, {attr_other})"
489485
merged_annotations = merge_annotations(self.annotations, other.annotations)
490486
kwargs.update(merged_annotations)
491487

@@ -564,7 +560,7 @@ def concatenate(self, other, allow_overlap=False):
564560
if attr_self == attr_other:
565561
kwargs[name] = attr_self
566562
else:
567-
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
563+
kwargs[name] = f"merge({attr_self}, {attr_other})"
568564
merged_annotations = merge_annotations(self.annotations, other.annotations)
569565
kwargs.update(merged_annotations)
570566

0 commit comments

Comments
 (0)