Converts code combinations to either a single code integer or a tuple of integers. meta-codes (in angular brackets, e.g. and ) are ignored. Empty codes or illegal ones are returned as None.
(codes, len=len, range=range)
| 44 | r'(#.+)?') |
| 45 | |
| 46 | def parsecodes(codes, len=len, range=range): |
| 47 | |
| 48 | """ Converts code combinations to either a single code integer |
| 49 | or a tuple of integers. |
| 50 | |
| 51 | meta-codes (in angular brackets, e.g. <LR> and <RL>) are |
| 52 | ignored. |
| 53 | |
| 54 | Empty codes or illegal ones are returned as None. |
| 55 | |
| 56 | """ |
| 57 | if not codes: |
| 58 | return MISSING_CODE |
| 59 | l = codes.split('+') |
| 60 | if len(l) == 1: |
| 61 | return int(l[0],16) |
| 62 | for i in range(len(l)): |
| 63 | try: |
| 64 | l[i] = int(l[i],16) |
| 65 | except ValueError: |
| 66 | l[i] = MISSING_CODE |
| 67 | l = [x for x in l if x != MISSING_CODE] |
| 68 | if len(l) == 1: |
| 69 | return l[0] |
| 70 | else: |
| 71 | return tuple(l) |
| 72 | |
| 73 | def readmap(filename): |
| 74 |