@@ -14,9 +14,9 @@ def __init__(self, width, height, colors):
1414
1515 def _abs_pos (self , width , height ):
1616 if height > self .height - 1 :
17- raise ValueError (' height > max' )
17+ raise ValueError (" height > max" )
1818 if width > self .width - 1 :
19- raise ValueError (' width > max' )
19+ raise ValueError (" width > max" )
2020 return (width * self .width ) + height
2121
2222 def _decode (self , position ):
@@ -27,92 +27,94 @@ def __setitem__(self, key, value):
2727 # order is X, Y from the docs https://github.com/adafruit/circuitpython/blob/master/shared-bindings/displayio/Bitmap.c
2828 self .__setitem__ (self ._abs_pos (key [0 ], key [1 ]), value )
2929 return
30- #if key > self.height * self.width:
30+ # if key > self.height * self.width:
3131 # raise RuntimeError('illegal set position {}'.format(self._decode(key)))
3232 if not isinstance (value , int ):
33- raise RuntimeError (f' set value as int, not { type (value )} ' )
33+ raise RuntimeError (f" set value as int, not { type (value )} " )
3434 self .data [key ] = value
3535
3636 def __getitem__ (self , item : str ) -> bytearray :
3737 if isinstance (item , tuple ):
3838 return self .__getitem__ (self ._abs_pos (item [0 ], item [1 ]))
39- #if item > self.height * self.width:
39+ # if item > self.height * self.width:
4040 # raise RuntimeError('illegal item position {}'.format(item))
4141 try :
4242 return self .data [item ]
4343 except KeyError :
44- raise RuntimeError (' no data at {}' .format (self ._decode (item )))
44+ raise RuntimeError (" no data at {}" .format (self ._decode (item )))
4545
4646 def validate (self ):
4747 if not self .data :
48- raise ValueError (' no rows were set / no data in memory' )
48+ raise ValueError (" no rows were set / no data in memory" )
4949 for i in range (self .height * self .width , 1 ):
5050 try :
5151 self .data [i ]
5252 except KeyError :
53- raise ValueError (' missing data at {i}' )
53+ raise ValueError (" missing data at {i}" )
5454
5555 def __str__ (self ):
56- out = ' \n '
56+ out = " \n "
5757 for y in range (self .height ):
5858 for x in range (self .width ):
5959 data = self [x , y ]
60- out += f' { data } '
61- out += ' \n '
60+ out += f" { data } "
61+ out += " \n "
6262 return out
6363
64+
6465logging .getLogger ().setLevel (logging .INFO )
6566
67+
6668class TestBitmap_C (TestCase ):
6769 def test_init (self ):
6870 b = Bitmap_C_Interface (2 , 4 , 1 )
69- self .assertEqual (2 ,b .width )
71+ self .assertEqual (2 , b .width )
7072 self .assertEqual (4 , b .height )
7173 self .assertEqual (1 , b .colors )
7274
7375 def test_set_tuple (self ):
74- b = Bitmap_C_Interface (2 ,4 , 1 )
76+ b = Bitmap_C_Interface (2 , 4 , 1 )
7577 b [0 , 0 ] = 67
7678 self .assertEqual (b [0 , 0 ], 67 )
7779
7880 def test_set_abs (self ):
79- b = Bitmap_C_Interface (2 ,4 , 1 )
81+ b = Bitmap_C_Interface (2 , 4 , 1 )
8082 b [0 ] = 42
8183 self .assertEqual (b [0 ], 42 )
8284
8385 def test_abs_and_tuple (self ):
84- b = Bitmap_C_Interface (2 ,4 , 1 )
86+ b = Bitmap_C_Interface (2 , 4 , 1 )
8587 b [0 ] = 101
8688 self .assertEqual (101 , b [0 , 0 ])
8789
8890 def test_non_zero (self ):
89- b = Bitmap_C_Interface (2 ,4 , 1 )
91+ b = Bitmap_C_Interface (2 , 4 , 1 )
9092 b [1 , 1 ] = 100
9193 self .assertEqual (100 , b [1 , 1 ])
9294
9395 def test_throws_x_out_of_range (self ):
94- b = Bitmap_C_Interface (2 ,4 , 1 )
96+ b = Bitmap_C_Interface (2 , 4 , 1 )
9597 try :
96- b [2 ,1 ] = 100
97- self .fail (' should have thrown' )
98+ b [2 , 1 ] = 100
99+ self .fail (" should have thrown" )
98100 except ValueError :
99101 pass
100102
101103 def test_max (self ):
102- b = Bitmap_C_Interface (2 ,4 , 1 )
103- b [1 ,1 ] = 66
104+ b = Bitmap_C_Interface (2 , 4 , 1 )
105+ b [1 , 1 ] = 66
104106 self .assertEqual (66 , b [1 , 1 ])
105107
106108 def test_uninitialized (self ):
107- b = Bitmap_C_Interface (2 ,4 , 1 )
109+ b = Bitmap_C_Interface (2 , 4 , 1 )
108110 try :
109111 b [1 , 1 ]
110- self .fail (' should have thrown' )
112+ self .fail (" should have thrown" )
111113 except RuntimeError :
112114 pass
113115
114116 def test_validate_throws (self ):
115- b = Bitmap_C_Interface (2 ,4 , 1 )
117+ b = Bitmap_C_Interface (2 , 4 , 1 )
116118 try :
117119 b .validate ()
118120 except ValueError :
@@ -126,74 +128,99 @@ def test_repr(self):
126128 b [0 , 1 ] = 1
127129 b [1 , 1 ] = 1
128130 b [2 , 1 ] = 0
129- self .assertEqual (' \n 100\n 110\n ' , str (b ))
131+ self .assertEqual (" \n 100\n 110\n " , str (b ))
130132
131133 def test_decode (self ):
132- b = Bitmap_C_Interface (4 ,4 , 1 )
134+ b = Bitmap_C_Interface (4 , 4 , 1 )
133135 self .assertEqual ((0 , 0 ), b ._decode (0 ))
134- encoded = b ._abs_pos (3 ,3 )
135- self .assertEqual ((3 ,3 ), b ._decode (encoded ))
136-
136+ encoded = b ._abs_pos (3 , 3 )
137+ self .assertEqual ((3 , 3 ), b ._decode (encoded ))
137138
138139
139140class TestPnmLoad (TestCase ):
140-
141141 def test_load_fails_with_no_header_data (self ):
142142 f = BytesIO (b"some initial binary data: \x00 \x01 " )
143143 try :
144- pnm .load (f , b'P1' , bitmap = Bitmap_C_Interface )
145- self .fail (' should have failed' )
144+ pnm .load (f , b"P1" , bitmap = Bitmap_C_Interface )
145+ self .fail (" should have failed" )
146146 except Exception as e :
147147 if "Unsupported image format" not in str (e ):
148148 raise
149149
150150 def test_load_works_p1_ascii (self ):
151- test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p1_mono.pbm' )
152- with open (test_file , 'rb' ) as f :
153- bitmap , palette = pnm .load (f , b'P1' , bitmap = Bitmap_C_Interface )
151+ test_file = os .path .join (
152+ os .path .dirname (__file__ ),
153+ ".." ,
154+ ".." ,
155+ "examples" ,
156+ "images" ,
157+ "netpbm_p1_mono.pbm" ,
158+ )
159+ with open (test_file , "rb" ) as f :
160+ bitmap , palette = pnm .load (f , b"P1" , bitmap = Bitmap_C_Interface )
154161 self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ), bitmap )
155162 self .assertEqual (1 , bitmap .colors )
156163 self .assertEqual (13 , bitmap .width )
157164 self .assertEqual (21 , bitmap .height )
158165
159166 bitmap .validate ()
160167 self .assertEqual (0 , bitmap [0 ]) # check first row
161- self .assertEqual (1 , bitmap [12 ,1 ]) # check second row
168+ self .assertEqual (1 , bitmap [12 , 1 ]) # check second row
162169
163170 def test_load_works_p4_in_mem (self ):
164171 f = BytesIO (b"P4\n 4 2\n \x55 " )
165- bitmap , palette = pnm .load (f , b'P4' , bitmap = Bitmap_C_Interface )
172+ bitmap , palette = pnm .load (f , b"P4" , bitmap = Bitmap_C_Interface )
166173 self .assertEqual (4 , bitmap .width )
167174 self .assertEqual (2 , bitmap .height )
168175 bitmap .validate ()
169- self .assertEqual (' \n 1010\n 1010\n ' , str (bitmap ))
176+ self .assertEqual (" \n 1010\n 1010\n " , str (bitmap ))
170177
171178 def test_load_works_p4_binary (self ):
172- test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p4_mono.pbm' )
173- with open (test_file , 'rb' ) as f :
174- bitmap , palette = pnm .load (f , b'P4' , bitmap = Bitmap_C_Interface )
179+ test_file = os .path .join (
180+ os .path .dirname (__file__ ),
181+ ".." ,
182+ ".." ,
183+ "examples" ,
184+ "images" ,
185+ "netpbm_p4_mono.pbm" ,
186+ )
187+ with open (test_file , "rb" ) as f :
188+ bitmap , palette = pnm .load (f , b"P4" , bitmap = Bitmap_C_Interface )
175189 self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ))
176190 self .assertEqual (1 , bitmap .colors )
177191 self .assertEqual (8 , bitmap .width )
178192 self .assertEqual (8 , bitmap .height )
179193 bitmap .validate ()
180194
181195 def test_load_works_p4_binary_high_res (self ):
182- test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p4_mono_large.pbm' )
183- with open (test_file , 'rb' ) as f :
184- bitmap , palette = pnm .load (f , b'P4' , bitmap = Bitmap_C_Interface )
196+ test_file = os .path .join (
197+ os .path .dirname (__file__ ),
198+ ".." ,
199+ ".." ,
200+ "examples" ,
201+ "images" ,
202+ "netpbm_p4_mono_large.pbm" ,
203+ )
204+ with open (test_file , "rb" ) as f :
205+ bitmap , palette = pnm .load (f , b"P4" , bitmap = Bitmap_C_Interface )
185206 self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ))
186207 self .assertEqual (1 , bitmap .colors )
187208 self .assertEqual (1920 , bitmap .width )
188209 self .assertEqual (1080 , bitmap .height )
189210
190211
191212class TestPPMLoad (TestCase ):
192-
193213 def test_load_works_p3_ascii (self ):
194- test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p3_rgb_ascii.ppm' )
195- with open (test_file , 'rb' ) as f :
196- bitmap , palette = pnm .load (f , b'P3' , bitmap = Bitmap_C_Interface )
214+ test_file = os .path .join (
215+ os .path .dirname (__file__ ),
216+ ".." ,
217+ ".." ,
218+ "examples" ,
219+ "images" ,
220+ "netpbm_p3_rgb_ascii.ppm" ,
221+ )
222+ with open (test_file , "rb" ) as f :
223+ bitmap , palette = pnm .load (f , b"P3" , bitmap = Bitmap_C_Interface )
197224 self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ), bitmap )
198225 self .assertEqual (16777216 , bitmap .colors )
199226 self .assertEqual (16 , bitmap .width )
@@ -202,9 +229,16 @@ def test_load_works_p3_ascii(self):
202229 str (bitmap )
203230
204231 def test_load_works_p6_binary (self ):
205- test_file = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'examples' , 'images' , 'netpbm_p6_binary.ppm' )
206- with open (test_file , 'rb' ) as f :
207- bitmap , palette = pnm .load (f , b'P6' , bitmap = Bitmap_C_Interface )
232+ test_file = os .path .join (
233+ os .path .dirname (__file__ ),
234+ ".." ,
235+ ".." ,
236+ "examples" ,
237+ "images" ,
238+ "netpbm_p6_binary.ppm" ,
239+ )
240+ with open (test_file , "rb" ) as f :
241+ bitmap , palette = pnm .load (f , b"P6" , bitmap = Bitmap_C_Interface )
208242 self .assertTrue (isinstance (bitmap , Bitmap_C_Interface ), bitmap )
209243 self .assertEqual (16777216 , bitmap .colors )
210244 self .assertEqual (16 , bitmap .width )
0 commit comments