Iteratively define functions /u/TTheRake Python Education

Hello guys!

I use python to control measurement instruments for which i often need to write the driver myself. Every time, i get to deal with the same problem. The instruments usually have many channels with different kind of measurements (for instance ch 1 to 16 with measurement x,y,r,theta). For any combination of the two i have to define a function to get the value. It’s of course a very tedious job and i spend a lot of time copypasting the same function to just change a couple of characters. Does anyone know a way to iteratively define functions with a for cycle or something similar?
The best solution i got is to generate the string that define my functions with a for cycle and then paste it in the code. Not very pythonic but hey it does the job

Thanks in advance!

Example of what i need to do

class mcl_lia_class(Instrument): def __init__(self, name: str,git_instance, **kwargs) -> None: super().__init__(name,**kwargs) self.git_instance=git_instance #frankenstein code to get a qcodes driver from the old driver def x_a1(self): return self.git_instance.L1.x[0] def x_a2(self): return self.git_instance.L1.x[1] def x_b1(self): return self.git_instance.L1.x[2] def x_b2(self): return self.git_instance.L1.x[3] def y_a1(self): return self.git_instance.L1.y[0] def y_a2(self): return self.git_instance.L1.y[1] def y_b1(self): return self.git_instance.L1.y[2] def y_b2(self): return self.git_instance.L1.y[3] def r_a1(self): return self.git_instance.L1.r[0] 

submitted by /u/TTheRake
[link] [comments]

​r/learnpython Hello guys! I use python to control measurement instruments for which i often need to write the driver myself. Every time, i get to deal with the same problem. The instruments usually have many channels with different kind of measurements (for instance ch 1 to 16 with measurement x,y,r,theta). For any combination of the two i have to define a function to get the value. It’s of course a very tedious job and i spend a lot of time copypasting the same function to just change a couple of characters. Does anyone know a way to iteratively define functions with a for cycle or something similar? The best solution i got is to generate the string that define my functions with a for cycle and then paste it in the code. Not very pythonic but hey it does the job Thanks in advance! Example of what i need to do class mcl_lia_class(Instrument): def __init__(self, name: str,git_instance, **kwargs) -> None: super().__init__(name,**kwargs) self.git_instance=git_instance #frankenstein code to get a qcodes driver from the old driver def x_a1(self): return self.git_instance.L1.x[0] def x_a2(self): return self.git_instance.L1.x[1] def x_b1(self): return self.git_instance.L1.x[2] def x_b2(self): return self.git_instance.L1.x[3] def y_a1(self): return self.git_instance.L1.y[0] def y_a2(self): return self.git_instance.L1.y[1] def y_b1(self): return self.git_instance.L1.y[2] def y_b2(self): return self.git_instance.L1.y[3] def r_a1(self): return self.git_instance.L1.r[0] submitted by /u/TTheRake [link] [comments] 

Hello guys!

I use python to control measurement instruments for which i often need to write the driver myself. Every time, i get to deal with the same problem. The instruments usually have many channels with different kind of measurements (for instance ch 1 to 16 with measurement x,y,r,theta). For any combination of the two i have to define a function to get the value. It’s of course a very tedious job and i spend a lot of time copypasting the same function to just change a couple of characters. Does anyone know a way to iteratively define functions with a for cycle or something similar?
The best solution i got is to generate the string that define my functions with a for cycle and then paste it in the code. Not very pythonic but hey it does the job

Thanks in advance!

Example of what i need to do

class mcl_lia_class(Instrument): def __init__(self, name: str,git_instance, **kwargs) -> None: super().__init__(name,**kwargs) self.git_instance=git_instance #frankenstein code to get a qcodes driver from the old driver def x_a1(self): return self.git_instance.L1.x[0] def x_a2(self): return self.git_instance.L1.x[1] def x_b1(self): return self.git_instance.L1.x[2] def x_b2(self): return self.git_instance.L1.x[3] def y_a1(self): return self.git_instance.L1.y[0] def y_a2(self): return self.git_instance.L1.y[1] def y_b1(self): return self.git_instance.L1.y[2] def y_b2(self): return self.git_instance.L1.y[3] def r_a1(self): return self.git_instance.L1.r[0] 

submitted by /u/TTheRake
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *