I am having an indexing issue due to the way I'm trying to pull data. Here is a snip of my code:
if field == 5:
#if all inputs, do this
if self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "" and self.getSibling("Dropdown").props.value[4] != "":
days = 11111
#if there's four outputs, do this
elif self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "":
if self.getSibling("Dropdown").props.value[0] == 0 or self.getSibling("Dropdown").props.value[1] == 0 or self.getSibling("Dropdown").props.value[2] == 0 or self.getSibling("Dropdown").props.value[3] == 0:
Monday = 1
else:
Monday = 0
if self.getSibling("Dropdown").props.value[0] == 1 or self.getSibling("Dropdown").props.value[1] == 1 or self.getSibling("Dropdown").props.value[2] == 1 or self.getSibling("Dropdown").props.value[3] == 1:
Tuesday = 1
else:
Tuesday = 0
if self.getSibling("Dropdown").props.value[0] == 2 or self.getSibling("Dropdown").props.value[1] == 2 or self.getSibling("Dropdown").props.value[2] == 2 or self.getSibling("Dropdown").props.value[3] == 2:
Wednesday = 1
else:
Wednesday = 0
if self.getSibling("Dropdown").props.value[0] == 3 or self.getSibling("Dropdown").props.value[1] == 3 or self.getSibling("Dropdown").props.value[2] == 3 or self.getSibling("Dropdown").props.value[3] == 3:
Thursday = 1
else:
Thursday = 0
if self.getSibling("Dropdown").props.value[0] == 4 or self.getSibling("Dropdown").props.value[1] == 4 or self.getSibling("Dropdown").props.value[2] == 4 or self.getSibling("Dropdown").props.value[3] == 4:
Friday = 1
else:
Friday = 0
days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)
My issue is starting at this line:
if self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "" and self.getSibling("Dropdown").props.value[4] != "":
The issue is that there is not always a list, it could just be one option or it could be all 5 options. When it's not all 5 options and only one option for example, I am unable to run my query as a self.getSibling("Dropdown").props.value[1] may not exist. Does anyone have any tips for situations like this?
PS: I did not set this up this way, I am trying to correct an issue with the programming a CO-OP left us with.
Thank you in advance for reading and participating in this amazing community.
I ran your code through ChatGPT(4) with specific instructions to optimize, and at a glance the output seems pretty good:
if field == 5:
values = self.getSibling("Dropdown").props.value
not_empty_values = all(v != "" for v in values[:5])
if not_empty_values and len(values) >= 5:
days = '11111'
elif not_empty_values and len(values) >= 4:
day_flags = [any(values[i] == str(day) for i in range(4)) for day in range(5)]
days = ''.join(['1' if flag else '0' for flag in day_flags])
I can't promise this will help with your indexing issue, but it's going to be a hell of a lot easier to maintain.
Hi Robert,
To get a better idea of how to help, what component is running this script and what is it trying to accomplish? Is the dropdown menu always populated with 5 options (Monday -> Friday)?
Create a custom prop of type array on the view. Call it ddValue.
Create a bidirectional property binding from Dropdown's value to view.custom.ddValue.
Now your code (not that I'm recommending it) becomes:
if view.custom.ddValue[0] == 0 or view.custom.ddValue[1] == 0 or view.custom.ddValue[2] == 0 orview.custom.ddValue[3] == 0:
Monday = 1
This in turn could be simplified to,
Monday = view.custom.ddValue[0] == 0 or view.custom.ddValue[1] == 0 or view.custom.ddValue[2] == 0 orview.custom.ddValue[3] == 0
Tuesday = view.custom.ddValue[0] == 1 or view.custom.ddValue[1] == 1 or view.custom.ddValue[2] == 1 or view.custom.ddValue[3] == 1
Wednesday = ...
Using the custom property binding gets rid of all the getSiblings which will all break if you rename the component or move it into a container. With the binding to a custom property on the view that won't happen.
A dropdown component is running this script, its purpose is to allow multiple days to be selected, giving a range of "#####" for the weekdays. So if Monday and Tuesday are selected, it will be 110000 whereas all selected will be 11111. I am inputting this into a SQL database.
if field == 5:
values = self.getSibling("Dropdown").props.value
days = ['0', '0', '0', '0', '0']
for i in xrange(len(days)):
if i in values:
days[i] = '1'
days = ''.join(days)
else:
days = table.getValueAt(0,6)
Think of ChatGPT as a clever but error-prone co-op, you have to carefully check its code for mistakes before you trust production with it. Sometimes the stuff it comes up with is just plain weird, too.