Make it possible to pass a start= argument to enum()

This commit is contained in:
Florian Bruhin 2014-06-04 07:02:04 +02:00
parent 93c6d8ea2f
commit 83d8bc47c0
2 changed files with 10 additions and 3 deletions

View File

@ -56,6 +56,12 @@ class EnumTests(TestCase):
with self.assertRaises(KeyError):
_ = self.enum['two']
def test_start(self):
"""Test the start= argument."""
e = enum('three', 'four', start=3)
self.assertEqual(e.three, 3)
self.assertEqual(e.four, 4)
if __name__ == '__main__':
unittest.main()

View File

@ -32,7 +32,7 @@ from qutebrowser.utils.log import misc as logger
_UNSET = object()
def enum(*items, **named):
def enum(*items, start=0):
"""Factory for simple enumerations.
We really don't need more complex things here, so we don't use python3.4's
@ -42,9 +42,10 @@ def enum(*items, **named):
Args:
*items: Items to be sequentally enumerated.
**named: Items to have a given position/number.
start: The number to use for the first value.
"""
enums = dict(zip(items, range(len(items))), **named)
numbers = range(start, len(items) + start)
enums = dict(zip(items, numbers))
return EnumBase('Enum', (), enums)