Extract Variable Values

This is the place for queries that don't fit in any of the other categories.

Extract Variable Values

Postby mcflie1994 » Thu Oct 24, 2013 11:05 pm

I'm trying to have the return return the value of the variable named in the comm variable. so if comm = 'on' then i want the return statement to be: return (baud, data, parity, stop, on). Is this achievable or do I have to use conditional statements? I'm trying t avoid a lot of if: elif: blocks because there are potentially 20 different values of comm. Thank you for your time.

Comm is a variable passed from a calling program. These functions are being designed to be callable from a parent script. Then this returns the five values to the parent to be manipulated further. Possible values passed to comm in this example are 'on' and 'off'.

Code: Select all
def InFocus_IN146(comm):
    baud = 115200
    data = 8
    parity = "N"
    stop = 1
    on = '(PWR1)'
    off = '(PWR0)'
    return (baud, data, parity, stop, %r) %(comm)


p.s. I know the %r does not work this way, but it was the easiest way to illustrate what I wanted to do. Thank you
Last edited by micseydel on Thu Oct 24, 2013 11:38 pm, edited 1 time in total.
Reason: Locked OP.
mcflie1994
 
Posts: 2
Joined: Thu Oct 24, 2013 10:54 pm

Re: Extract Variable Values

Postby metulburr » Thu Oct 24, 2013 11:39 pm

Possible values passed to comm in this example are 'on' and 'off'.


i think this is what you mean?

Code: Select all
def InFocus_IN146(comm):
    baud = 115200
    data = 8
    parity = "N"
    stop = 1
    on = '(PWR1)'
    off = '(PWR0)'
    return (baud, data, parity, stop, '{}'.format(comm))

print(InFocus_IN146('in'))
print(InFocus_IN146('off'))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1560
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Extract Variable Values

Postby micseydel » Thu Oct 24, 2013 11:50 pm

I think you want a dictionary
Code: Select all
COMMS = {
    "on": "(PWR1)",
    "off: "(PWR0)"
}

def InFocus_IN146(comm):
    baud = 115200
    data = 8
    parity = "N"
    stop = 1
    return (baud, data, parity, stop, COMMS[comm])
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1487
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Extract Variable Values

Postby mcflie1994 » Fri Oct 25, 2013 2:17 am

I think you want a dictionary


The issue with a dictionary is the values of on and off are different depending on the function and I'm trying not to split the following into separate files.

Code: Select all
def InFocus_IN146(comm):
   baud = 115200
   data = 8
   parity = 'N'
   stop = 1
   on = '(PWR1)'
   off = '(PWR0)'
   blks = '(BSS3)'
   blkon = '(BLK1)'
   blkoff = '(BLK0)'
   return (baud, data, parity, stop, |value of comm here|)

def Mitsubishi_200(comm):
   #Supported Projectors
   #EW270U
   #EW230U-ST
   #EX240U
   #ES200U
   baud = 115200
   data = 8
   parity = 'N'
   stop = 1
   on = '\r*pow=on#\r'
   off = '\r*pow=off#\r'
   blkon = '\r*blank=on#\r'
   blkoff = '\r*blank=off#\r'
   return (baud, data, parity, stop, |value of comm here|)

def Epson_VP21(comm):
   #ESC/VP21
   baud = 9600
   data = 8
   parity = 'N'
   stop = 1
   on = 'PWR ON\r'
   off = 'PWR OFF\r'
   blkon = 'MUTE ON\r'
   blkoff = 'MUTE OFF\r'
   return (baud, data, parity, stop, |value of comm here|)


As for metulburr's suggestion:
i think this is what you mean?

Code: Select all
    def InFocus_IN146(comm):
        baud = 115200
        data = 8
        parity = "N"
        stop = 1
        on = '(PWR1)'
        off = '(PWR0)'
        return (baud, data, parity, stop, '{}'.format(comm))

    print(InFocus_IN146('in'))
    print(InFocus_IN146('off'))



When I implement this suggestion i get the value of comm back, or on, not the value of on or (PWR1).

My full code is across two files, the first which defines the functions to be called by the main script is at the top of this post. The other that calls those functions is below. for simplicity of testing i have defined the passed variable in this file to on instead of prompting for a value.

Code: Select all
from test5a import *
#import test5a

comm = 'on'

baud, data, parity, stop, msg = InFocus_IN146(comm)

baud = int(baud)
data = int(data)
stop = float(stop)

print baud
print data
print parity
print stop
print msg
mcflie1994
 
Posts: 2
Joined: Thu Oct 24, 2013 10:54 pm

Re: Extract Variable Values

Postby micseydel » Fri Oct 25, 2013 3:46 am

You can still have a dictionary in each function. From what I see, what you have to do is just terrible and you have to work with it. Such is life.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1487
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Extract Variable Values

Postby metulburr » Fri Oct 25, 2013 4:19 am

When I implement this suggestion i get the value of comm back, or on, not the value of on or (PWR1).

i misunderstood. What you want then is micseydel's suggestion
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1560
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Extract Variable Values

Postby Kebap » Fri Oct 25, 2013 10:27 am

Maybe this can help
http://www.catb.org/esr/faqs/smart-questions.html#goal
Describe the goal, not the step

If you are trying to find out how to do something (as opposed to reporting a bug), begin by describing the goal. Only then describe the particular step towards it that you are blocked on.

Often, people who need technical help have a high-level goal in mind and get stuck on what they think is one particular path towards the goal. They come for help with the step, but don't realize that the path is wrong. It can take substantial effort to get past this.

Stupid: How do I get the color-picker on the FooDraw program to take a hexadecimal RGB value?

Smart: I'm trying to replace the color table on an image with values of my choosing. Right now the only way I can see to do this is by editing each table slot, but I can't get FooDraw's color picker to take a hexadecimal RGB value.

The second version of the question is smart. It allows an answer that suggests a tool better suited to the task.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 401
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests