Mapytex/mapytex/calculus/API/tokens/number.py

86 lines
2.0 KiB
Python

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Tokens representing interger and decimal
"""
from .token import Token
from ...core.MO.mo import MOnumber
from ...core.MO.fraction import MOFraction
from decimal import Decimal as _Decimal
__all__ = ["Integer", "Decimal"]
class Integer(Token):
""" Token representing a integer """
def __init__(self, mo, name="", ancestor=None):
if not isinstance(mo, MOnumber):
raise TypeError
if not isinstance(mo.value, int):
raise TypeError
Token.__init__(self, mo, name, ancestor)
self._mathtype = 'entier'
@classmethod
def random(cls):
raise NotImplemented
class Decimal(Token):
""" Token representing a decimal """
def __init__(self, mo, name="", ancestor=None):
if not isinstance(mo, MOnumber):
raise TypeError
if not isinstance(mo.value, _Decimal):
raise TypeError
Token.__init__(self, mo, name, ancestor)
self._mathtype = 'décimal'
@classmethod
def random(cls):
raise NotImplemented
class Fraction(Token):
""" Token representing a fraction """
def __init__(self, mo, name="", ancestor=None):
if not isinstance(mo, MOFraction):
raise TypeError
if not isinstance(mo._numerator, MOnumber):
raise TypeError
if not isinstance(mo._denominator, MOnumber):
raise TypeError
Token.__init__(self, mo, name, ancestor)
self._mathtype = 'fraction'
@classmethod
def random(cls):
raise NotImplemented
@property
def numerator(self):
return self._mo.numerator
@property
def denominator(self):
return self._mo.denominator
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del