Skip to content

Commit bfa1432

Browse files
add and test insert() method; update docs
1 parent 1773526 commit bfa1432

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

cedargrove_paletteslice/paletteslice_acme.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ def __setitem__(self, key, value):
8484
self._reference_list[key] = working_list
8585
self._create_new_palette(self._reference_list)
8686

87-
def __delitem__(self, key):
88-
"""UNTESTED:
89-
Delete a slice from the primary class displayio.Palette. Permanently modifies
90-
reference_list and palette.
91-
Usage is ``del PaletteSlice[key]``. ONLY WORKS IN REPL
92-
93-
param slice key: The target slice object to delete from the new color palette."""
94-
"""self._reference_list = del self._reference_list[key]
95-
self._create_new_palette(self._reference_list)"""
96-
# pylint: disable = (unnecessary-pass)
97-
pass
98-
9987
def __contains__(self, color):
10088
"""Determine if reference_list contains the singleton color. Returns True or False.
10189
Usage is ``color in PaletteSlice.palette``.
@@ -152,8 +140,7 @@ def extend(self, add_list):
152140
self._create_new_palette(self._reference_list)
153141

154142
def insert(self, key, color):
155-
"""UNTESTED:
156-
Insert an opaque color value into the primary class palette at
143+
"""Insert an opaque color value into the primary class palette at
157144
slice object key. Permanently modifies reference_list and palette.
158145
Usage is ``PaletteSlice.insert(key, color)``.
159146

examples/paletteslice_acme_simpletest.py

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,70 +65,96 @@ def print_palette(new_palette):
6565
# Test of append()
6666
print("\n" + ("=" * 15))
6767
print("TEST append()")
68-
test = 0xF0F0F0
69-
print(f" value to insert: {test:#08x}")
68+
test_value = 0xF0F0F0
69+
print(f" value to append: {test_value:#08x}")
7070

7171
(last_color, last_transparency) = pal_sliceable.reference_list[-1]
7272
length = len(pal_sliceable)
7373
print(
7474
f"length BEFORE append: {length} last item: {last_color:#08x} {last_transparency}"
7575
)
7676

77-
pal_sliceable.append(test)
77+
pal_sliceable.append(test_value)
7878

7979
(last_color, last_transparency) = pal_sliceable.reference_list[-1]
8080
length = len(pal_sliceable)
8181
print(
8282
f"length AFTER append: {length} last item: {last_color:#08x} {last_transparency}"
8383
)
8484

85+
# Test of insert()
86+
print("\n" + ("=" * 15))
87+
print("TEST insert()")
88+
test_value = 0xA0A0A0
89+
test_index = 100
90+
print(f" value to insert: {test_value:#08x} at index: {test_index}")
91+
92+
(old_color, old_transparency) = pal_sliceable.reference_list[test_index]
93+
length = len(pal_sliceable)
94+
print(f"length BEFORE insert: {length} item: {old_color:#08x} {old_transparency}")
95+
96+
pal_sliceable.insert(test_index, test_value)
97+
98+
(new_color, new_transparency) = pal_sliceable.reference_list[test_index]
99+
length = len(pal_sliceable)
100+
print(f"length AFTER insert: {length} item: {new_color:#08x} {new_transparency}")
101+
85102
# Test of is_transparent(), make_transparent(), make_opaque()
86103
print("\n" + ("=" * 15))
87-
test = -1 # Index to test
104+
test_value = -1 # Index to test
88105
print("TEST is_transparent(), make_transparent(), make_opaque()")
89-
print(f" index value for test: {test}")
90-
print(f" is_transparent: {pal_sliceable.is_transparent(test)}")
91-
pal_sliceable.make_transparent(test)
92-
print(f"AFTER make_transparent -> is_transparent: {pal_sliceable.is_transparent(test)}")
93-
pal_sliceable.make_opaque(test)
94-
print(f"AFTER make_opaque -> is_transparent: {pal_sliceable.is_transparent(test)}")
106+
print(f" index value for test: {test_value}")
107+
print(f" is_transparent: {pal_sliceable.is_transparent(test_value)}")
108+
pal_sliceable.make_transparent(test_value)
109+
print(
110+
f"AFTER make_transparent -> is_transparent: {pal_sliceable.is_transparent(test_value)}"
111+
)
112+
pal_sliceable.make_opaque(test_value)
113+
print(
114+
f"AFTER make_opaque -> is_transparent: {pal_sliceable.is_transparent(test_value)}"
115+
)
95116

96117
# Test of count()
97118
print("\n" + ("=" * 15))
98119
print("TEST count()")
99-
test = 0xFFFFFF # Color value for search
100-
print(f" color value for search: {test:#08x}")
120+
test_value = 0xFFFFFF # Color value for search
121+
print(f" color value for search: {test_value:#08x}")
101122

102-
print(f"number of occurrences: {pal_sliceable.count(test)}")
123+
print(f"number of occurrences: {pal_sliceable.count(test_value)}")
103124

104125
# Test of index()
105126
print("\n" + ("=" * 15))
106127
print("TEST index()")
107-
test = 0xFFFFF0 # Color value for search
108-
print(f" color value for search: {test:#08x}")
128+
test_value = 0xFFFFF0 # Color value for search
129+
print(f" color value for search: {test_value:#08x}")
130+
131+
print(f"first index found: {pal_sliceable.index(test_value)}")
109132

110-
print(f"first index found: {pal_sliceable.index(test)}")
133+
test_value = 0xFFFFFF # Color value for search
134+
print(f" color value for search: {test_value:#08x}")
135+
136+
print(f"first index found: {pal_sliceable.index(test_value)}")
111137

112138
# Test of pop()
113139
print("\n" + ("=" * 15))
114140
print("TEST pop()")
115-
test = -1 # Remove last item
116-
print(f" index to pop: {test}")
141+
test_value = -1 # Remove last item
142+
print(f" index to pop: {test_value}")
117143

118144
print(f" length BEFORE pop: {len(pal_sliceable)}")
119-
print(f"value removed: {pal_sliceable.pop(test):#08x}")
145+
print(f"value removed: {pal_sliceable.pop(test_value):#08x}")
120146
print(f" length AFTER pop: {len(pal_sliceable)}")
121147

122148
# Test of __contains__
123149
print("\n" + ("=" * 15))
124150
print("TEST __contains__")
125-
test = 0
126-
print(f" value to find: {test}")
151+
test_value = 0
152+
print(f" value to find: {test_value}")
127153

128-
if test in pal_sliceable:
129-
print(f"< {test} > in pal_sliceable (True)")
154+
if test_value in pal_sliceable:
155+
print(f"< {test_value} > in pal_sliceable (True)")
130156
else:
131-
print(f"< {test} > NOT in pal_sliceable (False)")
157+
print(f"< {test_value} > NOT in pal_sliceable (False)")
132158

133159
# Place the test image into a tile and append to the primary display group
134160
test_tile = displayio.TileGrid(test_bitmap, pixel_shader=test_palette_source)
@@ -142,11 +168,13 @@ def print_palette(new_palette):
142168

143169
# Show the test image and label
144170
display.show(primary_group)
171+
print("\n" + ("=" * 15))
145172
print("TEST of source image")
146173
slice_label.text = "PALETTE SLICE: source image and palette"
147174
time.sleep(2)
148175

149176
# Continuous random slice test
177+
print("\n" + ("=" * 15))
150178
print("TEST of random slices")
151179
while True:
152180
# Create a random slice object; prohibit step == 0
33.4 KB
Loading

0 commit comments

Comments
 (0)