def test_XX(self): from pylabcontrol.core import Script script_info = {'info': 'Enter docstring here', 'scripts': {'ScriptDummy': { 'info': '\nExample Script that has all different types of parameters (integer, str, fload, point, list of parameters). Plots 1D and 2D data.\n ', 'settings': {'count': 3, 'name': 'this is a counter', 'wait_time': 0.1, 'point2': {'y': 0.1, 'x': 0.1}, 'tag': 'scriptdummy', 'path': '', 'save': False, 'plot_style': 'main'}, 'class': 'ScriptDummy', 'filepath': '/Users/rettentulla/PycharmProjects/pylabcontrol/pylabcontrol/scripts/example_scripts.py'}}, 'class': 'ScriptIterator', 'settings': {'script_order': {'ScriptDummy': 0}, 'iterator_type': 'Loop'}, 'package': 'b26_toolkit'} script_info2 = {'info': 'Enter docstring here', 'scripts': {'ScriptDummy': { 'info': '\nExample Script that has all different types of parameters (integer, str, fload, point, list of parameters). Plots 1D and 2D data.\n ', 'settings': {'count': 3, 'name': 'this is a counter', 'wait_time': 0.1, 'point2': {'y': 0.1, 'x': 0.1}, 'tag': 'scriptdummy', 'path': '', 'save': False, 'plot_style': 'main'}, 'class': 'ScriptDummy', 'filepath': '/Users/rettentulla/PycharmProjects/pylabcontrol/pylabcontrol/scripts/example_scripts.py'} }, 'class': 'dynamic_script_iterator0', 'settings': {'script_order': {'ScriptDummy': 0}, 'run_all_first': True,'script_execution_freq': {'ScriptDummy': 1}, 'N': 0}, 'package': 'b26_toolkit'} print('============================================================') module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information(script_info) print((module, package, script_class_name)) script_info3, _ = ScriptIterator.create_dynamic_script_class(script_info) print('============================================================') module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information(script_info2) print((module, package, script_class_name)) module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information(script_info3) print((module, package, script_class_name))
def test_load_and_append(self): filepath = 'C:\\Users\\Experiment\\PycharmProjects\\pylabcontrol\\pylabcontrol\\scripts\\example_scripts.py' script_dict = { 'DefaultName': { 'info': 'Enter docstring here', 'scripts': { 'ScriptDummy': { 'info': '\nExample Script that has all different types of parameters (integer, str, fload, point, list of parameters). Plots 1D and 2D data.\n ', 'settings': { 'count': 3, 'name': 'this is a counter', 'wait_time': 0.1, 'point2': { 'y': 0.1, 'x': 0.1 }, 'tag': 'scriptdummy', 'path': '', 'save': False, 'plot_style': 'main' }, 'class': 'ScriptDummy', 'filepath': filepath } }, 'class': 'ScriptIterator', 'settings': { 'script_order': { 'ScriptDummy': 0 }, 'iterator_type': 'Loop' }, 'package': 'b26_toolkit' } } scripts = {} instruments = {} scripts, loaded_failed, instruments = Script.load_and_append( script_dict=script_dict, scripts=scripts, instruments=instruments, raise_errors=True) print(('scripts', scripts)) print(('loaded_failed', loaded_failed)) print(('instruments', instruments)) print('----x') print((scripts['DefaultName'].__class__.__name__)) print('----x') print((scripts['DefaultName'].__class__.__name__.split('.')[0], script_dict['DefaultName']['package'])) assert scripts['DefaultName'].__class__.__name__.split( '.')[0] == script_dict['DefaultName']['package'] print((type(scripts['DefaultName'].__module__))) module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information( script_dict['DefaultName']) print(module) print(script_class_name)
def test_get_module_1(self): filepath = 'C:\\Users\\Experiment\\PycharmProjects\\pylabcontrol\\pylabcontrol\\scripts\\example_scripts.py' print(' ===== start test_get_module_1 =======') script_info = { 'info': 'Enter docstring here', 'scripts': { 'ScriptDummy': { 'info': '\nExample Script that has all different types of parameters (integer, str, fload, point, list of parameters). Plots 1D and 2D data.\n ', 'settings': { 'count': 3, 'name': 'this is a counter', 'wait_time': 0.1, 'point2': { 'y': 0.1, 'x': 0.1 }, 'tag': 'scriptdummy', 'path': '', 'save': False, 'plot_style': 'main' }, 'class': 'ScriptDummy', 'filepath': filepath } }, 'class': 'ScriptIterator', 'settings': { 'script_order': { 'ScriptDummy': 0 }, 'iterator_type': 'Iter test' }, 'package': 'b26_toolkit' } module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information( script_info) print(('script_class_name', script_class_name)) print(('module', module)) print(('package', package)) assert script_class_name == 'ScriptIterator' print(' ===== end test_get_module_1 ========')
def test_load_and_append_from_file(self): from pylabcontrol.core.read_write_functions import load_b26_file filename = 'C:\\Users\Experiment\PycharmProjects\\user_data\pythonlab_config_lev_test.b26' in_data = load_b26_file(filename) instruments = in_data[ 'instruments'] if 'instruments' in in_data else {} scripts = in_data['scripts'] if 'scripts' in in_data else {} probes = in_data['probes'] if 'probes' in in_data else {} script_info = list(scripts.values())[0] module, script_class_name, script_settings, script_instruments, script_sub_scripts, script_doc, package = Script.get_script_information( script_info) print(('module', module.__name__.split('.')[0])) print(('script_class_name', script_class_name)) print(('package', script_info['package'])) assert module.__name__.split('.')[0] == script_info['package'] instruments_loaded, failed = Instrument.load_and_append(instruments) if len(failed) > 0: print(('WARNING! Following instruments could not be loaded: ', failed)) print( '========================================================================\n\n' ) scripts_loaded, failed, instruments_loaded = Script.load_and_append( script_dict=scripts, instruments=instruments_loaded)
def set_up_dynamic_script(script_information, script_iterators, verbose=verbose): ''' Args: script_information: information about the script as required by Script.get_script_information() Returns: script_default_settings: the default settings of the dynamically created script as a list of parameters sub_scripts script_iterators: dictionary of the script_iterator classes of the form {'package_name': <script_iterator_classe>} package: name of the package of the script_iterator ''' if verbose: print(('script_information', script_information)) sub_scripts = { } # dictonary of script classes that are to be subscripts of the dynamic class. Should be in the dictionary form {'class_name': <class_object>} (btw. class_object is not the instance) script_order = [ ] # A list of parameters giving the order that the scripts in the ScriptIterator should be executed. Must be in the form {'script_name': int}. Scripts are executed from lowest number to highest script_execution_freq = [ ] # A list of parameters giving the frequency with which each script should be executed _, script_class_name, script_settings, _, script_sub_scripts, _, package = Script.get_script_information( script_information) if package not in script_iterators: script_iterators.update( ScriptIterator.get_script_iterator(package)) assert package in script_iterators iterator_type = getattr(script_iterators[package], 'get_iterator_type')(script_settings, script_sub_scripts) if verbose: print(('iterator_type JG', iterator_type)) if isinstance(script_information, dict): for sub_script_name, sub_script_class in script_sub_scripts.items( ): if isinstance(sub_script_class, Script): # script already exists # To implement: add a function to scripts that outputs a script_information dict corresponding # to the current settings. This can then be fed into Script.get_script_information and things # can proceed as below. We may also need to add an additional tracker to the dialogue window # to differentiate between the case of wanting a new script from scratch when there is an # identically named one already loaded, vs using that loaded script raise NotImplementedError elif script_sub_scripts[sub_script_name][ 'class'] == 'ScriptIterator': # raise NotImplementedError # has to be dynamic maybe??? script_information_subclass, script_iterators = ScriptIterator.create_dynamic_script_class( script_sub_scripts[sub_script_name], script_iterators) subscript_class_name = script_information_subclass[ 'class'] #previously in python 2 had used: import pylabcontrol.core.script_iterator #however, this shouldn't have worked, as this was already imported as part of pylabcontrol, so nothing #happens and the old version without the dynamic script is used. Here, we force import the new #version of the module which now contains the script_iterator and everything works as expected script_iterator_module = __import__( 'pylabcontrol.core.script_iterator') sub_scripts.update({ sub_script_name: getattr(script_iterator_module, subscript_class_name) }) else: if verbose: print(('script_sub_scripts[sub_script_name]', sub_script_class)) module = Script.get_script_module(sub_script_class, verbose=verbose) if verbose: print(('module', module)) new_subscript = getattr( module, script_sub_scripts[sub_script_name]['class']) sub_scripts.update({sub_script_name: new_subscript}) # for some iterators have default scripts, e.g. point iteration has select points default_sub_scripts, default_script_settings = getattr( script_iterators[package], 'get_iterator_default_script')(iterator_type) sub_scripts.update(default_sub_scripts) for k, v in default_script_settings.items(): if k in script_settings: script_settings[k].update(v) elif isinstance(script_information, Script): print('old code - DOUBLE CHECK') raise NotImplementedError # if the script already exists, just update the script order parameter sub_scripts.update({script_class_name: script_information}) else: raise TypeError( 'create_dynamic_script_class: unknown type of script_information' ) script_order, script_execution_freq = getattr( script_iterators[package], 'get_script_order')(script_settings['script_order']) script_default_settings = getattr( script_iterators[package], 'get_default_settings')(sub_scripts, script_order, script_execution_freq, iterator_type) return script_default_settings, sub_scripts, script_iterators, package