Skip to content

Commit fe663e5

Browse files
committed
Add DataDict to Categories
1 parent 9d86800 commit fe663e5

5 files changed

Lines changed: 14 additions & 65 deletions

File tree

opus/categories.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
11
# -*- coding: utf-8 -*-
22

3+
from .data import DataDict
4+
35
class Category(object):
46
def __init__(self, json):
57
self.name = json['table_name']
68
self.label = json['label']
79

810
def __repr__(self):
9-
return '`{}` -> {}'.format(self.name, self.label)
11+
return '{} ({})'.format(self.label, self.name)
1012

1113

12-
class Categories(object):
14+
class Categories(DataDict):
1315
def __init__(self, json):
16+
DataDict.__init__(self)
1417
self._json = json
15-
self._data = {}
1618
for category in json:
17-
self._data[category['table_name']] = Category(category)
19+
self.append(category['table_name'], Category(category))
1820

1921
def __repr__(self):
2022
return 'OPUS API list of all categories ({}):\n'.format(len(self)) + \
21-
'\n'.join(
22-
' - {} ({})'.format(value.label, key) for key, value in self.items()
23-
)
24-
25-
def __len__(self):
26-
return len(self._json)
27-
28-
def __getitem__(self, attr):
29-
return self._data[attr.lower()]
30-
31-
def __iter__(self):
32-
return iter(self._data)
33-
34-
def keys(self):
35-
return self._data.keys()
36-
37-
def items(self):
38-
return self._data.items()
39-
40-
def values(self):
41-
return self._data.values()
42-
23+
'\n'.join(' - {}'.format(value) for value in self.values())

tests/test_categories.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import json as JSON
33
import pytest
4-
import six
54

6-
from opus.categories import *
5+
from opus.categories import Categories, Category
76

87
@pytest.fixture
98
def category():
@@ -16,35 +15,10 @@ def categories():
1615
return Categories(json)
1716

1817
def test_category_repr(category):
19-
assert repr(category) == '`obs_general` -> General Constraints'
18+
assert repr(category) == 'General Constraints (obs_general)'
2019

2120

2221
def test_categories_repr(categories):
23-
if six.PY3:
24-
assert repr(categories) == \
25-
'OPUS API list of all categories (3):\n' + \
26-
' - General Constraints (obs_general)\n' + \
27-
' - Ring Geometry Constraints (obs_ring_geometry)\n' + \
28-
' - Wavelength Constraints (obs_wavelength)'
29-
else:
30-
r = repr(categories)
31-
assert 'OPUS API list of all categories (3):' in r
32-
assert 'obs_general' in r
33-
assert 'obs_ring_geometry' in r
34-
assert 'obs_wavelength' in r
35-
36-
def test_categories_iter(categories):
37-
assert 'obs_general' in categories
38-
39-
for key, value in categories.items():
40-
assert categories[key] == value
41-
break
42-
43-
def test_categories_keys_values(categories):
44-
keys = categories.keys()
45-
values = categories.values()
46-
assert len(keys) == 3
47-
assert len(values) == 3
48-
assert 'obs_general' in keys
49-
50-
22+
r = repr(categories)
23+
assert 'OPUS API list of all categories (3)' in r
24+
assert 'General Constraints (obs_general)' in r

tests/test_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
import pytest
3-
43
import json as JSON
54

65
from opus.data import DataDict, Data, DataElement

tests/test_fields.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
import json as JSON
32
import pytest
4-
import six
5-
6-
7-
from opus.fields import *
3+
import json as JSON
84

5+
from opus.fields import Fields, Field, splitKey
96

107
@pytest.fixture
118
def field():

tests/test_metadata.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import pytest
3-
43
import json as JSON
5-
64
from datetime import datetime as dt
75

86
from opus.metadata import *

0 commit comments

Comments
 (0)