/usr/share/pyshared/pyvtk/common.py is in python-pyvtk 0.4.74-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #!/usr/bin/env python
"""
Common functions/methods.
"""
"""
Copyright 2001 Pearu Peterson all rights reserved,
Pearu Peterson <pearu@ioc.ee>
Permission to use, modify, and distribute this software is given under the
terms of the LGPL. See http://www.fsf.org
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
$Revision: 1.13 $
$Date: 2007-02-22 12:15:46 $
Pearu Peterson
"""
import types
import sys
import struct
def is_sequence(obj):
"""Check if obj is sequence."""
try:
len(obj)
return 1
except TypeError:
return 0
def is_sequence2(obj):
"""Check if obj is sequence of sequences."""
return is_sequence(obj) and len(obj) and is_sequence(obj[0])
def is_sequence3(obj):
"""Check if obj is sequence of sequences of sequences."""
return is_sequence(obj) and len(obj) and is_sequence2(obj[0])
def is_number(obj):
"""Check if obj is number."""
return isinstance(obj, (int, float))
def is_int(obj):
"""Check if obj is integer."""
return isinstance(obj, int)
def is_float(obj):
"""Check if obj is float."""
return isinstance(obj, float)
def is_string(obj):
"""Check if obj is string."""
return isinstance(obj, str)
def is_int255(obj):
if is_sequence(obj):
for v in obj:
r = is_int255(v)
if not r: return 0
return 1
return 0<=obj<256
def is_float01(obj):
if is_sequence(obj):
for v in obj:
r = is_float01(v)
if not r: return 0
return 1
return 0<=obj<=1
def is_datasetattr(obj):
return type(obj) is types.InstanceType and isinstance(obj,DataSetAttr.DataSetAttr)
def is_dataset(obj):
return type(obj) is types.InstanceType and isinstance(obj,DataSet.DataSet)
def is_pointdata(obj):
return type(obj) is types.InstanceType and isinstance(obj,Data.PointData)
def is_celldata(obj):
return type(obj) is types.InstanceType and isinstance(obj,Data.CellData)
def _getline(f):
l = ' '
while l:
l = f.readline()
if l.strip():
return l.strip()
return None
class Common:
"""Abstract class. Defines output, checker, and getter functions."""
struct_fmt_map = {'char':'c',
'long':'l','double':'d',
'int':'i','float':'f',
'unsigned char':'B'}
default_int = 'int'
default_float = 'float'
def _get_trace(self,m):
try:
frame = sys._getframe().f_back
except AttributeError: # Python 2.0 does not have sys._getframe
frame = None
n = ''
while frame:
i = frame.f_code.co_name
n = '%s.%s'%(i,n)
if i=='__init__':
break
frame = frame.f_back
print >>sys.stderr,'%s.%s:\n\t%s'%(self.__class__.__name__,n[:-1],m)
def warning(self,m=''):
self._get_trace(m)
def skipping(self,m=''):
self._get_trace(m)
def error(self,m=''):
self._get_trace(m)
def message(self,m=''):
self._get_trace(m)
def __str__(self):
return self.to_string()
def get_datatype(self,obj):
typecode = None
if hasattr(obj,'dtype'): # obj is numpy array
typecode = obj.dtype.char
elif hasattr(obj,'typecode'): # obj is Numeric array
typecode = obj.typecode()
if typecode is not None:
r = {'b':'unsigned_char', #'bit'??
'f':'float',
'd':'double',
'i':'int',
'l':'long',
'1':'char',
's':'short',
'w':'unsigned_short',
'u':'unsigned_int'
#'?':'unsigned_long'
}.get(typecode)
if r is not None:
return r
if is_int(obj): return self.default_int
if is_float(obj): return self.default_float
if not is_sequence(obj):
raise ValueError,'expected int|float|non-empty sequence but got %s'\
%(type(obj))
if not len(obj):
self.warning('no data, no datatype, using int')
r = 'int'
for o in obj:
r = self.get_datatype(o)
if r==self.default_float:
break
return r
def get_seq(self,obj,default=None):
"""Return sequence."""
if is_sequence(obj):
return obj
if is_number(obj): return [obj]
if obj is None and default is not None:
self.warning('using default value (%s)'%(default))
return self.get_seq(default)
raise ValueError,'expected sequence|number but got %s'%(type(obj))
def get_seq_seq(self,obj,default=None):
"""Return sequence of sequences."""
if is_sequence2(obj):
return [self.get_seq(o,default) for o in obj]
else:
return [self.get_seq(obj,default)]
def get_n_seq_seq(self,obj,default):
seq = self.get_seq_seq(obj,default)
if is_sequence(default):
n = len(default)
else:
n = max(map(len,seq))
default = [default]*n
ret = []
flag = 0
for v in seq:
if len(v)!=n:
ret.append(list(v)+default[len(v):])
flag = 1
else:
ret.append(list(v))
if flag:
self.warning('Some items were filled with default value (%s) to obtain size=%s'%(default[0],n))
return ret
def get_3_tuple(self,obj,default=None):
"""Return 3-tuple from
number -> (obj,default[1],default[2])
0-sequence|None -> default
1-sequence -> (obj[0],default[1],default[2])
2-sequence -> (obj[0],obj[1],default[2])
(3 or more)-sequence -> (obj[0],obj[1],obj[2])
"""
if not (default is not None \
and type(default) is types.TupleType \
and len(default)==3):
raise ValueError,'argument default must be 3-tuple|None but got %s'%(default)
if is_sequence(obj):
n = len(obj)
if n>3:
self.warning('expected 3-sequence but got %s-%s'%(n,type(obj)))
if n>=3:
return tuple(obj)
self.warning('filling with default value (%s) to obtain size=3'%(default[0]))
if default is not None:
if n==0:
return default
elif n==1:
return (obj[0],default[1],default[2])
elif n==2:
return (obj[0],obj[1],default[2])
elif is_number(obj) and default is not None:
self.warning('filling with default value (%s) to obtain size=3'%(default[0]))
return (obj,default[1],default[2])
elif obj is None and default is not None:
self.warning('filling with default value (%s) to obtain size=3'%(default[0]))
return default
raise ValueError,'failed to construct 3-tuple from %s-%s'%(n,type(obj))
def get_3_tuple_list(self,obj,default=None):
"""Return list of 3-tuples from
sequence of a sequence,
sequence - it is mapped to sequence of 3-sequences if possible
number
"""
if is_sequence2(obj):
return [self.get_3_tuple(o,default) for o in obj]
elif is_sequence(obj):
return [self.get_3_tuple(obj[i:i+3],default) for i in range(0,len(obj),3)]
else:
return [self.get_3_tuple(obj,default)]
def get_3_3_tuple(self,obj,default=None):
"""Return tuple of 3-tuples
"""
if is_sequence2(obj):
ret = []
for i in range(3):
if i<len(obj):
ret.append(self.get_3_tuple(obj[i],default))
else:
ret.append(self.get_3_tuple(default,default))
return tuple(ret)
if is_sequence(obj):
if len(obj)>9:
self.warning('ignoring elements obj[i], i>=9')
r = obj[:9]
r = [self.get_3_tuple(r[j:j+3],default) for j in range(0,len(r),3)]
if len(r)<3:
self.warning('filling with default value (%s) to obtain size=3'%(default[0]))
while len(r)<3:
r.append(self.get_3_tuple(default,default))
return tuple(r)
self.warning('filling with default value (%s) to obtain size=3'%(default[0]))
r1 = self.get_3_tuple(obj,default)
r2 = self.get_3_tuple(default,default)
r3 = self.get_3_tuple(default,default)
return (r1,r2,r3)
def get_3_3_tuple_list(self,obj,default=None):
"""Return list of 3x3-tuples.
"""
if is_sequence3(obj):
return [self.get_3_3_tuple(o,default) for o in obj]
return [self.get_3_3_tuple(obj,default)]
def _get_nof_objs(self,seq):
if is_sequence2(seq):
return reduce(lambda x,y:x+y,map(self._get_nof_objs,seq),0)
#return reduce(lambda x,y:x+y,[self._get_nof_objs(s) for s in seq],0)
return len(seq)
def seq_to_string(self,seq,format,datatype):
assert is_sequence(seq),'expected sequence but got %s'%(type(seq))
if format == 'ascii':
if is_sequence2(seq):
sep = '\n'
if is_sequence3(seq):
sep = '\n\n'
return sep.join([self.seq_to_string(v,format,datatype) for v in seq])
else:
return ' '.join(map(str,seq))
elif format == 'binary':
if is_sequence2(seq):
r = ''.join([self.seq_to_string(v,format,datatype) for v in seq])
return r
else:
try:
fmt = self.struct_fmt_map[datatype]
except KeyError:
fmt = None
if fmt:
r = struct.pack('!'+fmt*len(seq),*seq)
return r
raise NotImplementedError,'format=%s, datatype=%s'%(format,datatype)
def float01_to_int255(self,seq):
assert is_float01(seq)
if is_sequence(seq):
return map(self.float01_to_int255,seq)
#return [self.float01_to_int255(l) for l in seq]
else:
return int(seq*255)
def int255_to_float01(self,seq):
assert is_int255(seq)
if is_sequence(seq):
return map(self.int255_to_float01,seq)
#return [self.int255_to_float01(l) for l in seq]
else:
return round(seq/255.0,6)
import Data
import DataSet
import DataSetAttr
|