home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Numerical / Lib / Precision.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.4 KB  |  81 lines

  1. """ A simple hack to start thinking about a better way to handle
  2. specification of typecodes.
  3.  
  4. TODO:
  5.     The code_table should proably be cached somehow
  6.     Float/Complex needs to have precision and range specifiers
  7. """
  8.  
  9. from multiarray import zeros
  10. import string
  11.  
  12. typecodes = {'Character':'c', 'Integer':'1sil', 'UnsignedInteger':'b', 'Float':'fd', 'Complex':'FD'}
  13.  
  14. def _get_precisions(typecodes):
  15.     lst = []
  16.     for t in typecodes:
  17.         lst.append( (zeros( (1,), t ).itemsize()*8, t) )
  18.     return lst
  19.  
  20. def _fill_table(typecodes, table={}):
  21.     for key, value in typecodes.items():
  22.         table[key] = _get_precisions(value)
  23.     return table
  24.  
  25. _code_table = _fill_table(typecodes)
  26.  
  27. PrecisionError = "PrecisionError"
  28.  
  29. def _lookup(table, key, required_bits):
  30.     lst = table[key]
  31.     for bits, typecode in lst:
  32.         if bits >= required_bits:
  33.             return typecode
  34.     raise PrecisionError, key+" of "+str(required_bits)+" bits not available on this system"
  35.  
  36. UnsignedInt8 = 'b'
  37.  
  38. try: Int0 = _lookup(_code_table, 'Integer', 0)
  39. except(PrecisionError): pass
  40. try: Int8 = _lookup(_code_table, 'Integer', 8)
  41. except(PrecisionError): pass
  42. try: Int16 = _lookup(_code_table, 'Integer', 16)
  43. except(PrecisionError): pass
  44. try: Int32 = _lookup(_code_table, 'Integer', 32)
  45. except(PrecisionError): pass
  46. try: Int64 = _lookup(_code_table, 'Integer', 64)
  47. except(PrecisionError): pass
  48. try: Int128 = _lookup(_code_table, 'Integer', 128)
  49. except(PrecisionError): pass
  50. Int = 'l'
  51.  
  52. try: Float0 = _lookup(_code_table, 'Float', 0)
  53. except(PrecisionError): pass
  54. try: Float8 = _lookup(_code_table, 'Float', 8)
  55. except(PrecisionError): pass
  56. try: Float16 = _lookup(_code_table, 'Float', 16)
  57. except(PrecisionError): pass
  58. try: Float32 = _lookup(_code_table, 'Float', 32)
  59. except(PrecisionError): pass
  60. try: Float64 = _lookup(_code_table, 'Float', 64)
  61. except(PrecisionError): pass
  62. try: Float128 = _lookup(_code_table, 'Float', 128)
  63. except(PrecisionError): pass
  64. Float = 'd'
  65.  
  66. try: Complex0 = _lookup(_code_table, 'Complex', 0)
  67. except(PrecisionError): pass
  68. try: Complex8 = _lookup(_code_table, 'Complex', 16)
  69. except(PrecisionError): pass
  70. try: Complex16 = _lookup(_code_table, 'Complex', 32)
  71. except(PrecisionError): pass
  72. try: Complex32 = _lookup(_code_table, 'Complex', 64)
  73. except(PrecisionError): pass
  74. try: Complex64 = _lookup(_code_table, 'Complex', 128)
  75. except(PrecisionError): pass
  76. try: Complex128 = _lookup(_code_table, 'Complex', 256)
  77. except(PrecisionError): pass
  78. Complex = 'D'
  79.  
  80. PyObject = 'O'
  81.