выглядит как ошибка / ограничение в пакете.
При передаче целого числа (отличается от 0) pint
падает:
>>> ureg(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\pint\registry.py", line 837, in parse_expression
input_string = string_preprocessor(input_string)
File "C:\Python34\lib\site-packages\pint\util.py", line 579, in string_preprocessor
input_string = input_string.replace(",", "")
AttributeError: 'int' object has no attribute 'replace'
in registry.py
def parse_expression(self, input_string, case_sensitive=True, **values):
"""Parse a mathematical expression including units and return a quantity object.
Numerical constants can be specified as keyword arguments and will take precedence
over the names defined in the registry.
"""
if not input_string:
return self.Quantity(1)
input_string = string_preprocessor(input_string) # should not be called with something else than string
происходит сбой, потому что пакет пытается выполнить строковые операции над нестроковым, где ожидается строка.Но тест равен if not input_string
, поэтому 0.0
заставляют pint
создавать класс 1
(или что-то еще), как если бы вы проходили ""
.Передача 1
позволяет перейти на следующую строку, которая вылетает.
Просто отсутствует проверка типа, что-то вроде:
if not isinstance(input_string,str):
raise Exception("a string is required")