Python Forum
Reduce the code for if statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reduce the code for if statements
#1
Hi,

I need your help with my current code. I am working on the if statements to check on the CurrentRowX value and the CurrentWidth value to see if I can get pass on one of these statement so I can add the program_id value or use to call the EPG_Right function to move the buttons object.

I would like to reduce the code to make it much shorter, easy and simple, but I have no idea how I could reduce it.

Here is what I am using right now:

if int(CurrentRowX) == 375 and int(CurrentWidth) <= 864:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 375 and int(CurrentWidth) > 864:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 441 and int(CurrentWidth) <= 568:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 441 and int(CurrentWidth) >= 626:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 507 and int(CurrentWidth) <= 456:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 507 and int(CurrentWidth) >= 515:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 559 and int(CurrentWidth) >= 515:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 610 and int(CurrentWidth) < 691:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 610 and int(CurrentWidth) >= 691:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 669 and int(CurrentWidth) < 691:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 669 and int(CurrentWidth) >= 691:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 724 and int(CurrentWidth) <= 515:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 724 and int(CurrentWidth) >= 567:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 790 and int(CurrentWidth) <= 344:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 790 and int(CurrentWidth) >= 456:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 838 and int(CurrentWidth) >= 456:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 897 and int(CurrentWidth) < 396:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 897 and int(CurrentWidth) >= 396:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 959 and int(CurrentWidth) < 342:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 959 and int(CurrentWidth) >= 342:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 1009 and int(CurrentWidth) >= 227:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 1072 and int(CurrentWidth) < 227:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 1072 and int(CurrentWidth) >= 227:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 1138 and int(CurrentWidth) < 165:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 1138 and int(CurrentWidth) >= 165:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 1184 and int(CurrentWidth) < 167:
    program_id = int(program_id) + 1
elif int(CurrentRowX) == 1184 and int(CurrentWidth) >= 167:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
elif int(CurrentRowX) == 1246 and int(CurrentWidth) >= 59:
    self.move_right_flag = True
    self.time_flag = True
    EPG_Right.GoRight(self)
If you could show me how I could reduce it to make it easy and simple, that would be great.

Thanks in advance
Reply
#2
how is CurrentRowX defined?
how is CurrentWidth defined?
why are you using numbers?
Is it to determine widget?
What graphics package are you using?
I expect there is an easy solution, but hard to tell

All of these questions can be answered if they were in full context.
It would be very helpful to have access to all code.
Reply
#3
(Feb-26-2018, 11:19 PM)Larz60+ Wrote: how is CurrentRowX defined?
how is CurrentWidth defined?
why are you using numbers?
Is it to determine widget?
What graphics package are you using?
I expect there is an easy solution, but hard to tell

All of these questions can be answered if they were in full context.
It would be very helpful to have access to all code.

Here is what I use to defined them both:

Current_ID = int(self.getFocusId())
CurrentRowX = self.getControl(Current_ID).getX()
CurrentWidth = self.getControl(Current_ID).getWidth()
I use them both to get the pixel X and width size as I want to control on them whether to jump the yellow box to the next button object or if I should call the EPG_Right function to move the buttons.

Here for example: If the button have a pixel 375 and the button object have a width size 691, then I would use program_id to jump on to the next button object. If the button have pixel 1072 and the width size is greater than 227, then I want to call the EPG_Right function so I can move the buttons object to move them to the left. On the left for the X, it start with 375, on the middle it will be 724 and the end will be 1246.
Reply
#4
any idea????????
Reply
#5
how is CurrentRowX defined?
how is CurrentWidth defined?
why are you using numbers?
Is it to determine widget?
What graphics package are you using?

I expect there is an easy solution, but hard to tell
Reply
#6
(Feb-27-2018, 04:50 PM)Larz60+ Wrote: how is CurrentRowX defined?
how is CurrentWidth defined?
why are you using numbers?
Is it to determine widget?
What graphics package are you using?

