Review this code, please

mail@pastecode.io avatar
unknown
python
3 years ago
1.2 kB
8
Indexable
Never
class CSV_Report_Handler():
    """
    Helper class to fix yearly csv reports. Multiplies salaries for the
    specified workers by factor of 2.
    """
    def __init__(self, names=[]):
        """
        Accepts list of names to mulitply salaries.
        """
        self.names = names

    def openfile(self, filename):
        """
        Reads file and stores it in self.data.
        """
        self.filename = filename
        try:
            self.data = open(filename, 'r').readlines()
        except Exception:
            self.data = []

    def save(self):
        """
        Processes and saves file.
        """
        try:
            self.do_process()
            f = open(self.filename, 'w')
            f.write(self.data.join('\n'))
            f.close()
        except Exception, e:
            return (False, "File error: {}".format(str(e)))
        return (True, 'ok')

    def do_process(self):
        """
        Multiplies salaries for people in `names`.
        """
        res = []
        for line in self.data:
            name, salary = line.split(',')
            if name in self.names:
                salary = int(salary) * 2
            res.append(",".join([name, salary]))
        self.data = res