I am learning the use of class
in Python
. Typically I write several function
to run my script, but recently i am writing using class.
I am sorry for the basic question, but when is the limit to use a class?
In other hand, inside my code i coded this function where you read all text file in a directory and you save in a temporary file all values. The text files are x,y, and z format. The function returns the name of the temporary file, the bounding box, the origin (top-left corner), and the bottom (bottom-right corner). Is It useful to convert function like this in a class? if yes why? if no why?
import os
import tempfile
import glob
class LaserException(Exception):
"""Laser exception, indicates a laser-related error."""
pass
sepType = {
"space": ' ',
"tab": '\t',
"comma": ',',
"colon": ':',
"semicolon": ';',
"hyphen": '-',
"dot": '.'
}
def tempfile_merge(path,separator,wildcard= '*.txt'):
file_temp = tempfile.NamedTemporaryFile(delete=False,dir=path)
name = file_temp.name
minx = float('+inf')
maxx = float('-inf')
miny = float('+inf')
maxy = float('-inf')
for file in glob.glob(os.path.join(path,wildcard)):
for line in open(file, "r"):
element = line.split(sepType[separator])
if len(element) < 3:
raise TypeError("not enough arguments: %s has only %s columns" % (inFile_name_suffix,len(element)))
try:
maxx = max(maxx, float(element[0]))
minx = min(minx, float(element[0]))
maxy = max(maxy, float(element[1]))
miny = min(miny, float(element[1]))
except ValueError:
raise LaserException("x,y,z are not float-values")
newelement = " ".join([str(e) for e in element])+ "\n"
file_temp.write(newelement)
file_temp.close()
return(name, ((minx,maxy),(maxx,maxy),(maxx,miny),(minx,miny)),(minx,maxy),(maxx,miny))