From 3aa7c92f543c2864638f009acb6825a713035993 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 14 Mar 2018 11:42:10 +0300 Subject: [PATCH] Add doctest in MO.factory and an error message --- mapytex/calculus/core/MO/mo.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mapytex/calculus/core/MO/mo.py b/mapytex/calculus/core/MO/mo.py index b3637f3..b15242f 100644 --- a/mapytex/calculus/core/MO/mo.py +++ b/mapytex/calculus/core/MO/mo.py @@ -67,7 +67,22 @@ class MO(object): @classmethod 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") + + >>> MO.factory(2) + + >>> MO.factory(2.3) + + >>> MO.factory(Decimal("2.3")) + + >>> x = MO.factory("x") + >>> MO.factory(x) + + """ if isinstance(value, str): return MOstr(value) elif isinstance(value, int) \ @@ -77,7 +92,8 @@ class MO(object): elif isinstance(value, MO): 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): return f"<{self.__class__.__name__} {self.__txt__}>"