Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatools/platform.py: 0%
111 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 17:39 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 17:39 +0000
1##
2## CASAtools source file (src/python/platform.py)
3##
4from __future__ import absolute_import
5import sys as _sys
6import numpy as _numpy
8pyversion = float(_sys.version_info[0]) + float(_sys.version_info[1]) / 10
10from sys import stderr as __err
12def __static_vars(**kwargs):
13 '''Initialize static function variables to for use within a function.
15 This function is used as a decorator which allows for the initialization of
16 static local variables for use within a function. It is used like:
18 @static_vars(counter=0)
19 def foo():
20 foo.counter += 1
21 print "Counter is %d" % foo.counter
23 This is used from:
24 https://stackoverflow.com/questions/279561/what-is-the-python-equivalent-of-static-variables-inside-a-function?rq=1
26 Parameters
27 ----------
28 Initialized static local variables.
29 '''
31 def decorate(func):
32 for k, v in kwargs.items():
33 setattr(func, k, v)
34 return func
36 return decorate
38numpy_string_coding = "U"
40@__static_vars(warned=False)
41def str_encode(s):
42 if str_encode.warned == False:
43 str_encode.warned = True
44 __err.write("casatools.platform.str_encode is deprecated and will be removed in a future release\n")
46 if isinstance(s,list):
47 return [ val if isinstance(val,bytearray) else bytes(str(val),_sys.getdefaultencoding()) for val in s ]
48 else:
49 return s if isinstance(s,bytearray) else bytes(str(s),_sys.getdefaultencoding())
51@__static_vars(warned=False)
52def str_decode(bs):
53 if str_decode.warned == False:
54 str_decode.warned = True
55 __err.write("casatools.platform.str_decode is deprecated and will be removed in a future release\n")
57 if isinstance(bs,list):
58 return [ val.decode(_sys.getdefaultencoding( ),"strict") if isinstance(val,bytearray) or isinstance(val,bytes) else val for val in bs ]
59 else:
60 return bs.decode(_sys.getdefaultencoding( ),"strict") if isinstance(bs,bytearray) or isinstance(bs,bytes) else bs
62@__static_vars(warned=False)
63def byte_encode(bs,enc):
64 if byte_encode.warned == False:
65 byte_encode.warned = True
66 __err.write("casatools.platform.byte_encode is deprecated and will be removed in a future release\n")
68 if isinstance(bs,bytearray):
69 return bs
70 else:
71 return bytearray(bs,enc)
73@__static_vars(warned=False)
74def byte_decode(bs,enc):
75 if byte_decode.warned == False:
76 byte_decode.warned = True
77 __err.write("casatools.platform.byte_decode is deprecated and will be removed in a future release\n")
79 if isinstance(bs,bytearray):
80 return bs.decode(enc,"strict")
81 else:
82 return str(bs)
84str2bytes = str_encode
85bytes2str = str_decode
87@__static_vars(warned=False)
88def dict_encode(d):
89 if dict_encode.warned == False:
90 dict_encode.warned = True
91 __err.write("casatools.platform.dict_encode is deprecated and will be removed in a future release\n")
93 coerce = {
94 "string": lambda x: str_encode(str(x)),
95 "int": lambda x: int(x),
96 "bool": lambda x: bool(x),
97 "float": lambda x: float(x),
98 "double": lambda x: float(x),
99 "floatArray": lambda x: [float(y) for y in x],
100 "doubleArray": lambda x: [float(y) for y in x],
101 "intArray": lambda x: [int(y) for y in x],
102 "boolArray": lambda x: [bool(y) for y in x],
103 "byteArray": lambda x: bytearray(x),
104 "bytearray": lambda x: bytearray(x),
105 "record": lambda x: dict(x),
106 "dict": lambda x: dict(x),
107 }
109 if isinstance(d,dict):
110 if len(d) == 2 and 'type' in d and 'value' in d and d['type'] in coerce:
111 return coerce[d['type']](d['value'])
112 else:
113 return dict(list(map(encode,list(d.items( )))))
114 else:
115 return encode(d)
117@__static_vars(warned=False)
118def dict_decode(d):
119 if dict_decode.warned == False:
120 dict_decode.warned = True
121 __err.write("casatools.platform.dict_decode is deprecated and will be removed in a future release\n")
123 coerce = {
124 "string": lambda x: str_decode(x),
125 "int": lambda x: int(x),
126 "bool": lambda x: bool(x),
127 "float": lambda x: float(x),
128 "double": lambda x: float(x),
129 "floatArray": lambda x: [float(y) for y in x],
130 "doubleArray": lambda x: [float(y) for y in x],
131 "intArray": lambda x: [int(y) for y in x],
132 "boolArray": lambda x: [bool(y) for y in x],
133 "byteArray": lambda x: x if isinstance(x,bytearray) else byte_encode(str(x),_sys.getdefaultencoding( )),
134 "bytearray": lambda x: x if isinstance(x,bytearray) else byte_encode(str(x),_sys.getdefaultencoding( )),
135 "record": lambda x: dict(x),
136 "dict": lambda x: dict(x),
137 }
139 if isinstance(d,dict):
140 if len(d) == 2 and 'type' in d and 'value' in d and d['type'] in coerce:
141 return coerce[d['type']](d['value'])
142 else:
143 return dict(list(map(decode,list(d.items( )))))
144 else:
145 return decode(d)
147def isiterable(v):
148 try:
149 _iter = iter(v)
150 return True
151 except TypeError:
152 return False
154@__static_vars(warned=False)
155def encode(v):
156 if encode.warned == False:
157 encode.warned = True
158 __err.write("casatools.platform.encode is deprecated and will be removed in a future release\n")
160 if isinstance(v,str):
161 return str_encode(v)
162 elif isinstance(v,dict):
163 return dict_encode(v)
164 elif isiterable(v):
165 if isinstance(v,list):
166 return [encode(x) for x in v]
167 elif isinstance(v,tuple):
168 return tuple([encode(x) for x in v])
169 elif isinstance(v,_numpy.ndarray) and v.dtype.type is _numpy.str_:
170 return _numpy.array([encode(x) for x in v])
171 else:
172 return v
173 else:
174 return v
176@__static_vars(warned=False)
177def decode(v):
178 if decode.warned == False:
179 decode.warned = True
180 __err.write("casatools.platform.decode is deprecated and will be removed in a future release\n")
182 if isinstance(v,bytearray) or isinstance(v,bytes):
183 return str_decode(v)
184 elif isinstance(v,dict):
185 return dict_decode(v)
186 elif isiterable(v):
187 if isinstance(v,list):
188 return [decode(x) for x in v]
189 elif isinstance(v,tuple):
190 return tuple([decode(x) for x in v])
191 elif isinstance(v,_numpy.ndarray) and v.dtype.type is _numpy.string_:
192 size = max([15]+[len(x) for x in v.ravel( )])
193 return _numpy.array([decode(x) for x in v],dtype='|%s%d' % (numpy_string_coding,size+1))
194 else:
195 return v
196 else:
197 return v