Ad
Fundamentals

Represents an ordinal numeral (i.e. 1st, 24th, etc.)

Code
Diff
  • from __future__ import annotations
    
    
    class OrdinalNumeral:
        def __init__(self, n: int):
            """Represents an ordinal numeral (i.e. 1st, 24th, etc.)
    
            :param int n: The integer to represent
            :raises ValueError: Raised if n is below 0
            :raises TypeError: Raised if a non-integer/OrdinalNumber value is added to the number
            """
            if n < 0:
                raise ValueError("n must be positive or 0")
            self._n = n
    
        @property
        def n(self):
            return self._n
    
        @n.setter
        def n(self, n: int):
            if n < 0:
                raise ValueError("n must be positive or 0")
            self._n = n
    
        @property
        def numeral(self):
            if 11 <= (self.n % 100) <= 13:
                return str(self.n) + "th"
            return str(self.n) + ("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
    
        def __repr__(self):
            """Representation that can be used to recreate the object"""
            return f"OrdinalNumeral(n={self.n})"
    
        def __str__(self):
            """The 'nice' string version of the class"""
            return self.numeral
    
        def __int__(self):
            return self.n
    
        def __add__(self, other: int | OrdinalNumeral):
            """Can implement arithmetic operations if desired"""
            if isinstance(other, OrdinalNumeral):
                return OrdinalNumeral(self.n + other.n)
            elif isinstance(other, int):
                return OrdinalNumeral(self.n + other)
            else:
                raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
    
    • from __future__ import annotations
    • class OrdinalNumeral:
    • def __init__(self, n: int):
    • if n < 0: raise ValueError("n must be positive or 0")
    • self.n = n
    • self.numeral = self._numeral()
    • """Represents an ordinal numeral (i.e. 1st, 24th, etc.)
    • :param int n: The integer to represent
    • :raises ValueError: Raised if n is below 0
    • :raises TypeError: Raised if a non-integer/OrdinalNumber value is added to the number
    • """
    • if n < 0:
    • raise ValueError("n must be positive or 0")
    • self._n = n
    • @property
    • def n(self):
    • return self._n
    • @n.setter
    • def n(self, n: int):
    • if n < 0:
    • raise ValueError("n must be positive or 0")
    • self._n = n
    • @property
    • def numeral(self):
    • if 11 <= (self.n % 100) <= 13:
    • return str(self.n) + "th"
    • return str(self.n) + ("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
    • def __repr__(self):
    • """representation that can be used to recreate the object"""
    • return f"OrdinalNumeral(self.n)"
    • """Representation that can be used to recreate the object"""
    • return f"OrdinalNumeral(n={self.n})"
    • def __str__(self):
    • """the 'nice' string version of the class"""
    • """The 'nice' string version of the class"""
    • return self.numeral
    • def __int__(self):
    • return self.n
    • def __add__(self, other):
    • """can implement arithmetic operations if desired"""
    • if (isinstance(other, OrdinalNumeral)): return OrdinalNumeral(self.n + other.n)
    • elif (isinstance(other, int)): return OrdinalNumeral(self.n + other)
    • else: raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")
    • def _numeral(self):
    • """define the ordinal numeral string"""
    • if 11 <= (self.n % 100) <= 13: return str(self.n)+"th"
    • return str(self.n)+("th", "st", "nd", "rd", "th")[min(4, self.n % 10)]
    • def __add__(self, other: int | OrdinalNumeral):
    • """Can implement arithmetic operations if desired"""
    • if isinstance(other, OrdinalNumeral):
    • return OrdinalNumeral(self.n + other.n)
    • elif isinstance(other, int):
    • return OrdinalNumeral(self.n + other)
    • else:
    • raise TypeError(f"Unable to add {type(other)} with OrdinalNumeral")