Module panama.analytics.column_collection
Classes
class ColumnCollection (**col_lists)
-
Expand source code
class ColumnCollection: def __init__(self, **col_lists): self._COL_LIST_NAMES = [] for k, v in col_lists.items(): val = list_value_append(v) self.add_columns(k, val) def get_columns_by_name(self, name: str) -> List[str]: """Function used to get a column list by a name. The name is searched in a lax way, looking for keys that have that string in the name. If multiple occurrences are found, the lists are concatenated. If no match is found, an empty list is returned. Args: name (str): name of the type of cols to retrieve. Returns: List[str]: the list of the columns selected. """ output = [] keys = self.get_columns_keys() available_keys = [i for i in keys if name in i] for k in available_keys: v = list_value_append(getattr(self, k)) output.extend(v) return output def update_columns(self, key: str, value: Union[str, List[str], None]): """Update a column list, adding new data. Args: key (str): the name of the list to update. value (Union[str, List[str], None]): values to add. """ values = list_value_append(self.get_columns_by_name(key), value) self.add_columns(key=key, value=values) def add_columns(self, key: str, value: Union[str, List[str], None]): """Add a new list of columns. Args: key (str): name of the list. value (Union[str, List[str], None]): list of columns. """ wrapped_values = wrap_in_list(value) setattr(self, key, wrapped_values) if key not in self._COL_LIST_NAMES: self._COL_LIST_NAMES.append(key) def drop_column_value(self, key: str, value: Union[str, List[str]]): """Method to drop values from a column value. The key, unlike the get method, must be unique. Args: key (str): name of the columns list to drop. value (Union[str, List[str]]): values to drop from the list. Raises: NameError: column key {key} not defined. ValueError: Ambiguous key: multiple entries found matching {key}. Please provide an unique key. """ if key not in self._COL_LIST_NAMES: raise NameError(f"column key {key} not defined.") keys = [i for i in self._COL_LIST_NAMES if key in i] if len(keys) > 1: raise ValueError(f"Ambiguous key: multiple entries found matching {key}. Please provide an unique key.") else: keys = keys[0] cols = self.get_columns_by_name(keys) value = [i for i in cols if i not in list_value_append(value)] self.add_columns(key=keys, value=value) def get_columns(self) -> Dict[str, List[str]]: """Returns all the columns held by the class as as dict of lists. Returns: Dict[str, List[str]]: dictionary of columns. """ return {k: getattr(self, k) for k in self._COL_LIST_NAMES} def get_columns_list(self) -> List[str]: """Returns all the columns held by the class as a list. Returns: Union[List[str], str]: the columns held by the class. """ all_cols = self.get_columns().values() return [item for sublist in all_cols for item in sublist] def get_columns_keys(self) -> List[str]: """Returns all the keys held by the class. Returns: List[str]: list of the keys held by the class. """ return self._COL_LIST_NAMES def __getattr__(self, attr: str): if attr in self.get_columns_keys(): return self.get_columns_by_name(attr) else: raise ValueError(f"No column list having name {attr} found.")
Subclasses
Methods
def add_columns(self, key: str, value: Union[str, List[str], ForwardRef(None)])
-
Add a new list of columns.
Args
key
:str
- name of the list.
value
:Union[str, List[str], None]
- list of columns.
def drop_column_value(self, key: str, value: Union[str, List[str]])
-
Method to drop values from a column value. The key, unlike the get method, must be unique.
Args
key
:str
- name of the columns list to drop.
value
:Union[str, List[str]]
- values to drop from the list.
Raises
NameError
- column key {key} not defined.
ValueError
- Ambiguous key: multiple entries found matching {key}. Please provide an unique key.
def get_columns(self) ‑> Dict[str, List[str]]
-
Returns all the columns held by the class as as dict of lists.
Returns
Dict[str, List[str]]
- dictionary of columns.
def get_columns_by_name(self, name: str) ‑> List[str]
-
Function used to get a column list by a name. The name is searched in a lax way, looking for keys that have that string in the name.
If multiple occurrences are found, the lists are concatenated.
If no match is found, an empty list is returned.
Args
name
:str
- name of the type of cols to retrieve.
Returns
List[str]
- the list of the columns selected.
def get_columns_keys(self) ‑> List[str]
-
Returns all the keys held by the class.
Returns
List[str]
- list of the keys held by the class.
def get_columns_list(self) ‑> List[str]
-
Returns all the columns held by the class as a list.
Returns
Union[List[str], str]
- the columns held by the class.
def update_columns(self, key: str, value: Union[str, List[str], ForwardRef(None)])
-
Update a column list, adding new data.
Args
key
:str
- the name of the list to update.
value
:Union[str, List[str], None]
- values to add.