Skip to content

Commit f3b8f0c

Browse files
committed
Change Image class to be more explicit
1 parent e28211d commit f3b8f0c

3 files changed

Lines changed: 45 additions & 46 deletions

File tree

opus/data.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
class Data(object):
44
def __init__(self, json):
5-
self.json = json
6-
self.index = 0
5+
self._json = json
76
self._data = {}
8-
for row in self.json['page']:
7+
for row in json['page']:
98
el = DataElement(self.columns, row)
109
self._data[str(el)] = el
1110

@@ -33,27 +32,27 @@ def values(self):
3332

3433
@property
3534
def count(self):
36-
return int(self.json['count'])
35+
return int(self._json['count'])
3736

3837
@property
3938
def limit(self):
40-
return int(self.json['limit'])
39+
return int(self._json['limit'])
4140

4241
@property
4342
def order(self):
44-
return self.json['order']
43+
return self._json['order']
4544

4645
@property
4746
def page_no(self):
48-
return int(self.json['page_no'])
47+
return int(self._json['page_no'])
4948

5049
@property
5150
def labels(self):
52-
return self.json['labels']
51+
return self._json['labels']
5352

5453
@property
5554
def columns(self):
56-
return self.json['columns']
55+
return self._json['columns']
5756

5857
class DataElement(object):
5958
def __init__(self, columns, row):

opus/images.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,36 @@ def download(self, out=None):
2323

2424
class Images(object):
2525
def __init__(self, json, size):
26-
self.json = json
26+
self._json = json
2727
self.size = size
28-
self.index = 0
28+
self._data = {}
29+
for img in json['data']:
30+
el = Image(img['ring_obs_id'], img['path'], img['img'])
31+
self._data[str(el)] = el
2932

3033
def __repr__(self):
31-
return 'OPUS API Images object (with {} images)'.format(self.count)
34+
return 'OPUS API Images object (with {} {} images):\n'.format(len(self), self.size) + \
35+
'\n'.join(' - {}'.format(key) for key, _ in self.items())
3236

3337
def __len__(self):
34-
return self.count
38+
return len(self._data)
3539

36-
def __getitem__(self, index):
37-
img = self.json['data'][index]
38-
return Image(img['ring_obs_id'], img['path'], img['img'])
40+
def __getitem__(self, attr):
41+
return self._data[attr]
3942

4043
def __iter__(self):
41-
return self
44+
return iter(self._data)
4245

43-
def __next__(self):
44-
try:
45-
result = self.__getitem__(self.index)
46-
except IndexError:
47-
self.index = 0
48-
raise StopIteration
49-
self.index += 1
50-
return result
46+
def keys(self):
47+
return self._data.keys()
5148

52-
def next(self):
53-
return self.__next__()
49+
def items(self):
50+
return self._data.items()
51+
52+
def values(self):
53+
return self._data.values()
5454

55-
@property
56-
def count(self):
57-
return len(self.json['data'])
5855

5956
@property
6057
def order(self):
61-
return self.json['order']
58+
return self._json['order']

tests/test_images.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import responses
55
import json as JSON
66

7-
from opus.images import *
7+
from opus.images import Images, Image
88

99
@pytest.fixture
1010
def size():
@@ -22,26 +22,29 @@ def image(size):
2222
return Image(ring_obs_id, json['path'], json['data'][0]['img'])
2323

2424
def test_images_meta(images, size):
25-
assert repr(images) == 'OPUS API Images object (with 10 images)'
25+
r = repr(images)
26+
assert 'OPUS API Images object' in r
27+
assert 'S_IMG_CO_ISS_1508094647_N' in r
2628
assert len(images) == 10
2729
assert images.order == 'obs_general.time1'
2830
assert images.size == size
31+
assert isinstance(images['S_IMG_CO_ISS_1508094647_N'], Image)
2932

30-
31-
def test_images_image(images):
32-
image = images[0]
33-
assert repr(image) == 'OPUS API Image object: S_IMG_CO_ISS_1508094647_N'
34-
assert str(image) == 'S_IMG_CO_ISS_1508094647_N'
35-
assert image.ring_obs_id == 'S_IMG_CO_ISS_1508094647_N'
36-
assert image.path == 'https://pds-rings.seti.org/holdings/previews/'
37-
assert image.img == 'COISS_2xxx/COISS_2016/data/1508054834_1508117267/N1508094647_1_med.jpg'
38-
assert image.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2016/data/1508054834_1508117267/N1508094647_1_med.jpg'
39-
33+
def test_images_image(image):
34+
assert repr(image) == 'OPUS API Image object: S_IMG_CO_ISS_1459551972_N'
35+
assert str(image) == 'S_IMG_CO_ISS_1459551972_N'
36+
assert image.ring_obs_id == 'S_IMG_CO_ISS_1459551972_N'
37+
assert image.path == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/'
38+
assert image.img == 'COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
39+
assert image.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
4040

4141
def test_images_iter(images):
42-
images.next() == images[0]
43-
for ii, img in enumerate(images):
44-
img = images[ii]
42+
for key in images:
43+
assert key in images.keys()
44+
break
45+
for value in images.values():
46+
assert isinstance(value, Image)
47+
break
4548

4649

4750
@responses.activate

0 commit comments

Comments
 (0)