2018-12-07 07:10:15 +00:00
|
|
|
#! /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
|
2018-12-07 09:27:50 +00:00
|
|
|
from ...core.MO.mo import MOnumber
|
|
|
|
from ...core.MO.fraction import MOFraction
|
2018-12-07 07:10:15 +00:00
|
|
|
from decimal import Decimal as _Decimal
|
|
|
|
|
|
|
|
__all__ = ["Integer", "Decimal"]
|
|
|
|
|
|
|
|
class Integer(Token):
|
|
|
|
|
|
|
|
""" Token representing a integer """
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
def __init__(self, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOnumber):
|
|
|
|
raise TypeError
|
|
|
|
if not isinstance(mo.value, int):
|
|
|
|
raise TypeError
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
self._mathtype = 'entier'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def random(cls):
|
|
|
|
raise NotImplemented
|
|
|
|
|
|
|
|
class Decimal(Token):
|
|
|
|
|
|
|
|
""" Token representing a decimal """
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
def __init__(self, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOnumber):
|
|
|
|
raise TypeError
|
|
|
|
if not isinstance(mo.value, _Decimal):
|
|
|
|
raise TypeError
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
self._mathtype = 'décimal'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def random(cls):
|
|
|
|
raise NotImplemented
|
|
|
|
|
|
|
|
class Fraction(Token):
|
|
|
|
|
|
|
|
""" Token representing a fraction """
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
def __init__(self, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOFraction):
|
|
|
|
raise TypeError
|
|
|
|
if not isinstance(mo._numerator, MOnumber):
|
|
|
|
raise TypeError
|
|
|
|
if not isinstance(mo._denominator, MOnumber):
|
|
|
|
raise TypeError
|
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
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
|