Throw bases for computing (and start tiding exceptions)

This commit is contained in:
2018-03-11 18:34:41 +03:00
parent b2239e0e56
commit b10492533e
8 changed files with 190 additions and 121 deletions

View File

@@ -0,0 +1,17 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Exceptions for core tools
"""
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@@ -46,25 +46,6 @@ class MOFraction(MO):
self._denominator = denominator
self.negative = negative
def __add__(self, other):
""" Overload * for MOFraction
:param other: any other MO
:yields: calculus steps and the final yielded value is a MO.
>>> f = MOFraction(1, 2)
>>> g = MOFraction(3, 2)
"""
raise NotImplemented
def __mul__(self, other):
raise NotImplemented
def __truediv__(self, other):
raise NotImplemented
# -----------------------------
# Reglages pour 'vim'

View File

@@ -71,10 +71,14 @@ class MO(object):
@classmethod
def factory(cls, value):
""" Factory to ensure that a value is a MO before using it """
if isinstance(value, MO):
return value
elif isinstance(value, str):
if isinstance(value, str):
return MOstr(value)
elif isinstance(value, int) \
or isinstance(value, Decimal) \
or isinstance(value, float):
return MOnumber(value)
elif isinstance(value, MO):
return value
return MO(value)
@@ -98,64 +102,6 @@ class MO(object):
except AttributeError:
return str(self.value)
def __add__(self, other):
""" Overload + for MOs
>>> from decimal import Decimal
>>> a = MO(4)
>>> b = MO(Decimal("1.2"))
>>> a + b
<MO 5.2>
>>> b + a
<MO 5.2>
"""
return MO.factory(self.value + other.value)
def __mul__(self, other):
""" Overload * for MOs
>>> from decimal import Decimal
>>> a = MO(4)
>>> b = MO(Decimal("1.2"))
>>> a * b
<MO 4.8>
>>> b * a
<MO 4.8>
"""
return MO.factory(self.value * other.value)
def __truediv__(self, other):
""" Overload / for MOs
>>> from decimal import Decimal
>>> a = MO(4)
>>> b = MO(Decimal("1.4"))
>>> c = b / a
>>> c
<MO 0.35>
>>> type(c.value)
<class 'decimal.Decimal'>
>>> c = a / b
>>> c
<MO 2.857142857142857142857142857>
>>> type(c.value)
<class 'decimal.Decimal'>
"""
return MO.factory(self.value / other.value)
def __neg__(self):
""" Overload + for MOs
>>> from decimal import Decimal
>>> a = MO(4)
>>> - a
<MO -4>
>>> b = MO(Decimal("1.2"))
>>> - b
<MO -1.2>
"""
return MO.factory(-self.value)
class MOnumber(MO):
@@ -251,35 +197,6 @@ class MOstr(MO):
MO.__init__(self, value)
self.is_scalar = False
def __add__(self, other):
raise NotImplemented
def __radd__(self, other):
raise NotImplemented
def __mul__(self, other):
if other.is_scalar:
raise NotImplemented
raise NotImplemented
def __rmul__(self, other):
if other.is_scalar:
raise NotImplemented
raise NotImplemented
def __truediv__(self, other):
if other.is_scalar:
raise NotImplemented
raise NotImplemented
def __rtruediv__(self, other):
if other.is_scalar:
raise NotImplemented
raise NotImplemented
def __neg__(self):
pass
# -----------------------------
# Reglages pour 'vim'