The MALAffinity class

class malaffinity.MALAffinity(base_user=None, round=False)

The MALAffinity class.

The purpose of this class is to store a “base user“‘s scores, so affinity with other users can be calculated easily.

For the user Xinil, the class can be initialised as follows:

from malaffinity import MALAffinity

ma = MALAffinity("Xinil")

The instance, stored in ma, will now hold Xinil‘s scores.

comparison() and calculate_affinity() can now be called, to perform operations on this data.

__init__(base_user=None, round=False)

Initialise an instance of MALAffinity.


To avoid dealing with dodgy globals, this class MAY be initialised without the base_user argument, in the global scope (if you wish), but init() MUST be called sometime afterwards, with a base_user passed, before affinity calculations take place.

Example (for the user Xinil):

from malaffinity import MALAffinity

ma = MALAffinity()


The class should then be good to go.

  • base_user (str or None) – Base MAL username
  • round (int or False) – Decimal places to round affinity values to. Specify False for no rounding

Get the affinity between the “base user” and username.


The data returned will be a tuple, with the affinity and shared rated anime. This can easily be separated as follows (using the user Luna as username):

affinity, shared = ma.calculate_affinity("Luna")


The final affinity value may or may not be rounded, depending on the value of _round, set at class initialisation.

Parameters:username (str) – The username to calculate affinity with
Returns:(float affinity, int shared)
Return type:tuple

Get a comparison of scores between the “base user” and username.

A Key-Value returned will consist of the following:



    30831: [3, 8],
    31240: [4, 7],
    32901: [1, 5],


The JSON returned isn’t valid JSON. The keys are stored as integers instead of the JSON standard of strings. You’ll want to force the keys to strings if you’ll be using the ids elsewhere.

Parameters:username (str) – The username to compare the base users’ scores to
Returns:Key-value pairs as described above
Return type:dict

Retrieve a “base user“‘s list, and store it in _base_scores.

Parameters:base_user (str) – Base users’ username