Add doctest in MO.factory and an error message

This commit is contained in:
Bertrand Benjamin 2018-03-14 11:42:10 +03:00
parent 6763ae6074
commit 3aa7c92f54

View File

@ -67,7 +67,22 @@ class MO(object):
@classmethod @classmethod
def factory(cls, value): def factory(cls, value):
""" Factory to ensure that a value is a MO before using it """ """ Factory to ensure that a value is a MO before using it
Idempotent??
>>> MO.factory("x")
<MOstr x>
>>> MO.factory(2)
<MOnumber 2>
>>> MO.factory(2.3)
<MOnumber 2.29999999999999982236431605997495353221893310546875>
>>> MO.factory(Decimal("2.3"))
<MOnumber 2.3>
>>> x = MO.factory("x")
>>> MO.factory(x)
<MOstr x>
"""
if isinstance(value, str): if isinstance(value, str):
return MOstr(value) return MOstr(value)
elif isinstance(value, int) \ elif isinstance(value, int) \
@ -77,7 +92,8 @@ class MO(object):
elif isinstance(value, MO): elif isinstance(value, MO):
return value return value
return MO(value) raise MOError("Can't convert it into a MO."
f"Need str, int, Decimal, float or MO, got {value}")
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__} {self.__txt__}>" return f"<{self.__class__.__name__} {self.__txt__}>"