Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vars and getattr problem
#1
Hello everybody,
I have the following problem:
This code works very well:


rm = visa.ResourceManager()
lakeshore = rm.open_resource('ASRL1::INSTR')
lakeshore.baud_rate = 1200
lakeshore.data_bits = 7
lakeshore.stop_bits = constants.StopBits.one
lakeshore.parity = constants.Parity.odd
What I want is to write the same code but in diferent way:
rm = visa.ResourceManager()
vars()["lakeshore"] = rm.open_resource('ASRL1::INSTR')
which is works, but if I want to add some properties of the serial conection like:
getattr(vars()["lakeshore"], "baud_rate") = "1200"
getattr(vars()["lakeshore"], "data_bits") = "7"
getattr(vars()["lakeshore"], "stop_bits") = getattr("constants.StopBits","one")
getattr(vars()["lakeshore"], "stop_bits") = getattr("constants.Parity","odd")
The program give an error like:

Error:
Can't assign to function call
I there a way to solve this problem ?
Thank you in advance !
Reply
#2
Yes there is a way, use setattr()
setattr(vars()["lakeshore"], "baud_rate", 1200)
You can also write
vars()["lakeshore"].baud_rate = 1200
The following syntax is impossible in python:
some_expression(some_args) = some_value
Reply
#3
Thank You Gribouillis !!!
I will come back with results !

It works !!!

vars(self)["lakeshore"].baud_rate = 1200
vars(self)["lakeshore"].data_bits = 7
vars(self)["lakeshore"].stop_bits = getattr(constants.StopBits,"one")
vars(self)["lakeshore"].parity = getattr(constants.Parity,"odd")
Thank You again !!!!
Reply
#4
(Aug-28-2018, 08:13 AM)catosp Wrote: What I want is to write the same code but in diferent way:
any particular reason why you want to complicate things?
i.e. it makes sense to use getattr and setattr in some cases, e.g. if you want to access properties dynamically, but it doesn't make much sense to use vars() in this case.
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
#5
(Aug-28-2018, 11:44 AM)buran Wrote:
(Aug-28-2018, 08:13 AM)catosp Wrote: What I want is to write the same code but in diferent way:
any particular reason why you want to complicate things?
i.e. it makes sense to use getattr and setattr in some cases, e.g. if you want to access properties dynamically, but it doesn't make much sense to use vars() in this case.

I work now on a project (which will be open source) that simplify the laboratory measurements based on PyVisa (GPIB, Serial, Usb, etc.). For this, is necessary to create a pseudo-language embedded in universal GUI interface. The results will be something like LabView but without graphical programing.
Reply
#6
(Aug-28-2018, 12:19 PM)catosp Wrote: I work now on a project (which will be open source) that simplify the laboratory measurements based on PyVisa (GPIB, Serial, Usb, etc.). For this, is necessary to create a pseudo-language embedded in universal GUI interface. The results will be something like LabView but without graphical programing.
my question was why this
vars(self)["lakeshore"].baud_rate = 1200
vars(self)["lakeshore"].data_bits = 7
vars(self)["lakeshore"].stop_bits = getattr(constants.StopBits,"one")
vars(self)["lakeshore"].parity = getattr(constants.Parity,"odd")
and not

self.lakeshore.baud_rate = 1200
self.lakeshore.data_bits = 7
self.lakeshore.stop_bits = constants.StopBits.one
self.lakeshore.parity = constants.Parity.odd
i.e.
something like this makes sense, but in your case you use hard-coded attribute names...
for key, value in some_mapping: # some_maping, like some_dict.items(), container with two-elelement tuples/lists, etc.
    setattr(some_object, key, value)

By the way, this should also work

setattr(self.lakeshore, 'baud_rate', 1200)
setattr(self.lakeshore, 'data_bits', 7)
setattr(self.lakeshore, 'stop_bits', getattr(constants.StopBits,"one"))
setattr(self.lakeshore 'parity', constants.Parity.odd)
in the last line I removed also the getattr used.

Still I don't understand why insist on use of serattr(), getattr() and vars()
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  vars() can't be used in list interpretation? Cheng 3 2,106 Jun-22-2021, 11:59 AM
Last Post: snippsat
  question about getattr() ryfoa6 3 2,167 Apr-07-2020, 07:16 PM
Last Post: deanhystad
  About getattr() MingyuanLuo 7 5,737 Mar-20-2019, 01:46 PM
Last Post: ichabod801
  using vars from one file to match lines in another gt76_noobster 3 2,613 Jan-30-2019, 05:34 PM
Last Post: ichabod801
  A help whit vars and strings dhoyosg 10 5,321 May-05-2018, 08:52 AM
Last Post: dhoyosg
  What should I name these vars RickyWilson 1 2,207 Dec-03-2017, 03:49 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

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