Skip to content

Commit

Permalink
move Accessor out of GUIConfig and fix docstring style
Browse files Browse the repository at this point in the history
  • Loading branch information
j8xixo12 committed Aug 22, 2024
1 parent 6d4c3da commit d25d5c2
Showing 1 changed file with 54 additions and 45 deletions.
99 changes: 54 additions & 45 deletions modmesh/app/euler1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,68 +104,77 @@ def update(self, adata, ndata):
self.num.figure.canvas.draw()


class GUIConfig:
class _Accessor:
"""
Helper calss to access data within the configuration table using
multiple dimensions, currenlty only support 2-dimensions table.
This class allows users to access data by chaining multiple [],
supporting string-based.
Attributes:
:ivar: _data: handle input data that allow user can using
index or string to access it.
:ivar: _header (list): list of data's column header name.
:ivar: _dimIdx (int): using to indicate which dimension is
currenlty being accessed by __getitem__.
"""
def __init__(self, data, dimIdx=0, header=None):
self._data = data
self._header = header
self._dimIdx = dimIdx

def __getitem__(self, key):
if self._dimIdx == 0:
for row_idx, row in enumerate(self._data):
if key == row[0]:
return _Accessor(self._data[row_idx],
self._dimIdx+1,
self._header)
else:
for idx, ele in enumerate(self._header):
if key == ele:
return self._data[idx]
raise KeyError(f'Invaild key: {key} not found in table')


class GUIConfig(object):
"""
Configuration class for the GUI.
This class provides a configuration interface for the GUI, allowing
users to set and retrieve parameters related to the simulation.
Attributes:
- `state` (:class:`State`): The state object holding configuration
data.
- `_tbl_content` (:class:`State`): The content of the
configuration table.
- `_col_header` (list): The header for the configuration
table columns.
:ivar: state (:class:`State`): The state object holding configuration
data.
:ivar: _tbl_content (:class:`State`): The content of the
configuration table.
:ivar: _col_header (list): The header for the configuration
table columns.
Methods:
- :meth:`data(row, col)`: Get the value at a specific row and column
in the configuration table.
- :meth:`setData(row, col, value)`: Set the value at a specific row
and column in the configuration table.
- :meth:`columnHeader(col)`: Get the header for a specific column
in the configuration table.
- :meth:`editable(row, col)`: Check if a cell in the configuration
table is editable.
- :meth:`rowCount()`: Get the number of rows in the configuration
table.
- :meth:`columnCount()`: Get the number of columns in the
configuration table.
:meth:`data(row, col)`: Get the value at a specific row and column
in the configuration table.
:meth:`setData(row, col, value)`: Set the value at a specific row
and column in the configuration table.
:meth:`columnHeader(col)`: Get the header for a specific column
in the configuration table.
:meth:`editable(row, col)`: Check if a cell in the configuration
table is editable.
:meth:`rowCount()`: Get the number of rows in the configuration
table.
:meth:`columnCount()`: Get the number of columns in the
configuration table.
"""
class Accessor:
"""
Helper calss to access data within the configuration table using
multiple dimensions, currenlty only support 2-dimensions table.
This class allows users to access data by chaining multiple [],
supporting string-based.
"""
def __init__(self, data, dimIdx=0, header=None):
self._data = data
self._header = header
self._dimIdx = dimIdx

def __getitem__(self, key):
if self._dimIdx == 0:
for row_idx, row in enumerate(self._data):
if key == row[0]:
return GUIConfig.Accessor(self._data[row_idx],
self._dimIdx+1,
self._header)
else:
for idx, ele in enumerate(self._header):
if key == ele:
return self._data[idx]
raise KeyError(f'Invaild key: {key} not found in table')

def __init__(self, input_data, col_headers):
self.state = State(input_data)
self._tbl_content = self.state
self._col_header = col_headers

def __getitem__(self, key):
return self.Accessor(self._tbl_content, 0, self._col_header)[key]
return _Accessor(self._tbl_content, 0, self._col_header)[key]

def data(self, row, col):
"""
Expand Down

0 comments on commit d25d5c2

Please sign in to comment.