|
| 1 | +from typing import List |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from src.file_handler import FileHandler |
| 5 | +from src.analyzer import Analyzer |
| 6 | +from src.logger import Logger |
| 7 | + |
| 8 | +class Facade: |
| 9 | + """Facade class for the app. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, input_type: str, input_path: str): |
| 13 | + """Init method |
| 14 | +
|
| 15 | + Args: |
| 16 | + input_type (str): Type of input file |
| 17 | + input_path (str): Path of input file |
| 18 | + """ |
| 19 | + self.logger = Logger() |
| 20 | + |
| 21 | + self._file_handler = FileHandler(input_type=input_type, |
| 22 | + input_path=input_path) |
| 23 | + |
| 24 | + self._dataframe = self._file_handler.dataframe |
| 25 | + |
| 26 | + self._analyzer = Analyzer(self._dataframe) |
| 27 | + |
| 28 | + def count_unique_rows(self, columns: List[str]) -> int: |
| 29 | + """Count unique rows based on column combination. |
| 30 | +
|
| 31 | + Args: |
| 32 | + columns (List[str]): List of columns to be used in aggregation. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + int: Return number of rows |
| 36 | + """ |
| 37 | + count = self._analyzer.count_unique_rows(columns) |
| 38 | + |
| 39 | + return count |
| 40 | + |
| 41 | + def find_average(self, column: str) -> float: |
| 42 | + """Find average value of values in column |
| 43 | +
|
| 44 | + Args: |
| 45 | + column (str): column name |
| 46 | +
|
| 47 | + Returns: |
| 48 | + float: return average |
| 49 | + """ |
| 50 | + average = self._analyzer.find_average(column) |
| 51 | + |
| 52 | + return average |
| 53 | + |
| 54 | + def find_top(self, sort_column: str, top: int, return_column: str) -> str: |
| 55 | + """Find the top N values of return_column, based on sort_column. |
| 56 | +
|
| 57 | + Args: |
| 58 | + sort_column (str): Column to sort by |
| 59 | + top (int): Number of records to return |
| 60 | + return_column (str): Column to return values for |
| 61 | +
|
| 62 | + Returns: |
| 63 | + str: Top N values for return_column |
| 64 | + """ |
| 65 | + top_rated_movies = self._analyzer.find_top_rows(column=sort_column, top=top)[return_column] |
| 66 | + |
| 67 | + return top_rated_movies |
| 68 | + |
| 69 | + def movies_by_year(self, column: str) -> str: |
| 70 | + """Count movies by year |
| 71 | +
|
| 72 | + Args: |
| 73 | + column (str): year column |
| 74 | +
|
| 75 | + Returns: |
| 76 | + str: Return string representation of dataframe |
| 77 | + """ |
| 78 | + list_of_count = self._analyzer.movies_by_year(column)#.values.tolist() |
| 79 | + |
| 80 | + return list_of_count.to_string() |
| 81 | + |
| 82 | + def movies_by_genre(self, column: str) -> str: |
| 83 | + """Count movies by genre. |
| 84 | +
|
| 85 | + Args: |
| 86 | + column (str): genre column. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + str: Return string representation of dataframe |
| 90 | + """ |
| 91 | + list_of_count = self._analyzer.movies_by_genre(column)#.values.tolist() |
| 92 | + |
| 93 | + return list_of_count.to_string() |
| 94 | + |
| 95 | + def save_as(self, output_type: str, output_path: str): |
| 96 | + """Save dataframe as file type |
| 97 | +
|
| 98 | + Args: |
| 99 | + output_type (str): Type of the file to be saved |
| 100 | + output_path (str): Path of the file to be saved |
| 101 | + """ |
| 102 | + self._file_handler.save_df_as_file(output_type=output_type, output_path=output_path) |
| 103 | + |
0 commit comments