Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1f42e6

Browse files
committed
Minor cleanups in docstrings and error messages.
1 parent 0fa9c4b commit c1f42e6

5 files changed

Lines changed: 31 additions & 35 deletions

File tree

sdks/python/apache_beam/io/filebasedsource.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def split(
5454
if start_position or stop_position:
5555
raise ValueError(
5656
'Multi-level initial splitting is not supported. Expected start and '
57-
'stop positions to be None. Received %r and %r respectively.',
58-
start_position, stop_position)
57+
'stop positions to be None. Received %r and %r respectively.' %
58+
(start_position, stop_position))
5959

6060
for source in self._sources:
6161
# We assume all sub-sources to produce bundles that specify weight using
@@ -224,25 +224,22 @@ def splittable(self):
224224

225225

226226
class _SingleFileSource(iobase.BoundedSource):
227-
"""Denotes a source for a specific file type.
228-
229-
This should be sub-classed to add support for reading a new file type.
230-
"""
227+
"""Denotes a source for a specific file type."""
231228

232229
def __init__(self, file_based_source, file_name, start_offset, stop_offset,
233230
min_bundle_size=0):
234-
if not (isinstance(start_offset, int) or isinstance(start_offset, long)):
235-
raise ValueError(
236-
'start_offset must be a number. Received: %r', start_offset)
231+
if not isinstance(start_offset, (int, long)):
232+
raise TypeError(
233+
'start_offset must be a number. Received: %r' % start_offset)
237234
if stop_offset != range_trackers.OffsetRangeTracker.OFFSET_INFINITY:
238-
if not (isinstance(stop_offset, int) or isinstance(stop_offset, long)):
239-
raise ValueError(
240-
'stop_offset must be a number. Received: %r', stop_offset)
235+
if not isinstance(stop_offset, (int, long)):
236+
raise TypeError(
237+
'stop_offset must be a number. Received: %r' % stop_offset)
241238
if start_offset >= stop_offset:
242239
raise ValueError(
243240
'start_offset must be smaller than stop_offset. Received %d and %d '
244-
'for start and stop offsets respectively',
245-
start_offset, stop_offset)
241+
'for start and stop offsets respectively' %
242+
(start_offset, stop_offset))
246243

247244
self._file_name = file_name
248245
self._is_gcs_file = file_name.startswith('gs://') if file_name else False

sdks/python/apache_beam/io/filebasedsource_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,19 @@ def test_source_creation_fails_for_non_number_offsets(self):
384384

385385
fbs = LineSource('dymmy_pattern')
386386

387-
with self.assertRaisesRegexp(ValueError, start_not_a_number_error):
387+
with self.assertRaisesRegexp(TypeError, start_not_a_number_error):
388388
SingleFileSource(
389389
fbs, file_name='dummy_file', start_offset='aaa', stop_offset='bbb')
390-
with self.assertRaisesRegexp(ValueError, start_not_a_number_error):
390+
with self.assertRaisesRegexp(TypeError, start_not_a_number_error):
391391
SingleFileSource(
392392
fbs, file_name='dummy_file', start_offset='aaa', stop_offset=100)
393-
with self.assertRaisesRegexp(ValueError, stop_not_a_number_error):
393+
with self.assertRaisesRegexp(TypeError, stop_not_a_number_error):
394394
SingleFileSource(
395395
fbs, file_name='dummy_file', start_offset=100, stop_offset='bbb')
396-
with self.assertRaisesRegexp(ValueError, stop_not_a_number_error):
396+
with self.assertRaisesRegexp(TypeError, stop_not_a_number_error):
397397
SingleFileSource(
398398
fbs, file_name='dummy_file', start_offset=100, stop_offset=None)
399-
with self.assertRaisesRegexp(ValueError, start_not_a_number_error):
399+
with self.assertRaisesRegexp(TypeError, start_not_a_number_error):
400400
SingleFileSource(
401401
fbs, file_name='dummy_file', start_offset=None, stop_offset=100)
402402

