Persistant Data

To allow you to keep track of data between submitting a move for processing we offer a data dictionary called persistent-data that will preserve its contents between calls to calculateMove.

Using the persistent-data variable

The persistent-data data dictionary will initially be empty so will need to check to see if a key exists before setting or altering its value. For example if you wanted to keep count of the number moves you have submitted you could use the following code:

if "move_count" not in persistent_data:
    persistent_data["move_count"] = 0
    persistent_data["move_count"] += 1

The if statement check to see if the Key "move_count" exists in the persistent_data dictionary. If it does not it initialises the "move_count" value.

If the "move_count" key does already exist then your code can work with it. In this case it increases the "move_count" value by 1

You can add any number of new Keys to the persistent_data dictionary.

An example config function using persistent_data

Often the first step in calculateMove() is to recall data or update some persistent data. We can define a function that is called at the start of calculateMove() to make sure that our persistent_data is ready. This example comes from our Microsoft Match Game.

def set_persistent_data(num_tiles, tile_backs):
    if "AnalysedTiles" not in persistent_data:
        persistent_data["AnalysedTiles"] = [False for _ in range(num_tiles)]  # Mark all tiles as not analysed
    if "AttemptedCombinations" not in persistent_data:
        persistent_data["AttemptedCombinations"] = []
    if "TileInfo" not in persistent_data:
        # TileInfo records information on unmatched tiles only
        persistent_data["TileInfo"] = {}  # Initialise tile information as empty JSON object
    if "Categories" not in persistent_data:
        persistent_data["Categories"] = {}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License