Jump to content

Python, handling multiple values from an index

Dujith

I'm trying to figure out something in python.

Programming a Homeserver to make 1 module that will handle converting RGB to xy

The problem i am running into is the way i have to input values and they dont seem to stay in memory.

 

      if index == 2 or index == 3 or index == 4:
              
        if index == 2:
          rgbrood = value
        elif index == 3:
          rgbgroen = value
        else:
          rgbblauw = value
      
      #Caculate RGB -> xy
      print ('pre temp')
      temp = (rgbrood+rgbgroen+rgbrood)
      print ('after temp')
      print (temp)  

In the homeserver i define an object which has any number of inputs. whenever an input changes it will trigger: def on_input_value(self, index, value):

The index is my input number and from it i can get a value (1 is my on/off and 5 is my brightness, those work)

When i run this it will stop at my debug print (pre temp) and will stop there. 

 

I started using python 2.7 this week and was able to fix many things by searching and watching youtube, but this has me stumped atm. 

I'm prob doing something very stupid and will be happy to hear how stupid ;) 

Link to comment
Share on other sites

Link to post
Share on other sites

rgbrood, rgbroen, rgbblauw are attributes of a class? Then do self.rgbrood = value etc... as you want to change the object's attribute, not define a new variable that happens to share the same name as one of the class's attributes.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, 79wjd said:

rgbrood, rgbroen, rgbblauw are attributes of a class? Then do self.rgbrood = value etc... as you want to change the object's attribute, not define a new variable that happens to share the same name as one of the class's attributes.

they are new, and were meant to hold the values so i can calculate with them.

Link to comment
Share on other sites

Link to post
Share on other sites

      if index == 2 or index == 3 or index == 4:
              
        if index == 2:
          rgbrood = 1
        elif index == 3:
          rgbgroen = 2
        else:
          rgbblauw = 3
      
      #Caculate RGB -> xy
      print ('pre temp')
      print (rgbrood)
      print ('after temp')

If i simplify the code i notice something

setting the index 2 value to 1 will set rgbrood to 1 as seen in that statement and it will print the value

However if i then set a value to the 3rd index it will no longer print the value of rgbrood ? so its volatile?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Dujith said:

      if index == 2 or index == 3 or index == 4:
              
        if index == 2:
          rgbrood = 1
        elif index == 3:
          rgbgroen = 2
        else:
          rgbblauw = 3
      
      #Caculate RGB -> xy
      print ('pre temp')
      print (rgbrood)
      print ('after temp')

If i simplify the code i notice something

setting the index 2 value to 1 will set rgbrood to 1 as seen in that statement and it will print the value

However if i then set a value to the 3rd index it will no longer print the value of rgbrood ? so its volatile?

Yes, it's volatile and will be lost as soon as it goes out of scope -- which it will once you leave the function.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, 79wjd said:

Yes, it's volatile and will be lost as soon as it goes out of scope -- which it will once you leave the function.

So whats normally used for storing values?

Link to comment
Share on other sites

Link to post
Share on other sites

@79wjd

Found it, need to set a global variable and call it as such

        if index == 2:
          global rgbrood
          rgbrood = 1

Thanks for pointing me in the correct direction :D i mean it btw, sometimes i just need key words and scope - variable did it for me.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Dujith said:

@79wjd

Found it, need to set a global variable and call it as such


        if index == 2:
          global rgbrood
          rgbrood = 1

Thanks for pointing me in the correct direction :D i mean it btw, sometimes i just need key words and scope - variable did it for me.

Glad to help.

 

It's fine to use globals, but there is usually a better solution. Globals are often used as a hacky solution to get around scope issues that were designed to exist for a reason. That's not to say you should never use globals, but it's not a bad habit to be in to design solutions that don't rely on them.

 

In this particular case, it would seem reasonable for those to be members of the class. Although, that is without having seen any of the code.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×