I am struggling with commenting and variable naming. My teacher says my comments need to be more explanatory and explicit. He says my variable names are also sometimes confusing. I was just wondering whether you could go through my code and see whether you are able to understand the comments and add in comments/edit where you feel I should add comments or improve them. Lastly, are there any general rules to follow with commenting?
class PPM(object):
def __init__(self, infile, outfile):
self.infile=infile
self.outfile=outfile
#Read in data of image
data= open(self.infile,"r")
datain=data.read()
splits=datain.split(None, 4)
#Header info and pixel info
self.type=splits[0]
self.columns=int(splits[1])
self.rows=int(splits[2])
self.colour=int(splits[3])
#(Return a new array of bytes)
self.pixels=bytearray(splits[4])
def grey_scale(self):
for row in range(self.rows):
for column in range(self.columns):
start = row * self.columns * 3 + column * 3
end = start + 3
r, g, b = self.pixels[start:end]
brightness = int(round( (r + g + b) / 3.0 ))
self.pixels[start:end] = brightness, brightness, brightness
def writetofile(self):
dataout= open(self.outfile, "wb")
#Use format to convert back to strings to concatenate them and Those {} in the write function get's replaced by the arguments of the format function.
dataout.write('{}\n{} {}\n{}\n{}'.format(self.type,
self.columns, self.rows,
self.colour,
self.pixels))
sample = PPM("cake.ppm", "Replica.ppm")
sample.grey_scale()
sample.writetofile()