oTree Forum >

Dealing with null fields

#1 by Christoph

Hi everyone, currently I am updating some older code. As back then, I am again facing the problem that null fields can’t be accessed. 

In the experiment, participant are at some point confronted with a number of fields for which they have to state beliefs. Participants are not required to fill out all of them. Hence, some will be None (we do not want them to see a default value and I found no good way to hide default values in the form). 
In the past, I brute force replaced all fields that are None with 0 internally and run the following functions to get and sum the fields:

bins = [getattr(player, f'q1_bin{i}') for i in range(1, 15)]
player.q1_sum = sum(bins)

Technically, in Python this is supposed to work with None values, with a slight twist:

bins = [getattr(player, f'q1_bin{i}') for i in range(1, 15)]
player.q1_sum = sum(filter(None, bins))

As far as I can tell from testing, this works without problems in pure Python. In oTree, however, I of course get an error for the getattr()-function, as some of the fields are null fields.

Can this be fixed? I would like to simplify my code here, but so far could not find a way that works. Functions such as field_maybe_none() are apparently not recognized when putting them anywhere into getattr().

#2 by coralio (edited )


Otree protects against null values by raising an error. In Player/Group/Subsession you have the method field_maybe_none, which prevents the error message. So this should do the job:

bins = [player.field_maybe_none(f'q1_bin{i}') for i in range(1, 15)]
player.q1_sum = sum(filter(None, bins))

I hope this helped

#3 by coralio

Sorry, I think your filter would not work. Look at this alternative: 

bins = [player.field_maybe_none(f'q1_bin{i}') for i in range(1, 15)]
player.q1_sum = sum([num for num in bins if num is not None])

#4 by Christoph

Hi thanks for the reply! The solution provided in comment #2 works. The filter is however working as intended (tested it again).

In hindsight I am not sure why I used getattr() here, only that it seemed necessary back then (I am pretty sure I tried the normal list comprehension first). Anyway, it would still be great if functions such as getattr() would not get derailed by otrees error messages.

#5 by coralio

I'm glad it worked. I think the creator(s) decided towards the "safe" path of raising errors with null fields. I think the reason for this is oTree Studio and that fact that oTree users are not usually experienced programmers. Null fields are sometimes the source of problems that are internalized to the programmer through the function field_maybe_none. I like this approach. But I don't know, this is just my guess of the reasons.

Write a reply

Set forum username