Best Python code snippet using localstack_python
preferences_dialog.py
Source:preferences_dialog.py
1##########################################################################2# MediPy - Copyright (C) Universite de Strasbourg3# Distributed under the terms of the CeCILL-B license, as published by4# the CEA-CNRS-INRIA. Refer to the LICENSE file or to5# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html6# for details.7##########################################################################8import os9import socket10import wx11import wx.grid12import wx.lib.scrolledpanel13import medipy.network.dicom14import medipy.gui.base15import medipy.base16import logging17try :18 import paramiko19except ImportError :20 logging.warning("medipy.io.dicom.SSHTunnelConnect not usable: cannot find paramiko package")21class PreferencesDialog(medipy.gui.base.Panel):22 _connections = "network/dicom/connections"23 24 class UI(medipy.gui.base.UI):25 def __init__(self):26 self.add = None27 self.close = None28 self.connections = None29 30 self.controls = ["add","close","connections"]31 32 def __init__(self, parent=None, *args, **kwargs):33 34 self.list_connections = medipy.base.ObservableList()35 36 #Set TextCtrl and labels associated37 self.headers=['SSH','Username','Hostname','Port','Calling AE',38 'Called AE','Description','Retrieve','Retrieve Data']39 40 self.shortnames={"SSH" : 'ssh', "Username" : 'username',41 "Hostname" : 'host', "Port" : 'port', "Description" : 'description',42 "Calling AE" : 'calling_ae_title', "Called AE" : 'called_ae_title',43 "Retrieve" : "retrieve", "Retrieve Data" : "retrieve_data"}44 45 # User interface46 self.ui = PreferencesDialog.UI()47 48 xrc_file = medipy.base.find_resource(os.path.join("resources","gui","connection_preferences.xrc"))49 wrappers = []50 medipy.gui.base.Panel.__init__(self, xrc_file, "ConnectionPreferences", 51 wrappers, self.ui, self.ui.controls, parent, *args, **kwargs)52 sizer = self.ui.connections.GetSizer()53 self.sizer = wx.BoxSizer(wx.VERTICAL)54 55 self.scroll_panel = wx.lib.scrolledpanel.ScrolledPanel(self.ui.connections)56 self.scroll_panel.SetupScrolling(scroll_x=False)57 self.scroll_panel.SetSizer(self.sizer)58 self.scroll_panel.SetAutoLayout(1)59 sizer.Add(self.scroll_panel,1,wx.EXPAND)60 61 self.ui.close.SetId(wx.ID_CANCEL)62 63 #Set Events64 self.ui.add.Bind(wx.EVT_BUTTON,self.OnAdd)65 self.list_connections.add_observer("any", self._save_connections)66 self.list_connections.add_observer("any", self._update_list)67 preferences = medipy.gui.base.Preferences(68 wx.GetApp().GetAppName(), wx.GetApp().GetVendorName())69 # An observable list cannot be pickled, so a list is stored in the70 # preferences : fill the content of self.list_connections instead71 # of re-assigning it.72 self.list_connections[:] = preferences.get(self._connections, [])73 self._update_list()74 self.Show(True)75 76 def _update_list(self,*args,**kwargs):77 """ Initialize gui from connection in list_connections78 Add observer on any connection/retrieve panel79 """80 self.sizer.Clear(True)81 self.panels=[]82 83 for row,(description,connection,method,option) in enumerate(self.list_connections):84 85 connection_panel = medipy.gui.network.dicom.Connection(self.scroll_panel,connection)86 retrieve_panel = medipy.gui.network.dicom.Retrieve(self.scroll_panel,method,option)87 description_text = wx.TextCtrl(self.scroll_panel,value=description,name=str(row))88 delete = wx.Button(self.scroll_panel,label='Delete',name=str(row))89 validate = wx.Button(self.scroll_panel,label='Validate',name=str(row))90 91 box = wx.StaticBox(self.scroll_panel, label=description)92 sizer = wx.StaticBoxSizer(box,wx.VERTICAL)93 94 self.panels.append([connection_panel,95 retrieve_panel,96 description_text,97 delete,98 validate,99 box])100 101 connection_panel.add_observer("modify",self._update_connections)102 retrieve_panel.add_observer("modify",self._update_connections)103 description_text.Bind(wx.EVT_TEXT,self._update_connections)104 delete.Bind(wx.EVT_BUTTON,self.OnDelete)105 validate.Bind(wx.EVT_BUTTON,self.OnValidate)106 107 hrz_sizer = wx.BoxSizer(wx.HORIZONTAL)108 hrz_sizer.AddMany([delete,validate])109 110 sizer.AddMany([(description_text,0,wx.EXPAND), (connection_panel,1,wx.EXPAND),111 retrieve_panel, hrz_sizer])112 113 self.sizer.Add(sizer,0,wx.EXPAND)114 115 self.sizer.Layout()116 117 def _update_label(self,event):118 source = event.GetEventObject()119 row = int(source.GetName())120 description = self.panels[row][2].GetValue()121 self.panels[row][5].SetLabel(description)122 123 def _update_connections(self,event=None,*args,**kwargs):124 """ Update and save connections observable list from ui entry125 """126 127 for row,item in enumerate(self.panels):128 self.list_connections[row][0] = item[2].GetValue()129 self.list_connections[row][1] = item[0].connection130 self.list_connections[row][2] = item[1].retrieve[0]131 self.list_connections[row][3] = item[1].retrieve[1]132 self._save_connections()133 self.scroll_panel.FitInside()134 if isinstance(event,wx.Event):135 self._update_label(event)136 137 def _save_connections(self, *args) :138 """ Save the connections to the preferences. This function can also be139 used as an event handler.140 """141 preferences = medipy.gui.base.Preferences(142 wx.GetApp().GetAppName(), wx.GetApp().GetVendorName())143 preferences.set(self._connections, self.list_connections[:])144 ##################145 # Event handlers #146 ##################147 148 def OnAdd(self,_):149 """ Add a new connection set with default parameters150 """151 connection =medipy.network.dicom.Connection('----',0,socket.gethostname(),'----')152 self.list_connections.append(["----", connection, "get", ' '])153 self.scroll_panel.FitInside()154 def OnDelete(self,event):155 """ Delete selected connection from the connections ObservableList156 """157 source = event.GetEventObject()158 del self.list_connections[int(source.GetName())]159 self.scroll_panel.FitInside()160 def OnValidate(self,event):161 """ Test the selected connection162 """163 source = event.GetEventObject()164 row = int(source.GetName())165 166 connection = self.list_connections[row][1]167 168 if isinstance(connection,medipy.network.dicom.SSHTunnelConnection):169 #Ask Password to user170 dlg = wx.PasswordEntryDialog(self,'Enter Your Password','SSH Connection, {0}'.format(connection.user))171 if dlg.ShowModal() != wx.ID_OK:172 dlg.Destroy()173 return174 connection.password = dlg.GetValue()175 dlg.Destroy()176 177 connection.connect()178 echo = medipy.network.dicom.scu.Echo(connection)179 try:180 echo()181 except medipy.base.Exception, e:182 dlg = wx.MessageDialog(self, "Cannot contact entity.\nMake sure you entered the right parameters.",'Connection failure',wx.OK|wx.ICON_ERROR)183 dlg.ShowModal()184 dlg.Destroy()185 else:186 dlg = wx.MessageDialog(self, "Parameters seem to be correct.\nYou may be able to work on this connection",'Connection succeeded',wx.OK|wx.ICON_INFORMATION)187 dlg.ShowModal()188 dlg.Destroy()...
test_py_ssh_twist.py
Source:test_py_ssh_twist.py
...27 sm.open_connection('test3', conn['host'], conn['user'],28 conn['passwd'], conn['port'])29 print '||||||||||||||||||||||||||||||||||||||||||||||'30 print 'list_connections'31 print sm.list_connections()32 print '||||||||||||||||||||||||||||||||||||||||||||||'33 print 'get_connection'34 print sm.get_connection()35 print '||||||||||||||||||||||||||||||||||||||||||||||'36 print 'set_active_connection'37 print sm.set_active_connection('test')38 print '||||||||||||||||||||||||||||||||||||||||||||||'39 print 'get_connection'40 print sm.get_connection()41 print '||||||||||||||||||||||||||||||||||||||||||||||'42 print 'set_timeout'43 print sm.set_timeout(4)44 print '||||||||||||||||||||||||||||||||||||||||||||||'45 print 'write'46 print sm.write('ls')47 print '||||||||||||||||||||||||||||||||||||||||||||||'48 print 'read'49 print sm.read()50 print '||||||||||||||||||||||||||||||||||||||||||||||'51 print 'close_connection default'52 print sm.close_connection()53 print '||||||||||||||||||||||||||||||||||||||||||||||'54 print 'list_connections'55 print sm.list_connections()56 print '||||||||||||||||||||||||||||||||||||||||||||||'57 print 'close_connection'58 print sm.close_connection('test3')59 print '||||||||||||||||||||||||||||||||||||||||||||||'60 print 'list_connections'61 print sm.list_connections()62 print '||||||||||||||||||||||||||||||||||||||||||||||'63 print 'close_all_connections'64 print sm.close_all_connections()65 print '||||||||||||||||||||||||||||||||||||||||||||||'66 print 'list_connections'67 print sm.list_connections()68 logMsg('logRunning','Twister Ssh test done.')69 # This return is used by the framework!70 return "PASS"71#72# Must have one of the statuses:73# 'pass', 'fail', 'skipped', 'aborted', 'not executed', 'timeout'...
qubit_setup.py
Source:qubit_setup.py
1def generate_connections(distance):2 d = distance3 dim = 2*d - 14 total_qubits = dim*dim5 connections_dict = {}6 for i in range(1, total_qubits + 1):7 list_connections = []8 if i%dim == 0:9 if i-dim > 0:10 list_connections.append(i-dim)11 list_connections.append(i-1)12 if i+dim < total_qubits + 1:13 list_connections.append(i+dim)14 elif i%dim == 1:15 if i-dim > 0:16 list_connections.append(i-dim)17 list_connections.append(i+1)18 if i+dim < total_qubits + 1:19 list_connections.append(i+dim)20 else:21 if i-dim > 0:22 list_connections.append(i-dim)23 list_connections.append(i-1)24 list_connections.append(i+1)25 if i+dim < total_qubits+1:26 list_connections.append(i+dim)27 connections_dict[i] = list_connections28 return connections_dict29def generate_data_qubits(total_qubits):30 data_qubits = []31 for i in range(1, total_qubits+1):32 if i%2 != 0:33 data_qubits.append(i)34 return data_qubits35def generate_x_ancillas(distance, total_qubits):36 x_ancillas = []37 for i in range(1,distance):38 x_ancillas.append(2*i)39 for j in x_ancillas:40 if j + 4*distance - 2 < total_qubits:41 x_ancillas.append(j + 4*distance -2)42 return x_ancillas43def generate_z_ancillas(distance, total_qubits):44 z_ancillas = [2*distance]45 for i in range(1,distance):46 z_ancillas.append(z_ancillas[-1] + 2)47 for j in z_ancillas:48 if j + 4*distance - 2 < total_qubits:49 z_ancillas.append(j + 4*distance -2)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!