Skip to content

Commit 63ccc70

Browse files
gregparkesGregory ParkesKyle A Logue
authored
117 remove assertions (#118)
* feat: replace asserts with if statements * increment patch --------- Co-authored-by: Gregory Parkes <[email protected]> Co-authored-by: Kyle A Logue <[email protected]>
1 parent 4cdc35e commit 63ccc70

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

sigmf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# SPDX-License-Identifier: LGPL-3.0-or-later
66

77
# version of this python module
8-
__version__ = "1.2.12"
8+
__version__ = "1.2.13"
99
# matching version of the SigMF specification
1010
__specification__ = "1.2.5"
1111

sigmf/sigmffile.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def ordered_metadata(self):
6060
"""
6161
ordered_meta = OrderedDict()
6262
for top_key in self.VALID_KEYS.keys():
63-
assert top_key in self._metadata
63+
if top_key not in self._metadata:
64+
raise SigMFAccessError("key '{}' is not a VALID KEY for metadata".format(top_key))
6465
ordered_meta[top_key] = json.loads(json.dumps(self._metadata[top_key], sort_keys=True))
6566
# If there are other top-level keys, they go later
6667
# TODO: sort potential `other` top-level keys
@@ -270,7 +271,8 @@ def get_schema(self):
270271
if self.version != current_metadata_version or self.schema is None:
271272
self.version = current_metadata_version
272273
self.schema = schema.get_schema(self.version)
273-
assert isinstance(self.schema, dict)
274+
if not isinstance(self.schema, dict):
275+
raise SigMFError("SigMF schema expects a dict (key, value pairs)")
274276
return self.schema
275277

276278
def set_metadata(self, metadata):
@@ -327,7 +329,10 @@ def add_capture(self, start_index, metadata=None):
327329
If there is already capture info for this index, metadata will be merged
328330
with the existing metadata, overwriting keys if they were previously set.
329331
"""
330-
assert start_index >= self._get_start_offset()
332+
if start_index < self._get_start_offset():
333+
raise SigMFAccessError(
334+
"`start_index` {} must be >= the global start index {}".format(start_index, self._get_start_offset())
335+
)
331336
capture_list = self._metadata[self.CAPTURE_KEY]
332337
new_capture = metadata or {}
333338
new_capture[self.START_INDEX_KEY] = start_index
@@ -356,9 +361,13 @@ def get_capture_info(self, index):
356361
Returns a dictionary containing all the capture information at sample
357362
'index'.
358363
"""
359-
assert index >= self._get_start_offset()
364+
if index < self._get_start_offset():
365+
raise SigMFAccessError(
366+
"`start_index` {} must be >= the global start index {}".format(index, self._get_start_offset())
367+
)
360368
captures = self._metadata.get(self.CAPTURE_KEY, [])
361-
assert len(captures) > 0
369+
if len(captures) == 0:
370+
raise SigMFAccessError("No captures are present!")
362371
cap_info = captures[0]
363372
for capture in captures:
364373
if capture[self.START_INDEX_KEY] > index:
@@ -413,12 +422,17 @@ def add_annotation(self, start_index, length=None, metadata=None):
413422
"""
414423
Insert annotation at start_index with length (if != None).
415424
"""
416-
assert start_index >= self._get_start_offset()
425+
426+
if start_index < self._get_start_offset():
427+
raise SigMFAccessError(
428+
"`start_index` {} must be >= the global start index {}".format(start_index, self._get_start_offset())
429+
)
417430

418431
new_annot = metadata or {}
419432
new_annot[self.START_INDEX_KEY] = start_index
420433
if length is not None:
421-
assert length >= 1
434+
if length <= 0:
435+
raise SigMFAccessError("Annotation `length` must be >= 0")
422436
new_annot[self.LENGTH_INDEX_KEY] = length
423437

424438
self._metadata[self.ANNOTATION_KEY] += [new_annot]

0 commit comments

Comments
 (0)