Skip to content

Instantly share code, notes, and snippets.

@arturaugusto
Last active September 4, 2015 19:50
Show Gist options
  • Select an option

  • Save arturaugusto/880e5a0ab554e516e753 to your computer and use it in GitHub Desktop.

Select an option

Save arturaugusto/880e5a0ab554e516e753 to your computer and use it in GitHub Desktop.
Sample executing classes
from lantz import Feat
from lantz.messagebased import MessageBasedDriver
# Part 1: resources
# Create dict that will hold the classes
# Set string that contain the class definition
class_code = []
class_code.append({
"id": "abc",
"code": '''class Driver(MessageBasedDriver):
"""docstring for Driver"""
DEFAULTS = {'COMMON': {'write_termination': '\\n',
'read_termination': '\\n'}}
@Feat()
def idn(self):
return self.query('*IDN?')
def say_hi(self):
print("HI abc!")'''
})
class_code.append({
"id":"xyz",
"code": '''class Driver(MessageBasedDriver):
"""docstring for Driver"""
def say_hi(self):
print("HI xyz!")'''
})
workbench = dict()
for item in class_code:
# Execute the class
exec(item["code"])
# Define class on dict
try:
workbench[item["id"]] = Driver("GPIB0::9::INSTR")
except:
pass
#raise e
# Part 2
# Execute the code that uses created objects
code = '''
# Check if a_instance have the say_hy method
print("say_hi" in dir(workbench['abc']))
# Execute methods
inst = workbench['abc']
inst.initialize()
print(vars(inst))
print(inst.idn)
inst.finalize()
'''
# Exec code
#exec(code)
# Execute methods
class AutomationSteps(object):
"""docstring for AutomationSteps"""
def __init__(self, workbench):
super(AutomationSteps, self).__init__()
self.workbench = workbench
self.initialize()
def initialize(self):
inst = self.workbench['abc']
inst.bla = "lala"
print(vars(inst))
inst.initialize()
print(inst.idn)
inst.finalize()
automation = AutomationSteps(workbench)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment