Utilities

Various utilities of this package.

Warning

These utilties are less aimed at end users and might therefore be subject to change.

ankipandas.util.log.get_logger()[source]

Sets up global logger.

ankipandas.util.log.set_log_level(level: Union[str, int]) → None[source]

Set global log level.

Parameters:level – Either an int (https://docs.python.org/3/library/logging.html#levels) or one of the keywords, ‘critical’ (only the most terrifying of log messages), ‘error’, ‘warning’, ‘info’, ‘debug’ (all log messages)
Returns:None

DataFrame utilities.

ankipandas.util.dataframe.replace_df_inplace(df: pandas.core.frame.DataFrame, df_new: pandas.core.frame.DataFrame) → None[source]

Replace dataframe ‘in place’.

Parameters:
Returns:

None

ankipandas.util.dataframe.merge_dfs(df: pandas.core.frame.DataFrame, df_add: pandas.core.frame.DataFrame, id_df: str, inplace=False, id_add='id', prepend='', replace=False, prepend_clash_only=True, columns=None, drop_columns=None)[source]

Merge information from two dataframes.

Parameters:
  • df – Original pandas.DataFrame
  • df_addpandas.DataFrame to be merged with original pandas.DataFrame
  • id_df – Column of original dataframe that contains the id along which we merge.
  • inplace – If False, return new dataframe, else update old one
  • id_add – Column of the new dataframe that contains the id along which we merge
  • prepend – Prepend a string to the column names from the new dataframe
  • replace – Replace columns
  • prepend_clash_only – Only prepend string to the column names from the new dataframe if there is a name clash.
  • columns – Keep only these columns
  • drop_columns – Drop these columns
Returns:

New merged pandas.DataFrame

ankipandas.util.misc.invert_dict(dct: dict) → dict[source]

Invert dictionary, i.e. reverse keys and values.

Parameters:dct – Dictionary
Returns:Dictionary with reversed keys and values.
Raises:ValueError if values are not unique.
ankipandas.util.misc.flatten_list_list(lst: List[List[Any]]) → List[Any][source]

Takes a list of lists and returns a list of all elements.

Parameters:lst – List of Lists
Returns:list
ankipandas.util.checksum.field_checksum(data: str) → int[source]

32 bit unsigned number from first 8 digits of sha1 hash. Apply this to the first field to the the field checksum that is used by Anki to detect duplicates.

Parameters:data – string like
Returns:int