I'm working with an open-source library and they define a class like so:
class Provider(object):
""" Defines for each of the supported providers """
DUMMY = 0
EC2 = 1
EC2_EU = 2
RACKSPACE = 3
SLICEHOST = 4
GOGRID = 5
VPSNET = 6
LINODE = 7
VCLOUD = 8
RIMUHOSTING = 9
I need to take the properties (DUMMY, EC2, etc.) and convert them to a sorted list of tuples that would look something like this:
[(0, 'DUMMY'), (1, 'EC2'), ...]
I want to sort on the name of the property itself. I've come up with a few ways to tackle this, including the following which seems like an inefficient way to handle this:
import operator
from libcloud.types import Provider
PROVIDER_CHOICES = [(v,k) for k, v in vars(Provider).items()
if not k.startswith('__')]
PROVIDER_CHOICES = sorted(PROVIDER_CHOICES, key=operator.itemgetter(1))
It works but seems inelegant and like there may be a better way. I also see flaws in the way I'm constructing the list by doing the if not k.startswith('__')
- mainly what if the open-source lib adds methods to the Provider class?
Just looking for some opinions and other techniques that may work better for this.