Add doctest in MO.factory and an error message
This commit is contained in:
parent
6763ae6074
commit
3aa7c92f54
@ -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__}>"
|
||||||
|
Loading…
Reference in New Issue
Block a user