-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserle.py
48 lines (37 loc) · 1006 Bytes
/
parserle.py
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
def rle2bin(f):
if isinstance(f, basestring):
with open(f, 'r') as x:
t = rle2bin_core(x)
else:
t = rle2bin_core(f)
while (len(t) > 1) and (t[-1] == 0):
t = t[:-1]
return tuple(t)
def rle2bin_core(f):
binlist = [0]
xp = 0
rl = 0
for l in f:
if (len(l) == 0) or (l[0] == '#') or (l[0] == 'x'):
continue
for c in l:
if c in '0123456789':
rl = 10*rl + int(c)
if c == 'b':
rl = max(1, rl)
xp += rl
rl = 0
if c == 'o':
rl = max(1, rl)
for i in xrange(rl):
binlist[-1] += (1 << xp)
xp += 1
rl = 0
if c == '$':
rl = max(1, rl)
binlist += [0]*rl
rl = 0
xp = 0
if c == '!':
return binlist
return binlist