Skip to content

Commit 60a988e

Browse files
committed
Clean up enum usage
1 parent e3e6e85 commit 60a988e

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

api/jobs.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from __future__ import absolute_import
77

88
import bson
9-
from enum import Enum, unique
109
import pymongo
1110
import datetime
1211
from collections import namedtuple
1312

1413
from . import base
1514
from . import config
15+
from . import util
1616

1717
log = config.log
1818

@@ -38,18 +38,12 @@
3838
def valid_transition(from_state, to_state):
3939
return (from_state + ' --> ' + to_state) in JOB_TRANSITIONS or from_state == to_state
4040

41-
# Represet the general categories of gear function
42-
@unique
43-
class Category(Enum):
44-
classifier = 'classifier' # discover metadata
45-
converter = 'converter' # translate between formats
46-
qa = 'qa' # quality assurance
47-
analytical = 'analytical' # general purpose
48-
49-
# Enum strings are prefixed by their class: "Category.classifier".
50-
# This overrides that behaviour and removes the prefix.
51-
def __str__(self):
52-
return str(self.value)
41+
Category = util.Enum('Category', {
42+
'classifier': 'classifier', # discover metadata
43+
'converter': 'converter', # translate between formats
44+
'qa': 'qa', # quality assurance
45+
'analytical': 'analytical', # general purpose
46+
})
5347

5448
Gear = namedtuple('gear', ['name', 'category', 'input'])
5549

api/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import mimetypes
66
import bson.objectid
77
import tempdir as tempfile
8+
import enum as baseEnum
89

910
MIMETYPES = [
1011
('.bvec', 'text', 'bvec'),
@@ -110,3 +111,9 @@ def custom_json_serializer(obj):
110111
elif isinstance(obj, datetime.datetime):
111112
return pytz.timezone('UTC').localize(obj).isoformat()
112113
raise TypeError(repr(obj) + " is not JSON serializable")
114+
115+
class Enum(baseEnum.Enum):
116+
# Enum strings are prefixed by their class: "Category.classifier".
117+
# This overrides that behaviour and removes the prefix.
118+
def __str__(self):
119+
return str(self.name)

0 commit comments

Comments
 (0)