oTree Forum >

Defining variables as dictionaries - no models for it

#1 by kikenc49

Hi everyone,
Upgrading a project, I need to duplicate some variables to include other assets. To avoid duplication and creation of more variables I thought to transform those variables into dictionaries so they can allocate the data in different keys for the assets I want to track. I am aware that there is not option to initiate a variable on the class as a dictionary (https://otree.readthedocs.io/en/latest/models.html#fields), but inside the models options I found this VarsDict that seems to work. If I code them like ‘models.VarsDict(initial={‘key1’: 0, ‘key2’: 0})’ with the ‘initial’ statements it raises an error, so I left them as follow:

class Subsession(BaseSubsession):
    long_string1 = models.LongStringField()
    integer1 = models.IntegerField(initial=0)
    integer2 = models.IntegerField(initial=0)
    integer3 = models.IntegerField(initial=0)
    float1 = models.FloatField(initial=0)
    vars_dict1 = models.VarsDict({‘key1’: 0, ‘key2’: 0})
    vars_dict2 = models.VarsDict({'key1’: 0, 'key2’: 0})
    string1 = models.StringField()

The problem is that those variables (the ones defined as dict) get reset to the values above after every round, it does not keep the track of the methods and functions when I iterate on the classes at the page_sequence.

The question is: is there a way to initiate variables in the classes as dictionaries? Otherwise, is there a way around to make them be dictionaries? I think about define them as dictionaries afterwards using an specific method but I am not sure of the return.

Let me know if it is not clear or need more information.

Thanks in advance!

Best, Enrique

Write a reply

Set forum username