Repository: The-krolik/dpav (language: python)

Issue:
Update vbuffer.py
- renamed error checking functions for readability
- restricted to 8-bit color due to limitations with the window class


Discussion / hints:

Fix patch:
diff --git a/vbuffer.py b/vbuffer.py
index 250bcce..599888c 100644
--- a/vbuffer.py
+++ b/vbuffer.py
@@ -4,47 +4,73 @@
 
 import numpy as np
 
+# TODO: color depth arg?
+
 class VBuffer:
     
-    def __init__(self, dimensions):
-        # TODO
-        # imageArray: a numpy array that holds the pixel data of a picture
-        if type(dimensions) is not list and type(dimensions) is not tuple:
-            raise TypeError("Error! Dimensions is supposed to be a list or tuple!")
-        elif len(dimensions) != 2:
-            raise ValueError("Error! Dimensions are only meant to be 2 values!")
-        elif type(dimensions[0]) is not int or type(dimensions[1]) is not int:
-            raise TypeError("Error! Dimensions are meant to be integer values! ")
-
-
-        xdim, ydim = dimensions[0], dimensions[1]
-        self._buffer = np.zeros((xdim, ydim, 3), dtype=int)
-        self._dimensions = (xdim, ydim, 3)
+    def __init__(self, arg1 = (600, 800)):
+        if type(arg1) is np.ndarray:
+            dimensions = arg1.shape
+            self._checkNumpyArr(arg1,"arg1","__init__")
+        else:
+            self._checkCoordType(arg1, "arg1", "__init__")
+            dimensions = arg1
+            if dimensions[0] > 1920 or dimensions[1] > 1080:
+                raise ValueError(f"dimensions provided of size: {dimensions}, highest supported resolution is (1920,1080)")
         
-        # debugFlag: a boolean variable
+        self.buffer = arg1 if type(arg1) is np.ndarray else np.zeros(dimensions, dtype=int)
         self.debugFlag = False
+                
+    def _checkNumpyArr(self,arg1,argName,methodName):
+        if not np.issubdtype(arg1.dtype, int) and not np.issubdtype(arg1.dtype,float):
+            raise TypeError(f"{argName} argument to VBuffer. {methodName} must be of type numpy.ndarray dtype=int, or a 2 element dimension list/tuple!")
+        if np.any((arg1<0)|(arg1>16777215)):
+            raise ValueError(f"{argName} argument to VBuffer. {methodName} must contain values in range 0 to 16777215")
+        if arg1.ndim != 2:
+            raise TypeError(f"{argName} argument to VBuffer. {methodName} must be 2-dimensional numpy.ndarray")
+        if arg1.shape[0] > 1920 or arg1.shape[1] > 1080:
+            raise ValueError(f"{argName} argument to VBuffer. {methodName} np.ndarray is of size: {arg1.shape} highest supported resolution is (1920,1080)")
+        
 
+    def _checkCoordType(self, coords, argName, methodName):
+        if type(coords) is not list and type(coords) is not tuple:
+            raise TypeError(f"{argName} argument to VBuffer. {methodName} should be a list or a tuple!")
+        elif len(coords) != 2:
+            raise TypeError(f"{argName} argument to VBuffer. {methodName} requires exactly 2 values!")
+        elif type(coords[0]) is not int or type(coords[1]) is not int:
+            raise TypeError(f"{argName} argument to VBuffer. {methodName} can only have integer values!")
+        
+    def _checkCoordVal(self, coords, methodName):
+        x,y = coords[0],coords[1]
+        if x < 0 or y < 0:
+            raise ValueError(f"Coordinate args to VBuffer.{methodName} should be greater than zero.")
+        elif x >= self.buffer.shape[0] or y >= self.buffer.shape[1]:
+            raise ValueError(f"Coordinate args to VBuffer.{methodName} are out of bounds.")
         
     # Sets pixel at coordinates coords in buffer to hex value val
     def writePixel(self, coords, val):
+        self._checkCoordType(coords, "coords", "writePixel")
+        self._checkCoordVal(coords, "coords", "writePixel")
+        if type(val) is not int:
+            raise TypeError("Color value must be an integer value!")
+        elif val < 0 or val > 0xffffff:
+            raise ValueError(f"Color value must be an integer between 0 and 16777215!")
+            
         x, y = coords[0], coords[1]
-        self._buffer[x, y] = val
-        
-        
+        self.buffer[x, y] = val
+
     def getPixel(self, coords):
         x, y = coords[0], coords[1]
-        return self._buffer[x, y]
+        return self.buffer[x, y]
         
     # <insert definition of getDimensions function here>; X and Y are int variables
     def getDimensions(self):
-        return self._dimensions
-        
+        return self.buffer.shape
+    
+    def setBuffer(self, buf):
+        self._checkNumpyArr(buf,"buf", "setBuffer")
+        self.buffer = buf      
         
     # 
     def clearBuffer(self):
-        self._buffer[:] = 0
-
-    def getBuffer(self):
-        return self._buffer
-        
-    
+        self.buffer[:] = 0
\ No newline at end of file


Test patch:
diff --git a/tests.py b/tests.py
index f9d8814..58e02ab 100644
--- a/tests.py
+++ b/tests.py
@@ -105,8 +105,11 @@ def test_vbuffer():
         vb.writePixel([399, 299], 100.5)
     assert vb.getPixel([399, 299]) == 0
 
-    vb.writePixel([0,0], 255)
-    assert vb.getPixel([0, 0]) == 255
+    vb.writePixel([0,0], 16777215)
+    assert vb.getPixel([0, 0]) == 16777215
+    
+    vb.writePixel([599, 799], 0xffffff)
+    assert vb.getPixel([0, 0]) == 16777215
     
     vb.clearBuffer()
     buf = vb.getBuffer()