How to use signal_resource method in localstack

Best Python code snippet using localstack_python

laser.py

Source:laser.py Github

copy

Full Screen

1"""2ControlAxis for use with a laser controlled by a power supply and a signal generator3"""4import time5import visa6from visa import VisaIOError7from pyforms import BaseWidget8from pyforms.Controls import ControlNumber, ControlCombo9from framework import ControlAxis, LightSource10RESOURCE_MANAGER = visa.ResourceManager()11LASER = None12def get_devices():13 """14 Gets pyvisa resources to be used for a laser15 """16 resources = RESOURCE_MANAGER.list_resources()17 devices = {"Power Supply": [], "Signal Generator": []}18 for resource in resources:19 try:20 open_resource = RESOURCE_MANAGER.open_resource(resource)21 open_resource.timeout = 10022 name = open_resource.query("*IDN?").strip()23 open_resource.close()24 except VisaIOError as err:25 print("Could not talk to", resource)26 print(err)27 continue28 # Only add if it is a known power supply or signal generator29 if 'AFG-3021' in name:30 devices["Signal Generator"].append((name, resource))31 elif 'GPD-4303S' in name:32 devices["Power Supply"].append((name, resource))33 return devices34DEVICES = get_devices()35WIDGET = None36def laser_custom_config():37 """38 Get the GUI config to configure the laser39 The GUI is the same for each laser axis and for the laser lightsource40 """41 global WIDGET42 if WIDGET is None:43 widget = BaseWidget("Laser Config")44 widget.power_supply = ControlCombo(45 label="Power Supply"46 )47 widget.power_supply += ('None', None)48 for power in DEVICES['Power Supply']:49 widget.power_supply += power50 widget.power_supply.current_index_changed_event = update_laser51 widget.power_channel = ControlNumber(52 label="Power Supply Channel",53 default=1,54 minimum=1,55 maximum=456 )57 widget.signal_generator = ControlCombo(58 label="Signal Generator"59 )60 widget.signal_generator += ('None', None)61 for signal in DEVICES['Signal Generator']:62 widget.signal_generator += signal63 widget.signal_generator.current_index_changed_event = update_laser64 widget.formset = [65 "h5:Laser Using",66 'power_supply',67 'power_channel',68 'signal_generator',69 "(All laser axis use the same laser)"70 ]71 WIDGET = widget72 return WIDGET73def update_laser(_):74 """75 Update the current laser with the power supply and signal generator76 from the GUI77 """78 global LASER79 global WIDGET80 power = WIDGET.power_supply.value81 signal = WIDGET.signal_generator.value82 channel = WIDGET.power_channel.value83 print(power)84 print(signal)85 # Only update if both are selected86 if power is not None and power != 'None' and signal is not None and signal != 'None':87 print("Making Laser")88 power_resource = RESOURCE_MANAGER.open_resource(power)89 signal_resource = RESOURCE_MANAGER.open_resource(signal)90 LASER = Laser(power_resource, channel, signal_resource)91def cleanup():92 global WIDGET93 if isinstance(WIDGET, BaseWidget):94 for item in WIDGET.power_supply.items:95 if isinstance(item, tuple):96 resource = item[1]97 if isinstance(resource, visa.Resource):98 resource.close()99 RESOURCE_MANAGER.close()100class Laser:101 """102 Controls the laser and holds the LaserPowerAxis and Laser Frequency Axis103 """104 power_resource = None105 signal_resource = None106 power_channel = 0107 enabled_voltage = 8108 disabled_voltage = 0109 max_current = 0.5110 low_signal = -0.2111 high_signal = 1.0112 power = 0.0113 frequency = 0.0114 enabled = False115 delay = 0.01116 last_write = 0117 # Whether to use the external trigger to ensure outpout is synced118 # Requires wiring the power supply output to the external trigger port of the AFG119 _ext_trigger = True120 def __init__(self, power_resource, power_channel, signal_resource):121 """122 Create a Laser123 :param power_resource: The pyvisa resource for the laser power supply (Must be a GPD-4303S for now)124 :param power_channel: The channel on the power supply to use125 :param signal_resource: The pyvisa resource for the laser signal generator (Must be an AFG-3021 for now)126 """127 print("Signal Generator:", signal_resource.query('*IDN?'))128 print("Power Supply:", power_resource.query('*IDN?'))129 self.power_resource = power_resource130 self.power_channel = power_channel131 self.signal_resource = signal_resource132 # Turn on the outputs133 self.power_resource.write('OUT1')134 # Set the voltage to 8 volts135 self.power_resource.write('VSET{0}:8'.format(self.power_channel))136 # Set the current to 0.5 amps137 self.power_resource.write('ISET{0}:0.5'.format(self.power_channel))138 # Turn on the signal139 self.signal_resource.write('OUTPUT ON')140 self.set_power(1)141 def __del__(self):142 self.enabled = False143 self.offset = self.low_signal144 self.update_laser()145 self.power_resource.close()146 self.signal_resource.close()147 def update_laser(self):148 """149 Updates the laser to current power, frequency, and enabled150 """151 if time.time() - self.last_write > self.delay:152 if self.frequency > 0:153 amplitude = self.power * \154 (self.high_signal - self.low_signal) * 2155 offset = self.low_signal156 if self._ext_trigger:157 self.signal_resource.write(158 "SOURCE1:APPLY:SQUARE {0}HZ,{1},{2}".format(self.frequency, amplitude, offset))159 self.signal_resource.write("SOURCE1:BURST:STATE ON")160 self.signal_resource.write(161 "SOURCE1:APPLY:SQUARE {0}HZ,{1},{2}".format(self.frequency, amplitude, offset))162 self.signal_resource.write("SOURCE1:BURST:MODE GATE")163 self.signal_resource.write("SOURCE1:BURST:PHASE 0")164 self.signal_resource.write("SOURCE1:BURST:TRIGGER EXT")165 else:166 self.signal_resource.write(167 "SOURCE1:APPLY:SQUARE {0}HZ,{1},{2}".format(self.frequency, amplitude, offset))168 else:169 offset = self.power * \170 (self.high_signal - self.low_signal) + self.low_signal171 self.signal_resource.write("SOURCE1:BURST:STATE OFF")172 self.signal_resource.write(173 "SOURCE1:APPLY:DC DEFAULT,DEFAULT,{0}".format(offset))174 if self.enabled:175 self.power_resource.write("OUT1")176 else:177 self.power_resource.write("OUT0")178 self.last_write = time.time()179 def set_enabled(self, enable=True):180 """181 Enables or disables the power supply182 :param enable: Enables if True, disables if false183 :return: nothing184 """185 self.enabled = enable186 time.sleep(0.1)187 self.update_laser()188 def get_enabled(self):189 """190 Gets if the laser is enabled191 """192 return self.enabled193 def set_frequency(self, frequency):194 """195 Sets the frequency to pulse the laser at196 :param frequency: The frequency in hertz197 :return: nothing198 """199 self.frequency = frequency200 self.update_laser()201 def get_frequency(self):202 """203 Get the frequency204 """205 return self.frequency206 def set_power(self, power):207 """208 Sets the power to the laser by adjusting the amplitude and DC offset of the signal209 :param power: The power to apply between 0.0 and 1.0210 :return: nothing211 """212 self.power = power213 self.update_laser()214 def get_power(self):215 """216 Get the power217 """218 return self.power219 def get_signal_generator(self):220 """221 Returns the signal generator resource in use222 """223 return self.signal_resource224 def get_power_supply(self):225 """226 Returns the power supply resource in use227 """228 return self.power_resource229class LaserLightSource(LightSource):230 """231 Can enable/disable the laser232 """233 def get_custom_config(self):234 return laser_custom_config()235 def get_enabled(self):236 global LASER237 if isinstance(LASER, Laser):238 return LASER.get_enabled()239 else:240 return False241 def set_enabled(self, enable=True):242 global LASER243 print("Setting Laser Enabled", enable)244 if isinstance(LASER, Laser):245 LASER.set_enabled(enable)246class LaserPowerAxis(ControlAxis):247 """248 A ControlAxis to control the power to the laser249 """250 def get_custom_config(self):251 return laser_custom_config()252 def get_current_value(self):253 global LASER254 if isinstance(LASER, Laser):255 return LASER.get_power()256 else:257 return 0.0258 def _write_value(self, value):259 global LASER260 if isinstance(LASER, Laser):261 LASER.set_power(value)262 print("Setting laser power to: {}".format(value))263 def get_units(self):264 return "0-1"265class LaserFequencyAxis(ControlAxis):266 """267 A ControlAxis to control the frequency to the laser268 """269 def get_custom_config(self):270 return laser_custom_config()271 def get_current_value(self):272 global LASER273 if isinstance(LASER, Laser):274 return LASER.get_frequency()275 else:276 return 0.0277 def _write_value(self, value):278 global LASER279 if isinstance(LASER, Laser):280 LASER.set_frequency(value)281 print("Setting laser frequency to: {}".format(value))282 def get_units(self):...

