@@ -183,13 +183,15 @@ def __getitem__(self, item): # pragma: no cover
183
183
if variable in key_string :
184
184
found_variable = True
185
185
break
186
- assert found_variable , "Input string {} is not a key of the data dictionary." .format (variable )
186
+ if not found_variable :
187
+ raise KeyError (f"Input string { variable } is not a key of the data dictionary." )
187
188
data_out ._data [variable ] = self ._data [key_string ]
188
189
data_out ._header .append (variable )
189
190
return data_out
190
191
191
192
def __add__ (self , other ): # pragma: no cover
192
- assert self .number_of_columns == other .number_of_columns , "Inconsistent number of columns"
193
+ if self .number_of_columns != other .number_of_columns :
194
+ raise ValueError ("Number of columns is inconsistent." )
193
195
# Create a new object to return, avoiding changing the original inputs
194
196
new_dataset = CSVDataset ()
195
197
# Add empty columns to new_dataset
@@ -224,7 +226,8 @@ def __iadd__(self, other): # pragma: no cover
224
226
for column in other .data :
225
227
self ._data [column ] = []
226
228
227
- assert self .number_of_columns == other .number_of_columns , "Inconsistent number of columns"
229
+ if self .number_of_columns != other .number_of_columns :
230
+ raise ValueError ("Number of columns is inconsistent." )
228
231
229
232
# Append the data from 'other'
230
233
for column , row_data in other .data .items ():
@@ -1348,9 +1351,10 @@ def __init__(
1348
1351
self ._value = self ._calculated_value
1349
1352
# If units have been specified, check for a conflict and otherwise use the specified unit system
1350
1353
if units :
1351
- assert not self ._units , "The unit specification {} is inconsistent with the identified units {}." .format (
1352
- specified_units , self ._units
1353
- )
1354
+ if self ._units and self ._units != specified_units :
1355
+ raise RuntimeError (
1356
+ f"The unit specification { specified_units } is inconsistent with the identified units { self ._units } ."
1357
+ )
1354
1358
self ._units = specified_units
1355
1359
1356
1360
if not si_value and is_number (self ._value ):
@@ -1737,9 +1741,10 @@ def rescale_to(self, units): # pragma: no cover
1737
1741
1738
1742
"""
1739
1743
new_unit_system = unit_system (units )
1740
- assert new_unit_system == self .unit_system , (
1741
- "New unit system {0} is inconsistent with the current unit system {1}."
1742
- )
1744
+ if new_unit_system != self .unit_system :
1745
+ raise ValueError (
1746
+ f"New unit system { new_unit_system } is inconsistent with the current unit system { self .unit_system } ."
1747
+ )
1743
1748
self ._units = units
1744
1749
return self
1745
1750
@@ -1810,7 +1815,8 @@ def __mul__(self, other): # pragma: no cover
1810
1815
>>> assert result_3.unit_system == "Power"
1811
1816
1812
1817
"""
1813
- assert is_number (other ) or isinstance (other , Variable ), "Multiplier must be a scalar quantity or a variable."
1818
+ if not is_number (other ) and not isinstance (other , Variable ):
1819
+ raise ValueError ("Multiplier must be a scalar quantity or a variable." )
1814
1820
if is_number (other ):
1815
1821
result_value = self .numeric_value * other
1816
1822
result_units = self .units
@@ -1854,10 +1860,10 @@ def __add__(self, other): # pragma: no cover
1854
1860
>>> assert result.unit_system == "Current"
1855
1861
1856
1862
"""
1857
- assert isinstance (other , Variable ), "You can only add a variable with another variable."
1858
- assert self . unit_system == other . unit_system , (
1859
- "Only ``Variable`` objects with the same unit system can be added."
1860
- )
1863
+ if not isinstance (other , Variable ):
1864
+ raise ValueError ( "You can only add a variable with another variable." )
1865
+ if self . unit_system != other . unit_system :
1866
+ raise ValueError ( "Only Variable objects with the same unit system can be added." )
1861
1867
result_value = self .value + other .value
1862
1868
result_units = SI_UNITS [self .unit_system ]
1863
1869
# If the units of the two operands are different, return SI-Units
@@ -1895,10 +1901,10 @@ def __sub__(self, other): # pragma: no cover
1895
1901
>>> assert result_2.unit_system == "Current"
1896
1902
1897
1903
"""
1898
- assert isinstance (other , Variable ), "You can only subtract a variable from another variable."
1899
- assert self . unit_system == other . unit_system , (
1900
- "Only ``Variable`` objects with the same unit system can be subtracted."
1901
- )
1904
+ if not isinstance (other , Variable ):
1905
+ raise ValueError ( "You can only subtract a variable from another variable." )
1906
+ if self . unit_system != other . unit_system :
1907
+ raise ValueError ( "Only Variable objects with the same unit system can be subtracted." )
1902
1908
result_value = self .value - other .value
1903
1909
result_units = SI_UNITS [self .unit_system ]
1904
1910
# If the units of the two operands are different, return SI-Units
@@ -1940,7 +1946,8 @@ def __truediv__(self, other): # pragma: no cover
1940
1946
>>> assert result_1.unit_system == "Current"
1941
1947
1942
1948
"""
1943
- assert is_number (other ) or isinstance (other , Variable ), "Divisor must be a scalar quantity or a variable."
1949
+ if not is_number (other ) and not isinstance (other , Variable ):
1950
+ raise ValueError ("Divisor must be a scalar quantity or a variable." )
1944
1951
if is_number (other ):
1945
1952
result_value = self .numeric_value / other
1946
1953
result_units = self .units
0 commit comments