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 !
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
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 !!!!
(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.
(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.
(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()