blob: b143b6856e0289d241aea6f430ce808187d0ae19 (
plain)
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
|
# (c) Copyright 2007 The Board of Trustees of the University of Illinois.
import struct
def uint16(f):
"""Read a 16-bit unsigned integer from f."""
[c, c2] = f.read(2)
return ord(c) + (ord(c2) << 8)
def uint32(f):
"""Read a 32-bit unsigned integer from f."""
[c, c2, c3, c4] = f.read(4)
return ord(c) + (ord(c2) << 8) + (ord(c3) << 16) + (ord(c4) << 24)
def float(f):
"""Read a floating-point number from f."""
s = f.read(4)
(n,) = struct.unpack("<f", s)
return n
def many(reader, count):
"""Create a reader function that reads a fixed-size sequence of
values from f."""
return lambda f: [reader(f) for n in range(count)]
def many_uint16(count):
"""Create a reader function that reads a fixed-size sequence of
16-bit unsigned integers from f."""
def read(f):
s = f.read(2*count)
ns = struct.unpack("<%dH" % count, s)
return ns
return read
def many_float(count):
"""Create a reader function that reads a fixed-size sequence of
floats from f."""
def read(f):
s = f.read(4*count)
floats = struct.unpack("<%df" % count, s)
return floats
return read
def eof(f):
"""Read the end of file 'f'. A ValueError is raised if anything
other than EOF is read from the file."""
if f.readline() == "": return None
else: raise ValueError, "Expecting end of file"
|