Utilities#

Various utilities of this package.

Warning

These utilities 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: 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

ankipandas.util.log.set_debug_log_level() None[source]#

Set global log level to debug.

DataFrame utilities.

ankipandas.util.dataframe.replace_df_inplace(df: DataFrame, df_new: DataFrame) None[source]#

Replace dataframe ‘in place’. If the dataframe has a _metadata field, containing a list of attribute names that contain metadata, then this is copied from df to the new dataframe.

Parameters:
  • dfpandas.DataFrame to be replaced

  • df_newpandas.DataFrame to replace the previous one

Returns:

None

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

Merge information from two dataframes. If the dataframe has a _metadata field, containing a list of attribute names that contain metadata, then this is copied from df to the new dataframe.

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

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.misc.nested_dict()[source]#

This is very clever and stolen from https://stackoverflow.com/questions/16724788/ Use it to initialize a dictionary-like object which automatically adds levels. E.g.

a = nested_dict()
a['test']['this']['is']['working'] = "yaaay"
ankipandas.util.misc.defaultdict2dict(defdict: defaultdict) dict[source]#
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