wesnoth/utils/codelist
Thibault Févry 384981b974 Use isinstance instead of type() checks.
Use repr() instead of deprecated . Also make it fully respectfull of Pep8.
2011-03-12 01:15:58 +00:00

44 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
# codelist
# given list of integers, one per line, outputs a minimal list of ranges
# describing the list
# this is useful for codepoints that are in a font, based on list of codes
# output format is suitable for codepoints="" in Wesnoth fonts.cfg
import sys
def rangeify(lst):
"""
Turn ranges of adjacent ints in a list into [start, end] list elements.
"""
lst.sort()
lst.append(None)
while True:
for i in range(len(lst) - 1):
if isinstance(lst[i], int) and lst[i + 1] == lst[i] + 1:
lst = lst[:i] + [[lst[i], lst[i + 1]]] + lst[i + 2:]
break
elif isinstance(lst[i], list) and lst[i + 1] == lst[i][-1] + 1:
lst[i][1] = lst[i + 1]
lst = lst[:i + 1] + lst[i + 2:]
break
else:
break
lst.pop()
return lst
def printbyrange(lst):
out = ""
for elt in lst:
if isinstance(elt, int):
out += repr(elt) + ","
else:
out += "%d-%d," % tuple(elt)
return out[:-1]
codepoints = map(lambda x: int(x.strip()), sys.stdin.readlines())
print printbyrange(rangeify(codepoints))