Package biana :: Package utilities :: Module int_ascii
[hide private]
[frames] | no frames]

Source Code for Module biana.utilities.int_ascii

  1  """ 
  2      BIANA: Biologic Interactions and Network Analysis 
  3      Copyright (C) 2009  Javier Garcia-Garcia, Emre Guney, Baldo Oliva 
  4   
  5      This program is free software: you can redistribute it and/or modify 
  6      it under the terms of the GNU General Public License as published by 
  7      the Free Software Foundation, either version 3 of the License, or 
  8      (at your option) any later version. 
  9   
 10      This program is distributed in the hope that it will be useful, 
 11      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13      GNU General Public License for more details. 
 14   
 15      You should have received a copy of the GNU General Public License 
 16      along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18  """ 
 19   
 20  #PRIMARY METHODS 
 21   
 22  # For signed and unsigned values 
 23   
24 -def int_to_ascii(max_bytes,value):
25 value = int(value) 26 if value<0: 27 value = -1*value | pow(2,max_bytes*8-1) 28 temp = [ '\x00' for x in xrange(max_bytes) ] 29 del x 30 pos = max_bytes-1 31 while value>255: 32 temp[pos] = chr(value%255) 33 pos-=1 34 value/=255 35 if value==255: temp[pos] = chr(255) 36 else: temp[pos] = chr(value%255) 37 return "".join(temp)
38
39 -def ascii_to_int(ascii):
40 value = 0 41 pos = len(ascii)-1 42 for x in xrange(len(ascii)): 43 value += ord(ascii[x])*pow(255,pos) 44 pos-=1 45 if ord(ascii[0])&128: 46 return -1* value ^ pow(2,len(ascii)*8-1) 47 else: 48 return value
49 50 51 # For unsigned values 52
53 -def unsigned_int_to_ascii(max_bytes,value):
54 55 value = int(value) 56 temp = [ '\x00' for x in xrange(max_bytes) ] 57 del x 58 pos = max_bytes-1 59 while value>255: 60 temp[pos] = chr(value%255) 61 pos-=1 62 value/=255 63 if value==255: temp[pos] = chr(255) 64 else: temp[pos] = chr(value%255) 65 66 return "".join(temp)
67 68
69 -def ascii_to_unsigned_int(ascii):
70 71 value = 0 72 73 pos = len(ascii)-1 74 75 for x in xrange(len(ascii)): 76 value += ord(ascii[x])*pow(255,pos) 77 pos-=1 78 79 return value
80 81 82 83 # DERIVED METHODS 84
85 -def unsigned_float_to_ascii(bytes,dec_positions,value):
86 87 return unsigned_int_to_ascii(bytes,float(value)*pow(10,dec_positions))
88
89 -def ascii_to_unsigned_float(dec_positions, ascii):
90 91 return ascii_to_unsigned_int(ascii)/float(pow(10,dec_positions))
92
93 -def float_to_ascii(bytes,dec_positions,value):
94 95 return int_to_ascii(bytes,float(value)*pow(10,dec_positions))
96
97 -def ascii_to_float(dec_positions,ascii):
98 99 return ascii_to_int(ascii)/float(pow(10,dec_positions))
100 101 102 103 # ITERATIVE METHODS 104 105
106 -def unsigned_int_list_to_ascii(bytes,list):
107 108 return "".join( [unsigned_int_to_ascii(bytes,x) for x in list] )
109
110 -def ascii_to_unsigned_int_list(bytes,ascii):
111 112 return [ ascii_to_unsigned_int(ascii[x:x+bytes]) for x in xrange(0,len(ascii),bytes)]
113
114 -def unsigned_float_list_to_ascii(int_bytes,dec_bytes,list):
115 116 return "".join( [unsigned_float_to_ascii(int_bytes,dec_bytes,x) for x in list] )
117
118 -def ascii_to_unsigned_float_list(bytes,dec_bytes,ascii):
119 120 return [ ascii_to_unsigned_float(dec_bytes,ascii[x:x+bytes]) for x in xrange(0,len(ascii),bytes)]
121 122
123 -def int_list_to_ascii(bytes,list):
124 125 return "".join( [int_to_ascii(bytes,x) for x in list] )
126
127 -def ascii_to_int_list(bytes,ascii):
128 129 return [ ascii_to_int(ascii[x:x+bytes]) for x in xrange(0,len(ascii),bytes)]
130
131 -def float_list_to_ascii(int_bytes,dec_bytes,list):
132 133 return "".join( [float_to_ascii(int_bytes,dec_bytes,x) for x in list] )
134
135 -def ascii_to_float_list(bytes,dec_bytes,ascii):
136 137 return [ ascii_to_float(dec_bytes,ascii[x:x+bytes]) for x in xrange(0,len(ascii),bytes)]
138