Junior — Senior
SOLID Refactoring: Reworking the FileStats Class
livecode
Task condition
It is necessary to modify the FileStats class so that it fully complies with SOLID principles. Responsibility should be divided among separate components, and checks dependent on file extensions should be removed from within methods.
class FileStats:
def __init__(self, *extensions):
self.file_count = 0
self.extensions = extensions
self.line_total = 0
self.column_total = 0
def increase(self, **kwargs):
self.file_count += 1
if 'txt' in self.extensions or 'csv' in self.extensions:
self.line_total += kwargs.get('lines', 0)
if 'csv' in self.extensions:
self.column_total += kwargs.get('columns', 0)
def __str__(self):
result = f'Files count = {self.file_count}'
if 'txt' in self.extensions or 'csv' in self.extensions:
result += f'\ntotal lines = {self.line_total}'
if 'csv' in self.extensions:
result += f'\ntotal columns = {self.column_total};'
return result