Python Forum
Alternative to dynamic variable names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alternative to dynamic variable names
#1
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!
Reply
#2
Use a dictionary
Reply
#3
(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.
Reply
#4
my_dict = {}
rm = visa.ResourceManager()
my_dict["lakeshore"] = rm.open_resource('GPIB0::11::INSTR')
my_dict["lakeshore"].write("command")
Reply
#5
(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 !!!!!!!!
Reply
#6
I had edited my reply to include an example of accessing write in the time that you were adding a reply
Reply
#7
Thank you a lot!
Reply
#8
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.
Reply
#9
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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating arbitrary local variable names Skaperen 9 2,989 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 3,121 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Use dynamic variable from parallel running python script Sicksym 0 2,724 May-15-2020, 02:52 PM
Last Post: Sicksym
  Can I use iteration to create variable names? Mark17 8 12,495 Oct-17-2019, 06:05 AM
Last Post: perfringo
  How do I create a Dynamic Variable? Nwb 1 3,707 Jun-10-2018, 11:50 AM
Last Post: volcano63
  2D Array/List OR using variables in other variable names? IAMK 4 5,920 Apr-16-2018, 09:09 PM
Last Post: IAMK
  Creating Dynamic Variable Names Dragonexpert 3 10,575 Oct-22-2016, 02:17 PM
Last Post: Dragonexpert

Forum Jump:

User Panel Messages

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