decode the number to a string using the given statistics
(longval, nbits, probs)
| 74 | |
| 75 | |
| 76 | def decode(longval, nbits, probs): |
| 77 | """decode the number to a string using the given statistics""" |
| 78 | val = R(longval, 1L<<nbits) |
| 79 | letters = [] |
| 80 | probs_items = [(c, minval, maxval) for (c, (minval, maxval)) |
| 81 | in probs.items()] |
| 82 | |
| 83 | while 1: |
| 84 | for (c, minval, maxval) in probs_items: |
| 85 | if minval <= val < maxval: |
| 86 | break |
| 87 | else: |
| 88 | raise AssertionError("not found") |
| 89 | |
| 90 | if c == "\x00": |
| 91 | break |
| 92 | letters.append(c) |
| 93 | delta = maxval - minval |
| 94 | val = (val - minval)/delta |
| 95 | return "".join(letters) |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | # getopt? optparse? What are they? |
no test coverage detected