def make_column_list(df: Optional[pd.DataFrame] = None, columns: Optional[Union[List[str], str]] = None, prefixes: Optional[Union[List[str], str]] = None, suffixes: Optional[Union[List[str], str]] = None, mask: Optional[Union[List[bool]]] = None) -> None: """Dynamically creates a new column list from a list of columns, lists of prefixes, and/or boolean mask. This method serves as the basis for the 'column_lists' decorator which allows users to pass 'prefixes', 'columns', and 'mask' to a wrapped method with a 'columns' argument. Those three arguments are then combined into the final 'columns' argument. Args: df (DataFrame): pandas object. columns (list or str): column names to be included. prefixes (list or str): list of prefixes for columns to be included. suffixes (list or str): list of suffixes for columns to be included. mask (numpy array, list, or Series, of booleans): mask for columns to be included. Returns: column_names (list): column names created from 'columns', 'prefixes', and 'mask'. """ column_names = [] try: for boolean, feature in zip(mask, list(df.columns)): if boolean: column_names.append(feature) except TypeError: pass try: temp_list = [] for prefix in listify(prefixes, default_null=True): temp_list = [col for col in df if col.startswith(prefix)] column_names.extend(temp_list) except TypeError: pass try: temp_list = [] for prefix in listify(suffixes, default_null=True): temp_list = [col for col in df if col.endswith(suffix)] column_names.extend(temp_list) except TypeError: pass try: column_names.extend(listify(columns, default_null=True)) except TypeError: pass return deduplicate(iterable=column_names)
def wrapper(*args, **kwargs): new_arguments = {} parameters = dict(call_signature.parameters) arguments = dict(call_signature.bind(*args, **kwargs).arguments) unpassed = list(parameters.keys() - arguments.keys()) if 'columns' in unpassed: columns = [] else: columns = listify(arguments['columns']) try: columns.extend( make_column_list(prefixes=arguments['prefixes'])) del arguments['prefixes'] except KeyError: pass try: columns.extend( make_column_list(suffixes=arguments['suffixes'])) del arguments['suffixes'] except KeyError: pass try: columns.extend(make_column_list(mask=arguments['mask'])) del arguments['mask'] except KeyError: pass if not columns: columns = list(columns.keys()) arguments['columns'] = deduplicate(columns) # method.__signature__ = Signature(arguments) return method(**arguments)
def inject_attributes(self, instance: object, overwrite: Optional[bool] = False) -> object: """Injects appropriate items into 'instance' from 'configuration'. Args: instance (object): siMpLify class instance to be modified. overwrite (Optional[bool]): whether to overwrite a local attribute in 'instance' if there are values stored in that attribute. Defaults to False. Returns: instance (object): siMpLify class instance with modifications made. """ sections = ['general'] try: sections.append(instance.name) except AttributeError: pass try: sections.extend(listify(instance.idea_sections)) except AttributeError: pass for section in sections: try: for key, value in self.configuration[section].items(): self._inject(instance=instance, attribute=key, value=value, overwrite=overwrite) except KeyError: pass return instance
def _create_mask( self, arguments: Dict[str, Union[List[str], str]]) -> Dict[str, List[str]]: try: arguments['columns'] = listify(arguments['columns']) except KeyError: arguments['columns'] = [] return arguments
def __delitem__(self, key: Union[List[str], str]) -> None: """Deletes 'key' in 'contents'. Args: key (Union[List[str], str]): name(s) of key(s) in 'contents'. """ self.contents = { i: self.contents[i] for i in self.contents if i not in listify(key)} return self
def add(self, chapters: Union[List['Chapter'], 'Chapter']) -> None: """Combines 'chapters' with existing 'chapters' attribute. Args: chapters (Union['Chapter', List['Chapter']]): a 'Chapter' instance or list of such instances. """ self.chapters.extend(listify(chapters, default_empty=True)) return self
def _inject_instance(self, source: str, targets: List[Any]) -> None: """Injects local attribute into other classes. Args: source (str): attribute name of item to be injected into each target in 'targets'. targets (List[Any]): objects to inject 'source' into. """ for target in listify(targets): setattr(target, source, getattr(self, source)) return self
def subsetify(self, keys: Union[List[str], str]) -> 'Repository': """Returns a subset of a Repository Args: keys (Union[List[str], str]): key(s) to get key/values. Returns: 'Repository': with only keys in 'key'. """ return Repository( contents = {i: self.contents[i] for i in listify(keys)})
def add( self, techniques: Union[List['Technique'], 'Technique', List[Tuple[str, str]], Tuple[str, str]] ) -> None: """Combines 'techniques' with 'steps' or 'techniques' attribute. If a tuple or list of tuples is passed, 'techniques' are added to the 'steps' attribute. Otherwise, they are added to the 'techniques' attribute. Args: chapters (Union[List['Technique'], 'Technique', List[Tuple[str, str]], Tuple[str, str]]): a 'Technique' instance or tuple used to create one. """ if isinstance(listify(techniques)[0], 'Technique'): self.techniques.extend(listify(techniques)) else: self.steps.extend(listify(techniques)) return self
def _get_settings(self, section: str, prefix: str, suffix: str) -> List[str]: """Returns settings from 'idea' based on 'name' and 'suffix'. Args: section (str): outer key name in 'idea' section. prefix (str); prefix for an inner key name. suffix (str): suffix to inner key name in 'idea'. Returns: List[str]: names of matching workers, steps, or techniques. """ return listify(self.idea[section]['_'.join([prefix, suffix])])
def _get_indices(self, columns: Union[List[str], str]) -> List[bool]: """Gets column indices for a list of column names. Args: columns (Union[List[str], str]): name(s) of columns for which indices are sought. Returns: bool mask for columns matching 'columns'. """ return [ self.data.columns.get_loc(column) for column in listify(columns) ]
def _check_columns(self, columns: Union[List[str], str] = None) -> List[str]: """Returns 'columns' keys if columns doesn't exist. Args: columns (Union[List[str], str]): column names. Returns: if columns is not None, returns columns, otherwise, the keys of the 'columns' attribute are returned. """ if columns: return listify(columns) else: return list(self.data.columns.values)
def _get_special(self, instance: object, suffix: str) -> List[str]: """Returns list of strings from appropriate item in 'configuration'. Args: instance (object): siMpLify class with 'name' attribute to find matching item in 'configuration'. suffix (str): suffix of item in 'configuration'. Returns: List[str]: item from 'configuration. """ try: return listify(self.configuration[instance.name]['_'.join( [instance.name, suffix])]) except (KeyError, AttributeError): return None
def change_datatype(self, columns: [Union[List[str], str]], datatype: str) -> None: """Changes column datatypes of 'columns'. The 'datatype' becomes the new datatype for the columns in both the 'datatypes' dict and in reality - a method is called to try to convert the column to the appropriate datatype. Args: columns ([Union[List[str], str]]): column name(s) for datatypes to be changed. datatype (str): contains name of the datatype to convert the columns. """ for name in listify(columns): self.data[name] = self.types.convert(proxy_type=datatype, column=self.data[name]) self.datatypes[name] = datatype return self
def _get_techniques(self, instance: object) -> Dict[str, Dict[str, None]]: """Returns nested dictionary of techniques. Args: instance (object): siMpLify class with 'name' attribute to find matching items in 'configuration'. Returns: Dict[str, Dict[str, None]]: techniques dictionary prepared to be loaded into a 'Repository' instance. """ contents = {} try: for key, value in self.configuration[instance.name].items(): if (key.endswith('_techniques') and not value in [None, 'none', 'None']): step = key.replace('_techniques', '') contents[step] = {} for technique in listify(value): contents[step][technique] = None return Repository(contents=contents) except (KeyError, AttributeError): return None