Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
The behavior of the following chunk of code has me tearing my hair out! It is from one of the 'routes' in a fairly complex Flask application, which I am writing to schedule Wemo devices. This is only a section of a much longer piece of code, and its purpose is to handle edits to the weekly 'pattern' for turning on (or off) a device at a given time. On the form that it is attached to, each day of the week is represented by a check box. If the name of the relevant checkbox ("Day_0" through "Day_6") exists in request.form (which is a dictionary), the box was checked. If the box was checked when the form was originally displayed, fields['pattern'][n] (where n corresponds to the Day #) is set to 1, if not, it is set to 0. In this code, 'changed' should be set to True if any of the check boxes has changed value.

The problem I am currently facing is that the code works as it should if a box that was originally displayed checked is unchecked by the user, but does not respond correctly if a box that was unchecked is checked by the user.
if 'Repeat' in request.form:
    for daynum in range(0,7):
        fldname='Day_'+ str(daynum)
        if fldname in request.form:
            flash(fldname + ': ' + str(fields['pattern'][daynum])) # added for debugging
            count +=  1
            if fields['pattern'][daynum] != 1:
                flash('changed should be True') # added for debugging
        elif fields['pattern'][daynum] == 1:
    if count == 0:
        flash('Repeating actions must repeat on at least one day of the week')

What I have tried:

I have tried to determine why the if test fails. The bizarre thing is that the newly checked box is correctly detected, and the first debugging flash duly appears when the form reloads. This flash also correctly reports that the relevant fields['pattern'][n] value is 0. However, the test a couple of lines below, which should succeed (since the value is not equal to one), fails, and 'changed' is not set to True (and the second debugging flash does not appear). Can anyone make any sense out of this?
Updated 7-Nov-19 10:23am

1 solution

The explanation for the weird behavior was that the contents of the fields['pattern'] array was being changed when I changed the corresponding setting in the mypattern Class instance, so, by the time the code got to testing it, it was the same as the relevant setting from request.form. Because of the way the code was written, this only affected its behavior when a previously checked box had been unchecked.

The basic problem was that, in writing the Class of which mypattern was an instance, I had not taken account of the way Python passes objects: the Class keeps the representation of the daily settings for an instance in an internal bytearray. The function that I had used to initialize fields['pattern'] should have been returning a copy of this array, but was actually returning a reference to the instance's internal array. Changing the setting in the instance also changed it in fields['pattern'], since this was actually pointing to the same array. I am not used to Classes allowing you to do this sort of thing!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900