Full Screen

Full Screen

test_cloudformation.py

Source:test_cloudformation.py Github

copy

Full Screen

1from botocore.exceptions import ClientError2from mock import patch3from spacel.aws import CloudFormationSignaller4from test.aws import MockedClientTest, INSTANCE_ID5STACK_NAME = 'stack_name'6RESOURCE_NAME = 'resource_name'7class TestCloudFormationSignaller(MockedClientTest):8 def setUp(self):9 super(TestCloudFormationSignaller, self).setUp()10 self.signaller = CloudFormationSignaller(self.clients, INSTANCE_ID)11 self.manifest.cf_signal = {STACK_NAME: RESOURCE_NAME}12 def test_notify_not_enabled(self):13 self.manifest.cf_signal = {}14 self.signaller.notify(self.manifest)15 self.cloudformation.signal_resource.assert_not_called()16 def test_notify(self):17 self.signaller.notify(self.manifest)18 self.cloudformation.signal_resource.assert_called_with(19 StackName=STACK_NAME,20 LogicalResourceId=RESOURCE_NAME,21 UniqueId=INSTANCE_ID,22 Status='SUCCESS')23 def test_notify_exception_caught(self):24 client_error = ClientError({'Error': {'Code': 'ValidationError'}}, '')25 self.cloudformation.signal_resource.side_effect = client_error26 self.signaller.notify(self.manifest)27 self.cloudformation.signal_resource.assert_called_with(28 StackName=STACK_NAME,29 LogicalResourceId=RESOURCE_NAME,30 UniqueId=INSTANCE_ID,31 Status='SUCCESS')32 def test_notify_exception(self):33 client_error = ClientError({'Error': {'Code': 'Kaboom'}}, '')34 self.cloudformation.signal_resource.side_effect = client_error35 self.assertRaises(ClientError, self.signaller.notify, self.manifest)36 self.cloudformation.signal_resource.assert_called_with(37 StackName=STACK_NAME,38 LogicalResourceId=RESOURCE_NAME,39 UniqueId=INSTANCE_ID,40 Status='SUCCESS')41 @patch('spacel.aws.cloudformation.read_file')42 def test_notify_path(self, mock_read_file):43 mock_read_file.side_effect = [STACK_NAME, RESOURCE_NAME]44 self.manifest.cf_signal = {45 'path:/home/stack': 'path:/home/resource_id'46 }47 self.signaller.notify(self.manifest)48 self.cloudformation.signal_resource.assert_called_with(49 StackName=STACK_NAME,50 LogicalResourceId=RESOURCE_NAME,51 UniqueId=INSTANCE_ID,52 Status='SUCCESS')53 @patch('spacel.aws.cloudformation.read_file')54 def test_notify_path_not_found(self, mock_read_file):55 mock_read_file.side_effect = [STACK_NAME, None]56 self.manifest.cf_signal = {57 'path:/home/stack': 'path:/home/resource_id'58 }59 self.signaller.notify(self.manifest)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful