Increment Variable Name

Pablo Ziliani pablo at decode.com.ar
Wed Jan 23 21:14:48 EST 2008


David Brochu wrote:
> Pablo - Thanks for the reply.
>
> Basically what I am trying to do is pass each value from a list to the 
> following line of code (where XXX is where I need to pass each value 
> of the list
>
> tests = easygui.multchoicebox(message="Pick the test(s) you would like 
> to run from the list below."
> , title="Validation Tests"
> ,choices=[XXX])
>
> If i were to manually pass values to this line it would appear like :
> tests = easygui.multchoicebox(message="Pick the test(s) you would like 
> to run from the list below."
> , title="Validation Tests"
> ,choices=["Test 1","Test 2", "Test 3"])
>
> When I actually pass the list to the line of code, it works, however 
> it doesn't really split the list by element. The desired output of 
> this line of code would be a GUi that included a list consisting of 
> each element from the passed list. Right now I can only get it to 
> consist of a single element that contains every part of the list.
>
> Does this make sense?

David,

I am reposting this to the list to let other people provide you with a 
probable better answer.
I'm not really gui-oriented, so I might have a narrow sight of the problem.

I downloaded and tried the easygui script. Looks like your actual 
problem is it's lack of support for value/text pairs. So, depending on 
your actual needs I'd consider using a "less than easy" library.

If you still want/need to stick with this one, you will have to pass the 
index as part of the label and parse the answer afterwards. You could do 
something like:

# starts code:
import easygui

def getIndex(choice):
    return int(choice.split('.', 1)[0]) - 1

choices = ['Personality Test', 'Political Test', 'Driving Test', 'Icules 
Test']
labels = ["%s. %s" % item for item in enumerate(['dummy'] + choices)][1:]
answer = easygui.multchoicebox(
        message="Pick the test(s) you would like to run from the list 
below.",
        title="Validation Tests",
        choices=labels
)
tests = [getIndex(item) for item in answer]
# ends code

After this, the variable "tests" holds the list of the chosen indexes, 
that you can use to get any value from the actual objects you had in 
your original list.

A much simple approach (given the fact that you will probably never have 
two identical labels) is to simply use list.index, but given 
easygui.multchoicebox's nature, you will first have to sort your objects 
labels aphabetically:

# starts code:
import easygui
labels = ['Personality Test', 'Political Test', 'Driving Test', 'Icules 
Test']
labels.sort()
answer = easygui.multchoicebox(
        message="Pick the test(s) you would like to run from the list 
below.",
        title="Validation Tests",
        choices=labels
)
tests = [labels.index(item) for item in answer]
# ends code

Notice that these indexes refer to the labels order, not necessary your 
original objects'. Supposing you initially had a list of dictionaries, 
you will have to sort that list by the labels first. E.g.:

 >>> objects = [{'label': 'Personality Test', 'cargo': 'something'}, 
{'label': 'Political Test', 'cargo': 'something'}, {'label': 'Driving 
Test', 'cargo': 'something'}, {'label': 'Icules Test', 'cargo': 
'something'}]
 >>> from operator import itemgetter
 >>> objects.sort(key=itemgetter('label'))
 >>> objects
[{'cargo': 'something', 'label': 'Driving Test'}, {'cargo': 'something', 
'label': 'Icules Test'}, {'cargo': 'something', 'label': 'Personality 
Test'}, {'cargo': 'something', 'label': 'Political Test'}]
 >>> labels = [obj['label'] for obj in objects]
 >>> labels
['Driving Test', 'Icules Test', 'Personality Test', 'Political Test']

HTH,
Pablo



More information about the Python-list mailing list