home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / Fixed.py < prev    next >
Text File  |  1999-06-28  |  7KB  |  232 lines

  1. #!/usr/bin/env python
  2. #############################################################################
  3. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4. # All Rights Reserved.
  5. #
  6. # The software contained on this media is the property of the DSTC Pty
  7. # Ltd.  Use of this software is strictly in accordance with the
  8. # license agreement in the accompanying LICENSE.HTML file.  If your
  9. # distribution of this software does not contain a LICENSE.HTML file
  10. # then you have no rights to use this software in any manner and
  11. # should contact DSTC at the address below to determine an appropriate
  12. # licensing arrangement.
  13. #      DSTC Pty Ltd
  14. #      Level 7, GP South
  15. #      Staff House Road
  16. #      University of Queensland
  17. #      St Lucia, 4072
  18. #      Australia
  19. #      Tel: +61 7 3365 4310
  20. #      Fax: +61 7 3365 4311
  21. #      Email: enquiries@dstc.edu.au
  22. # This software is being provided "AS IS" without warranty of any
  23. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  24. # kind arising out of or in connection with the use or performance of
  25. # this software.
  26. #
  27. # Project:      Fnorb
  28. # File:         $Source: /units/arch/src/Fnorb/orb/RCS/Fixed.py,v $
  29. # Version:      @(#)$RCSfile: Fixed.py,v $ $Revision: 1.2 $
  30. #
  31. #############################################################################
  32. """ Implementation of CORBA 'fixed' type (fixed point decimals).
  33.  
  34. ** Warning **
  35.  
  36. This is just a start - it is in nowhere near complete, and is just intended to
  37. get the IDL parsed - the Fnorb runtime does not handle the 'fixed' type. Even
  38. arithmetic for constant expressions isn't handled!!!!!!
  39.  
  40. *************
  41.  
  42. """
  43.  
  44. # Standard/built-in modules.
  45. import string
  46.  
  47. # Fnorb modules.
  48. import CORBA
  49.  
  50.  
  51. class Fixed:
  52.     """ Implementation of CORBA 'fixed' type (fixed point decimals). """
  53.  
  54.     def __init__(self, precision, decimals, value=None):
  55.     """ Constructor.
  56.     
  57.     'precision' is the number of significant digits.
  58.     'decimals'  is the number of decimals.
  59.     'value'     is an integer value containing the significant digits.
  60.  
  61.     """
  62.     self.__precision = digits
  63.     self.__decimals = scale
  64.  
  65.     if value is not None:
  66.         self.__value = value
  67.  
  68.     return
  69.  
  70.     #########################################################################
  71.     # Methods for arithmetic operations!
  72.     #########################################################################
  73.  
  74.     def __add__(self, other):
  75.     """ Addition ;^) """
  76.  
  77.     if not isinstance(other, Fixed):
  78.         raise TypeError, "Mixed mode arithmetic not allowed."
  79.  
  80.     raise 'Fixed point arithmetic not supported by Fnorb.'
  81.  
  82.     def __sub__(self, other):
  83.     """ Subtraction ;^) """
  84.  
  85.     if not isinstance(other, Fixed):
  86.         raise TypeError, "Mixed mode arithmetic not allowed."
  87.  
  88.     raise 'Fixed point arithmetic not supported by Fnorb.'
  89.  
  90.     def __mul__(self, other):
  91.     """ Multiplication ;^) """
  92.  
  93.     if not isinstance(other, Fixed):
  94.         raise TypeError, "Mixed mode arithmetic not allowed."
  95.  
  96.     raise 'Fixed point arithmetic not supported by Fnorb.'
  97.  
  98.     def __div__(self, other):
  99.     """ Division ;^) """
  100.  
  101.     if not isinstance(other, Fixed):
  102.         raise TypeError, "Mixed mode arithmetic not allowed."
  103.  
  104.     raise 'Fixed point arithmetic not supported by Fnorb.'
  105.  
  106.     def __neg__(self):
  107.     """ Unary '-' """
  108.  
  109.     raise 'Fixed point arithmetic not supported by Fnorb.'
  110.  
  111.     def __pos__(self):
  112.     """ Unary '+' """
  113.  
  114.     raise 'Fixed point arithmetic not supported by Fnorb.'
  115.  
  116.     #########################################################################
  117.     # CORBA interface.
  118.     #########################################################################
  119.  
  120.     def value(self):
  121.     """ Return the significant digits as an integer. """
  122.  
  123.     return self.__value
  124.  
  125.     def precision(self):
  126.     """ Return the number of significant digits. """
  127.  
  128.     return self.__precision
  129.  
  130.     def decimals(self):
  131.     """ Return the number of decimals. """
  132.  
  133.     return self.__decimals
  134.  
  135.     #########################################################################
  136.     # Fnorb-specific interface.
  137.     #########################################################################
  138.  
  139.     def _fnorb_from_literal(self, literal):
  140.     """ Initialise the instance from a literal. """
  141.  
  142.     # Is there a decimal point?
  143.     point = string.find(literal, '.')
  144.  
  145.     # Nope!
  146.     if point == -1:
  147.         significant = literal[:-1] # The '-1' drops the 'd' or 'D'.
  148.         fraction = ''
  149.  
  150.     # Yep!
  151.     else:
  152.         significant = literal[:point]
  153.         fraction = literal[point+1:-1] # The '-1' drops the 'd' or 'D'.
  154.  
  155.     # Concatenate all of the (possibly!) significant digits.
  156.     significant = significant + fraction
  157.  
  158.     # Count the leading and trailing zeroes.
  159.     leading = self.__count_leading_zeroes(significant)
  160.     trailing = self.__count_trailing_zeroes(significant)
  161.  
  162.     # Strip the leading and trailing zeroes and evalute the string to get
  163.     # the integer representation.
  164.     self.__value = eval(significant[leading:len(significant) - trailing])
  165.  
  166.     # Set the number of significant digits.
  167.     self.__precision = len(str(self.__value))
  168.  
  169.     # Set the number of decimals.
  170.     self.__decimals = len(fraction) - trailing
  171.  
  172.     return
  173.  
  174.     def _fnorb_typecode(self):
  175.     """ Return a typecode for the instance. """
  176.  
  177.     return CORBA.FixedTypeCode(self.__precision, self.__decimals)
  178.  
  179.     def _fnorb_marshal(self, cursor):
  180.     """ Marshal myself onto an octet stream. """
  181.  
  182.     raise "Can't marshal fixed point decimals!"
  183.  
  184.     def _fnorb_unmarshal(self, cursor):
  185.     """ Unmarshal myself from an octet stream. """
  186.  
  187.     raise "Can't unmarshal fixed point decimals!"
  188.  
  189.     #########################################################################
  190.     # Private interface.
  191.     #########################################################################
  192.  
  193.     def __count_leading_zeroes(self, s):
  194.     """ Count the leading zeroes in the string 's'. """
  195.  
  196.     count = 0
  197.     for i in range(len(s)):
  198.         if s[i] != '0':
  199.         break
  200.  
  201.         count = count + 1
  202.  
  203.     return count
  204.  
  205.     def __count_trailing_zeroes(self, s):
  206.     """ Count the trailing zeroes in the string 's'. """
  207.  
  208.     count = 0
  209.     for i in range(len(s) - 1, -1, -1):
  210.         if s[i] != '0':
  211.         break
  212.  
  213.         count = count + 1
  214.  
  215.     return count
  216.  
  217. #############################################################################
  218.  
  219. # Testing.
  220. if __name__ == '__main__':
  221.  
  222.     import new, sys
  223.  
  224.     f = new.instance(Fixed, {})
  225.     f._fnorb_from_literal(sys.argv[1])
  226.  
  227.     print 'Typecode:', f._fnorb_typecode().__dict__
  228.  
  229. #############################################################################
  230.