Skip to content

Commit

Permalink
preserve order of option values (fixes #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaas Bosteels committed Jul 3, 2012
1 parent 1123dcc commit 9937c92
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions dumbo/util.py
Expand Up @@ -83,6 +83,7 @@ class Options(object):
"""
Class that represents a set of options. A key can hold
more than one value and keys are stored in lowercase.
The order of the values is preserved per key.
"""

def __init__(self, seq=None, **kwargs):
Expand All @@ -92,15 +93,24 @@ def __init__(self, seq=None, **kwargs):
Args:
- seq: a list of (key, value) pairs
"""
self._opts = defaultdict(set)
self._opts = defaultdict(list) # not sets since order is important
options = seq or []
for k, v in kwargs.iteritems():
self.add(k, v)
for k, v in options:
self.add(k, v)

def add(self, key, value):
self._opts[key].add(value)
optlist = self._opts[key]
try:
optlist.remove(value)
except ValueError:
pass # ignore "not in list" error
optlist.append(value)

def update(self, key, values):
for value in values:
self.add(key, value)

def get(self, key):
if key not in self._opts:
Expand All @@ -116,7 +126,7 @@ def __delitem__(self, key):
def __iadd__(self, opts):
if isinstance(opts, Options):
for k, vs in opts._opts.items():
self._opts[k].update(vs)
self.update(k, vs)
return self
elif isinstance(opts, (list, tuple, set)):
for k, v in opts:
Expand All @@ -142,10 +152,10 @@ def filter(self, keys):

def allopts(self):
"""Return a list with all the options in the form of (key, value)"""
return [(k, v) for k, vs in self._opts.items() for v in sorted(vs)]
return [(k, v) for k, vs in self._opts.items() for v in vs]

def to_dict(self):
return dict((k, list(sorted(vs))) for k, vs in self._opts.items())
return dict((k, list(vs)) for k, vs in self._opts.items())

def __str__(self):
ps = self.allopts()
Expand Down

0 comments on commit 9937c92

Please sign in to comment.