File utils refactor and unittests
Create unit test for file utils closes #25 closes #26
This commit is contained in:
30
pcbdevice/utils/FileUtils.py
Normal file
30
pcbdevice/utils/FileUtils.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import math
|
||||
|
||||
class FileUtils:
|
||||
@staticmethod
|
||||
def pbmToCsv(pbmFile, dimensionLineIndex = 2):
|
||||
completeFile = []
|
||||
|
||||
file = open(pbmFile, 'r')
|
||||
lines = file.readlines()
|
||||
width, height = (int(val) for val in lines[dimensionLineIndex].split())
|
||||
file.close()
|
||||
|
||||
for line in lines[dimensionLineIndex + 1:]:
|
||||
for val in line.split():
|
||||
completeFile += [int(val)]
|
||||
|
||||
matrix = [[0 for i in range(width)] for j in range(height)]
|
||||
for index, value in enumerate(completeFile):
|
||||
matrix[math.floor(index / width)][index % width] = value
|
||||
|
||||
return matrix
|
||||
|
||||
@staticmethod
|
||||
def saveMatrixToFile(matrix, filePath):
|
||||
with open(filePath, 'w') as f:
|
||||
for x in matrix:
|
||||
for y in x:
|
||||
f.write('%s ' % y )
|
||||
f.write('\n')
|
||||
f.close()
|
||||
14
pcbdevice/utils/TestUtils.py
Normal file
14
pcbdevice/utils/TestUtils.py
Normal file
@@ -0,0 +1,14 @@
|
||||
def readIntFile(filePath):
|
||||
completeFile = []
|
||||
file = open(filePath, 'r')
|
||||
lines = file.readlines()
|
||||
file.close()
|
||||
|
||||
for line in lines:
|
||||
tempArray = []
|
||||
for val in line.split():
|
||||
tempArray.append(int(val))
|
||||
|
||||
completeFile.append(tempArray)
|
||||
|
||||
return completeFile
|
||||
@@ -1,24 +0,0 @@
|
||||
import math
|
||||
|
||||
"""
|
||||
Reformat the file receive by the binary to ascii converter to have matrix with the right width and height
|
||||
"""
|
||||
|
||||
|
||||
def formatPbm(pbmFile):
|
||||
dimensionLineIndex = 2
|
||||
completeFile = []
|
||||
|
||||
lines = open(pbmFile, 'r').readlines()
|
||||
width, height = (int(val) for val in lines[dimensionLineIndex].split())
|
||||
|
||||
for line in lines[dimensionLineIndex+1:]:
|
||||
for val in line.split():
|
||||
completeFile += [int(val)]
|
||||
|
||||
formattedFile = [[0 for i in range(width)] for j in range(height)]
|
||||
for index, value in enumerate(completeFile):
|
||||
formattedFile[math.floor(index/width)][index%width] = value
|
||||
|
||||
return formattedFile
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
"""
|
||||
Save a two dimension array in a file
|
||||
"""
|
||||
|
||||
def matrixToFile(matrix, fileName):
|
||||
with open(fileName, 'w') as f:
|
||||
for x in matrix:
|
||||
for y in x:
|
||||
f.write('%s ' % y )
|
||||
f.write('\n')
|
||||
|
||||
Reference in New Issue
Block a user