Python Forum

Full Version: Alternative to dynamic variable names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello !

This is a clasical example of pyvisa program:
import pyvisa
rm = pyvisa.ResourceManager()
rm.list_resources()
('ASRL1::INSTR', 'ASRL2::INSTR', 'GPIB0::12::INSTR')
inst = rm.open_resource('GPIB0::12::INSTR')
print(inst.query("*IDN?"))
Now, the problem is if I have a text file with multiple names and address of various instruments, something like this:

keithley,GPIB0::12::INSTR
lakeshore,GPIB0::11::INSTR
etc
How can I make the program from above to work without to dynamically create the variable with the name and address of each instrument from the file text?

I want to avoid this type of variable declaration:
rm = visa.ResourceManager()
vars()["lakeshore"] = rm.open_resource('GPIB0::11::INSTR')
Is possible?
Thank you!
Use a dictionary
(Jun-20-2020, 12:44 PM)Yoriz Wrote: [ -> ]Use a dictionary

I know how to use a dictionary but in this case, I have no idea how to do it. Because once I create the name of the instrument, then I need to acces its properties like instr.write("command"), inst.read(), etc.
my_dict = {}
rm = visa.ResourceManager()
my_dict["lakeshore"] = rm.open_resource('GPIB0::11::INSTR')
my_dict["lakeshore"].write("command")
(Jun-20-2020, 12:47 PM)Yoriz Wrote: [ -> ]
my_dict = {}
rm = visa.ResourceManager()
my_dict["lakeshore"] = rm.open_resource('GPIB0::11::INSTR')

And in this case how acces its properties? Like:
lakeshore.write("*IDN?")
print(lakeshore.read())

Ok!
Thank you !!!!!!!!
I had edited my reply to include an example of accessing write in the time that you were adding a reply
Thank you a lot!
From the performance point of view which method is faster: dictionary or vars()[]....?

I test the speed:
import time
my = {}

start_time = time.time()
my["clock"] = time
print(my["clock"].localtime())
print("--- %s seconds ---" % (time.time() - start_time))

start_time = time.time()
vars()["clock"] = time
print(vars()["clock"].localtime())
print("--- %s seconds ---" % (time.time() - start_time))
Output:
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=20, tm_hour=17, tm_min=40, tm_sec=0, tm_wday=5, tm_yday=172, tm_isdst=1) --- 0.02257513999938965 seconds --- time.struct_time(tm_year=2020, tm_mon=6, tm_mday=20, tm_hour=17, tm_min=40, tm_sec=0, tm_wday=5, tm_yday=172, tm_isdst=1) --- 0.0029897689819335938 seconds ---
So, the dictionary method is 10x slower than vars()[] method.
import time
my = {}
my["clock"] = time

def test1():
    my["clock"] = time
    my["clock"].localtime()

def test2():
    vars()["clock"] = time
    vars()["clock"].localtime()


if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test1()", setup="from __main__ import test1"))
    print(timeit.timeit("test2()", setup="from __main__ import test2"))
Output:
1.4009026 1.504943
Seriously
from timeit import timeit
import time


r = timeit("vars()['clock'] = time", "from __main__ import time")
print(r)

my = {}

r = timeit("my['clock'] = time", "from __main__ import time, my")

print(r)
Output:
0.1516461489991343 0.022416326999518787
Dict is faster.
Pages: 1 2