Python Forum
Python Variable manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Variable manipulation
#1
I am going to assume this is simple and the issue is that I am missing the obvious.

I have variables defined like so:
# First I define all the variables
    desc_part_number = 'not_selected'
    asc_part_number = 'not_selected'

    desc_product_serial_number = 'not_selected'
    asc_product_serial_number = 'not_selected'

    desc_result_arrow = 'not_selected'
    asc_result_arrow = 'not_selected'

    desc_date_time = 'not_selected'
    asc_date_time = 'not_selected'

    desc_location_of_test = 'not_selected'
    asc_location_of_test = 'not_selected'

    desc_test_equipment_set = 'not_selected'
    asc_test_equipment_set = 'not_selected'

# Now I sees which column was selected by looking at other variables defined earlier in the function
    selected_column = my_order_direction+'_'+order_column
The combination of "my_order_direction+'_'+order_column " will make selected_column equal to one of the previously defined variables.

What i want to now do is use the selected_column to change the variable it is now equal to, so that it is now equal to 'selected'

E.g. If asc_location_of_test was click then
selected_column = asc_location_of_test
And the variable asc_location_of_test would be changed
 asc_location_of_test = 'selected'
# The rest of the variables would still equal 'not_selected'
Ideally I do not want to use a large combination of if statement
Reply
#2
You can do that with
vars()[selected_column] = 'selected'
Edit:
I have just browsed a bit and saw warnings against using vars() to modify variables. A more recommended approach seems to be using setattr():
import sys

thismodule = sys.modules[__name__]

setattr(thismodule, selected_column, selected)
Source
Reply
#3
Why you don't want to dynamically create variables
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Yes, use a dictionary or a named tuple instead of variables.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks for the help, Dicts was defiantly the way to look at it. And even though this is probably not the most refined answer, here is what I will be using:

   
    arrow_dict = {'desc_part_number': 'not_selected',
                    'asc_part_number': 'not_selected',
                    'desc_product_serial_number': 'not_selected',
                    'asc_product_serial_number': 'not_selected',
                    'desc_result': 'not_selected',
                    'asc_result': 'not_selected',
                    'desc_date_time': 'not_selected',
                    'asc_date_time': 'not_selected',
                    'desc_location_of_test': 'not_selected',
                    'asc_location_of_test': 'not_selected',
                    'desc_test_equipment_set': 'not_selected',
                    'asc_test_equipment_set': 'not_selected'
                   }

    selected_column = my_order_direction+'_'+order_column
    print('************************************'+selected_column+'*************************************')
    arrow_dict.update({selected_column: 'selected'})

    desc_part_number_final = arrow_dict['desc_part_number']
    asc_part_number_final = arrow_dict['asc_part_number']
    desc_product_serial_number_final = arrow_dict['desc_product_serial_number']
    asc_product_serial_number_final = arrow_dict['asc_product_serial_number']
    desc_result_final = arrow_dict['desc_result']
    asc_result_final = arrow_dict['asc_result']
    desc_date_time_final = arrow_dict['desc_date_time']
    asc_date_time_final = arrow_dict['asc_date_time']
    desc_location_of_test_final = arrow_dict['desc_location_of_test']
    asc_location_of_test_final = arrow_dict['asc_location_of_test']
    desc_test_equipment_set_final = arrow_dict['desc_test_equipment_set']
    asc_test_equipment_set_final = arrow_dict['asc_test_equipment_set']
Reply
#6
could you explain what you try to do? This doesn't look much different from your initial idea. Maybe even worse...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Yes, why assign the string to a variable that is the same as the string, and then use the variable instead of the string? You're typing the full column name out three times, when you could just be typing it out once.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
As i stated it is defiantly not the most refined way of completing this task. I will explain what my code does and reasons for it, I am open to suggestions on how to refine it.

The first section of the code sets up the dict and makes all the values equal to 'not_selected'

The 2nd section uses predefined variables to change the value of the selected column to be 'selected'

The final part of the code introduces new variables which cut up the dict into multiple values as the main purpose of this code is to introduce dynamically variables to my html so that I can alter the css of a html page. Its long winded and the file has many processes in it so would be useless if i posted it here.

And while typing all of this i see my error and maybe the cause of your confusion, why not just have arrow_dict['desc_part_number']. Which is what i have just changed the code to be. Maybe this makes it easier to understand?
Reply
#9
(Aug-14-2018, 02:58 PM)KirkmanJ Wrote: Maybe this makes it easier to understand?
Not really looking at what you have done.
Here a take on it with classes,which is flexible on changing state,
can also copy object so not have bunch of final variables.
class Foo:
    def __init__(self,  desc_part_number='not_selected', asc_part_number='not_selected'):
        self.desc_part_number= desc_part_number
        self.asc_part_number = asc_part_number

class Bar:
    def __init__(self):
        self.part = Foo()

    def __call__(self, part_choice):
        if self.part.__dict__[part_choice] == 'not_selected':
            self.part.__dict__[part_choice] = 'selected'
        else:
            if self.part.__dict__[part_choice] == 'selected':
                self.part.__dict__[part_choice] = 'not_selected'

    @property
    def reset_all(self):
        self.part = Foo()
Test:
>>> selected_column = Bar()
>>> selected_column.part.desc_part_number
'not_selected'
>>> selected_column.part.asc_part_number
'not_selected'
>>> 
>>> # Change state
>>> part = 'desc_part_number'
>>> selected_column(part)
>>> selected_column.part.desc_part_number
'selected'
>>> selected_column.part.asc_part_number
'not_selected'

>>> # Back the same command
>>> selected_column(part)
>>> selected_column.part.desc_part_number
'not_selected'
>>> selected_column.part.asc_part_number
'not_selected'
Test change both and reset:
>>> selected_column = Bar()
>>> selected_column('desc_part_number')
>>> selected_column('asc_part_number')
>>> selected_column.part.desc_part_number
'selected'
>>> selected_column.part.asc_part_number
'selected'
>>> selected_column.reset_all
>>> selected_column.part.desc_part_number
'not_selected'
>>> selected_column.part.asc_part_number
'not_selected'
Copy:
>>> selected_column = Bar()
>>> selected_column('desc_part_number')
>>> selected_column.part.desc_part_number
'selected'
>>> selected_column.part.asc_part_number
'not_selected'
>>> 
>>> import copy
>>> new_obj = copy.copy(selected_column)
>>> new_obj.part.desc_part_number
'selected'
>>> new_obj.part.asc_part_number
'not_selected'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Manipulation Tbot1000 7 2,796 Aug-31-2020, 06:40 PM
Last Post: deanhystad
  Python re.sub text manipulation on matched contents before substituting xilex 2 2,068 May-19-2020, 05:42 AM
Last Post: xilex
  [split] Python Pillow - Photo Manipulation keegan_010 1 2,942 Oct-11-2018, 09:57 AM
Last Post: Larz60+
  Python Pillow - Photo Manipulation keegan_010 2 2,871 Oct-11-2018, 03:49 AM
Last Post: keegan_010

Forum Jump:

User Panel Messages

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