I expect there is an easy solution, but hard to tell

I am using the numbers as I want to control on the buttons whether to jump the yellow box to the next button object or if I should call the EPG_Right function to move the buttons.

Yes it is determine widget.

The graphic package I am using kodi gui, but it don't really matter because it will still work without the gui as I have wrote on the if statements.

I can only think this may work but you might have got a better idea than I do:

if int(CurrentRowX) == 375 and int(CurrentWidth) <= 864:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 441 and int(CurrentWidth) <= 568:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 507 and int(CurrentWidth) <= 456:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 610 and int(CurrentWidth) < 691:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 669 and int(CurrentWidth) < 691:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 724 and int(CurrentWidth) <= 515:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 790 and int(CurrentWidth) <= 344:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 897 and int(CurrentWidth) < 396:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 959 and int(CurrentWidth) < 342:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 1072 and int(CurrentWidth) < 227:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 1138 and int(CurrentWidth) < 165:
   program_id = int(program_id) + 1
elif int(CurrentRowX) == 1184 and int(CurrentWidth) < 167:
   program_id = int(program_id) + 1
else:
   self.move_right_flag = True
   self.time_flag = True
   EPG_Right.GoRight(self)
Reply
#7
The reason why I was asking which graphics program is that they handle events, and each one is different to a small degree,
but enough so that you have to know.
You seem to be trying to identify an item by where it is according to pixel location. If this is true, consider the situation where you have new entries at the top of the list or whatever this is coming from. Hard to tell because you won't share your code.
Kodi does indeed use events, see: https://kodi.tv/article/kodi-160-jarvis-mark-xvi
You have to learn this, and change your code accordingly.
Events allow you to click on a widget (whatever they are called in kodi) and immediately execute code that is associated with
that, and only that, widget. No need to use screen position. If you add a new widget, anywhere, the events don't change, so there
is no positioning problem.

There was a reason for all of my questions, I wasn't trying to be difficult.
so, If you insist on using x, y to find where mouse click (or touch event) occurred, I will now show you how to do that with
a dictionary.
Please consider what I am saying here, and make a decision.
Reply
#8
I have an idea you could try.
Store the values in two arrays.
I modified some of the width values so it could use all >= and <= operators.
That's as tight as I could think of making it with my limited python knowledge.
Let me know if it works or not, might need some tweaking.
rWidth = [864,865,568,626,456,515,514,690,691,690,691,515,567,344,456,455,395,396,341,342,226 ,226 ,227 ,164 ,166 ,166 ,167 ,58]
RowX =   [375,375,441,441,507,507,559,610,610,669,669,724,724,790,790,838,897,897,959,959,1009,1072,1072,1138,1138,1184,1184,1246]
for x in range(0,len(RowX)):
  if int(CurrentRowX) == RowX[x]:
    if int(CurrentWidth) <= rWidth[x]:  
      program_id = int(program_id) + 1
      break
    elif int(CurrentWidth) >= rWidth[x]:
      self.move_right_flag = True
      self.time_flag = True
      EPG_Right.GoRight(self)
      break
Reply
#9
Quote:I have an idea you could try.
This will work until the first time you add a new widget, now all of the pixel points
change in relation to the widgets, so entire table has to be re-written.
The proper way to do this is with events, which don't care about pixel positions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,867 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Adding values with reduce() function from the list of tuples kinimod 10 2,637 Jan-24-2023, 08:22 AM
Last Post: perfringo
  How do I reduce the time to Invoke Macro via Python? JaneTan 1 2,129 Dec-28-2020, 06:46 AM
Last Post: buran
  How to reduce the following code to run in sequence? Giggel 4 2,674 Jun-28-2020, 01:31 AM
Last Post: Giggel
  Why the multithread does not reduce the execution time? Nicely 2 2,480 Nov-23-2019, 02:28 PM
Last Post: Nicely
  Help to reduce time to execute the code prakash52kar 1 2,222 Oct-14-2019, 10:56 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020