forked from laanwj/decuda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.py
More file actions
46 lines (41 loc) · 948 Bytes
/
Util.py
File metadata and controls
46 lines (41 loc) · 948 Bytes
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
def wraplist(x):
"""Wrap x in a list if it isn't a list"""
if isinstance(x, list):
return x
else:
return [x]
def lookup(x, y):
"""
Look up an entry in an array or hash, and return an
approciate response if it runs out of bounds or
there is a hole.
"""
try:
if x[y] != None:
return x[y]
except LookupError,e:
pass
return "?%i?" % y
def numlookup(x, y):
"""
Try to resolve y as a numberic, then try to
look it up a key in dictionary x.
"""
try:
return int(y)
except ValueError:
return x[y]
def f2i(x):
"""
float32 x to four-byte representation to uint32
"""
from struct import pack,unpack
(x,) = unpack("I", pack("f", x))
return x
def i2f(x):
"""
int x to four-byte representation to float32
"""
from struct import pack,unpack
(x,) = unpack("f", pack("I", x))
return x