The PYTHON_NUMBER tree

The PYTHON_NUMBER class, in conjunction with PYTHON_INTEGER, PYTHON_FLOAT, and PYTHON_LONG, allow the Eiffel application to manipulate Python numeric types as native objects.

PYTHON_NUMBER

class interface PYTHON_NUMBER
   -- base class for python numeric types
creation
   make_from_new_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer without increasing reference 
      -- count of the python object.  Used because several Python C 
      -- api calls generate a new object with a reference count of 
      -- 1, and we don't want to increase that or we get too many 
      -- false references
      require
         new_python_object_pointer.is_not_null
   make_from_borrowed_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer and increases the reference 
      -- count of the given python object.  Used in cases where the 
      -- Python C api does not automatically increase the reference 
      -- count.  Terminology ("borrowed reference" vs "new reference") 
      -- is taken from the Python C Api documentation
      require
         new_python_object_pointer.is_not_null
   make_from_python_object (object: PYTHON_OBJECT)
      -- uses the existing python_object_pointer of python_object 
      require
         object /= Void
feature(s) from PYTHON_NUMBER
   infix "+" (other: like Current): like Current
      -- sum with other
      require
         other /= Void
   infix "-" (other: like Current): like Current
      -- subtract
      require
         other /= Void
   infix "*" (other: like Current): like Current
      -- multiply
      require
         other /= Void
   infix "/" (other: like Current): like Current
      -- divide
      require
         other /= Void;
         divisible(other)
   remainder (other: like Current): like Current
      -- remainder
   infix "\\" (other: like Current): like Current
      -- remainder
   divmod (other: like Current): PYTHON_TUPLE
      -- the tuple pair (x/y, x \\ y )
   infix "^" (other: INTEGER): like Current
      --  `Current' raised to `exp'-th power.
      require
         other >= 0
   power (other: like Current): like Current
   prefix "+": like Current
      -- unary positive
   prefix "-": like Current
      -- unary negative
   abs: like Current
      -- absolute value
   invert: like Current
      -- bitwise inversion (negation)
   left_shift (amount: INTEGER): like Current
      -- bitwise left-shift
   right_shift (amount: INTEGER): like Current
      -- bitwise right-shift
   bitwise_and (other: like Current): like Current
      -- bitwise and
   bitwise_xor (other: like Current): like Current
      -- bitwise xor
   bitwise_or (other: like Current): like Current
      -- bitwise or
   to_python_integer: PYTHON_INTEGER
      -- converts to integer
   to_integer: INTEGER
   as_integer: INTEGER
   to_python_long: PYTHON_LONG
      -- converts to long
   to_python_float: PYTHON_FLOAT
      -- converts to float
   to_double: DOUBLE
   as_double: DOUBLE
invariant
   reference_count >= 0;
   valid_reference: not is_none implies is_number;
end of PYTHON_NUMBER

PYTHON_INTEGER

class interface PYTHON_INTEGER
creation
   make_from_borrowed_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer and increases the reference 
      -- count of the given python object.  Used in cases where the 
      -- Python C api does not automatically increase the reference 
      -- count.  Terminology ("borrowed reference" vs "new reference") 
      -- is taken from the Python C Api documentation
      require
         new_python_object_pointer.is_not_null
   make_from_new_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer without increasing reference 
      -- count of the python object.  Used because several Python C 
      -- api calls generate a new object with a reference count of 
      -- 1, and we don't want to increase that or we get too many 
      -- false references
      require
         new_python_object_pointer.is_not_null
   make_from_python_object (object: PYTHON_OBJECT)
      -- uses the existing python_object_pointer of python_object 
      require
         object /= Void
   from_integer (value: INTEGER)
      -- creates with initial integer value
feature(s) from PYTHON_INTEGER
   from_integer (value: INTEGER)
      -- creates with initial integer value
   to_integer: INTEGER
      -- converts to eiffel-style integer
   as_integer: INTEGER
      -- converts to eiffel-style integer
   divisible (other: PYTHON_INTEGER): BOOLEAN
      --  May `Current' be divided by `other' ?
      require
         other /= Void
invariant
   reference_count >= 0;
   valid_reference: not is_none implies is_number;
   valid_reference: not is_none implies is_integer;
end of PYTHON_INTEGER

PYTHON_FLOAT

class interface PYTHON_FLOAT
creation
   make_from_borrowed_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer and increases the reference 
      -- count of the given python object.  Used in cases where the 
      -- Python C api does not automatically increase the reference 
      -- count.  Terminology ("borrowed reference" vs "new reference") 
      -- is taken from the Python C Api documentation
      require
         new_python_object_pointer.is_not_null
   make_from_new_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer without increasing reference 
      -- count of the python object.  Used because several Python C 
      -- api calls generate a new object with a reference count of 
      -- 1, and we don't want to increase that or we get too many 
      -- false references
      require
         new_python_object_pointer.is_not_null
   make_from_python_object (object: PYTHON_OBJECT)
      -- uses the existing python_object_pointer of python_object 
      require
         object /= Void
   from_double (value: DOUBLE)
      -- from a double-precision floating point number
      ensure
         reference_count = 1
feature(s) from PYTHON_FLOAT
   from_double (value: DOUBLE)
      -- from a double-precision floating point number
      ensure
         reference_count = 1
   to_double: DOUBLE
      -- convert to eiffel-style double
   as_double: DOUBLE
      -- convert to eiffel-style double
   divisible (other: PYTHON_FLOAT): BOOLEAN
      --  May `Current' be divided by `other' ?
      require
         other /= Void
invariant
   reference_count >= 0;
   valid_reference: not is_none implies is_number;
   valid_reference: not is_none implies is_float;
end of PYTHON_FLOAT

PYTHON_LONG

class interface PYTHON_LONG
creation
   make_from_borrowed_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer and increases the reference 
      -- count of the given python object.  Used in cases where the 
      -- Python C api does not automatically increase the reference 
      -- count.  Terminology ("borrowed reference" vs "new reference") 
      -- is taken from the Python C Api documentation
      require
         new_python_object_pointer.is_not_null
   make_from_new_reference (new_python_object_pointer: POINTER)
      -- uses the given object pointer without increasing reference 
      -- count of the python object.  Used because several Python C 
      -- api calls generate a new object with a reference count of 
      -- 1, and we don't want to increase that or we get too many 
      -- false references
      require
         new_python_object_pointer.is_not_null
   make_from_python_object (object: PYTHON_OBJECT)
      -- uses the existing python_object_pointer of python_object 
      require
         object /= Void
   from_integer (value: INTEGER)
      -- creates with initial integer value
   from_double (value: DOUBLE)
      -- from a double-precision floating point number
feature(s) from PYTHON_LONG
   from_integer (value: INTEGER)
      -- creates with initial integer value
   to_integer: INTEGER
      -- converts to eiffel-style integer
   as_integer: INTEGER
      -- converts to eiffel-style integer
   from_double (value: DOUBLE)
      -- from a double-precision floating point number
   to_double: DOUBLE
      -- convert to eiffel-style double
   as_double: DOUBLE
      -- convert to eiffel-style double
   divisible (other: PYTHON_LONG): BOOLEAN
      --  May `Current' be divided by `other' ?
      require
         other /= Void
invariant
   reference_count >= 0;
   valid_reference: not is_none implies is_number;
   valid_reference: not is_none implies is_long;
end of PYTHON_LONG