Skip to content

Instantly share code, notes, and snippets.

@tdfischer
Created November 16, 2012 00:16
Show Gist options
  • Save tdfischer/4082680 to your computer and use it in GitHub Desktop.
Save tdfischer/4082680 to your computer and use it in GitHub Desktop.
generate imap message ranges from a list of ints
msgRanges = []
num = iter(sorted(numbers))
startNum = None
lastNum = None
try:
while True:
curNum = num.next()
if startNum is None:
startNum = curNum
lastNum = curNum
continue
if lastNum < curNum-1:
if startNum == curNum:
msgRanges.append("%d"%(startNum))
else:
msgRanges.append("%d:%d"%(startNum, lastNum))
startNum = None
lastNum = curNum
except StopIteration:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment