API

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.

Note

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()

ma.init("Xinil")

The class should then be good to go.

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

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

Note

The data returned will be a namedtuple, 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")

Alternatively, the following also works:

affinity = ma.calculate_affinity("Luna")

with the affinity and shared available as affinity.affinity and affinity.shared respectively.

Note

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
comparison(username)

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

A Key-Value returned will consist of the following:

{
    ANIME_ID: [BASE_USER_SCORE, OTHER_USER_SCORE],
    ...
}

Example:

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

Warning

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
init(base_user)

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

Parameters:base_user (str) – Base users’ username