sdks/python/apache_beam/io/iobase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,11 @@ class RangeTracker(object):
545545

546546
def start_position(self):
547547
"""Returns the starting position of the current range, inclusive."""
548-
raise NotImplementedError
548+
raise NotImplementedError(type(self))
549549

550550
def stop_position(self):
551551
"""Returns the ending position of the current range, exclusive."""
552-
raise NotImplementedError
552+
raise NotImplementedError(type(self))
553553

554554
def try_claim(self, position): # pylint: disable=unused-argument
555555
"""Atomically determines if a record at a split point is within the range.

sdks/python/apache_beam/io/range_trackers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ def try_split(self, split_offset):
138138
return
139139

140140
logging.debug('Agreeing to split %r at %d', self, split_offset)
141-
self._stop_offset = split_offset
142141

143142
split_fraction = (float(split_offset - self._start_offset) / (
144143
self._stop_offset - self._start_offset))
144+
self._stop_offset = split_offset
145145

146146
return self._stop_offset, split_fraction
147147

sdks/python/apache_beam/io/source_test_utils.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,43 +201,42 @@ def _assertSplitAtFractionBehavior(
201201
if split_result is not None:
202202
if len(split_result) != 2:
203203
raise ValueError('Split result must be a tuple that contains split '
204-
'position and split fraction. Received: %r',
205-
split_result)
204+
'position and split fraction. Received: %r' %
205+
(split_result,))
206206

207207
if range_tracker.stop_position() != split_result[0]:
208208
raise ValueError('After a successful split, the stop position of the '
209209
'RangeTracker must be the same as the returned split '
210210
'position. Observed %r and %r which are different.',
211-
range_tracker.stop_position(), split_result[0])
211+
range_tracker.stop_position() % (split_result[0],))
212212

213213
if split_fraction < 0 or split_fraction > 1:
214214
raise ValueError('Split fraction must be within the range [0,1]',
215-
'Observed split fraction was %r.', split_result[1])
215+
'Observed split fraction was %r.' % (split_result[1],))
216216

217217
stop_position_after_split = range_tracker.stop_position()
218218
if split_result and stop_position_after_split == stop_position_before_split:
219219
raise ValueError('Stop position %r did not change after a successful '
220-
'split of source %r at fraction %r.',
221-
stop_position_before_split, source, split_fraction)
220+
'split of source %r at fraction %r.' %
221+
(stop_position_before_split, source, split_fraction))
222222

223223
if expected_outcome == ExpectedSplitOutcome.MUST_SUCCEED_AND_BE_CONSISTENT:
224224
if not split_result:
225225
raise ValueError('Expected split of source %r at fraction %r to be '
226226
'successful after reading %d elements. But '
227-
'the split failed.',
228-
source, split_fraction,
229-
num_items_to_read_before_split)
227+
'the split failed.' %
228+
(source, split_fraction, num_items_to_read_before_split))
230229
elif expected_outcome == ExpectedSplitOutcome.MUST_FAIL:
231230
if split_result:
232231
raise ValueError('Expected split of source %r at fraction %r after '
233232
'reading %d elements to fail. But splitting '
234-
'succeeded with result %r.',
235-
source, split_fraction,
236-
num_items_to_read_before_split, split_result)
233+
'succeeded with result %r.' %
234+
(source, split_fraction, num_items_to_read_before_split,
235+
split_result))
237236

238237
elif (expected_outcome !=
239238
ExpectedSplitOutcome.MUST_BE_CONSISTENT_IF_SUCCEEDS):
240-
raise ValueError('Unknown type of expected outcome: %r',
239+
raise ValueError('Unknown type of expected outcome: %r'%
241240
expected_outcome)
242241
current_items.extend([value for value in reader_iter])
243242

0 commit comments

Comments
 (0)