iPoetDev/PyCriteria

Solved: A memory leak ResourceWarning: Enable tracemalloc to get the object allocation traceback and controller.py:518: ResourceWarning: unclosed

iPoetDev opened this issue · 0 comments

Issue: _headers: list[str] = wsheet.row_values(headings)

Created an inner dependency on a remote connection, just to extract the headers for the table based on old predefine the headers

Solution: Using dataframes, dataframe.columns are a list[str] which greatly simplified

Usage:

  1. Configure WebConsole.table = None on init
  2. Use configure_table (as an inner function) and a given dataframe in a setter styled class method: updates internal table of webconsole
  3. The latest dataframe is applied to the table before display (no custom table filters)

Historical Triage:
Old Code (since removed, the furthest below is the new code

 @staticmethod
    def configure_table(headings: int = 1) -> rich.table.Table:
        """Configures Rich Console table."""
        wsheet: gspread.Worksheet = Controller.load_wsheet()
        consoletable: rich.table.Table = Table()
        selectheaders: dict[str, str] = tablesettings.CRITERIA_FILTER
        headersrange: str = tablesettings.HEADERS_RANGE
        _headers: list[str] = wsheet.row_values(headings)
        
        def configure_columns(_headers: list[str]):
            """Configures the headers."""
            for _header in _headers:
                # Check if the header is the predefined headers by values
                if _header in selectheaders.values():
                    # Use list comprehension to get the key from the value: reverse lookup
                    key = list(selectheaders.keys())[list(selectheaders.values()).index(_header)]
                    # Use the key to get the value from the dictionary
                    if key in headersrange:
                        consoletable.add_column(selectheaders[key])
        
        configure_columns(_headers)
        return consoletable

Was generating

PS D:\Code\Code Institute\PyCriteria> python app.py find --help

D:\Code\Code Institute\PyCriteria\controller.py:518: ResourceWarning: unclosed <ssl.SSLSocket fd=1844, family=2, type=1, proto=0, laddr=('192.168.0.46', 52221), raddr=('74.125.193.95', 443)>

  self.table = self.configure_table()                                                                                                                                                         

ResourceWarning: Enable tracemalloc to get the object allocation traceback                                                                                                                    

ResourceWarning: Enable tracemalloc to get the object allocation traceback

[pycriteria] controller.py (Lines 567-579)


 @staticmethod
    def configure_table(dataframe: pd.DataFrame, headings: int = 1) -> rich.table.Table:
        """Configures Rich Console table."""
        consoletable: rich.table.Table = Table()
        
        def configure_columns(_headers: list[str]):
            """Configures the headers."""
            for _header in _headers:
                # Check if the header is the predefined headers by values
                consoletable.add_column(_header)
        
        configure_columns(dataframe.columns)
        return consoletable

Open in IDE · Open on GitHub

Created from JetBrains using CodeStream