diff --git a/doc/source/installation/installation.rst b/doc/source/installation/installation.rst index 5e6e5239379ea5087cc8335311889fc4c6bc95c3..93ce1a7a530c4ff1b09ef5e0e1a613b510b8b1d6 100644 --- a/doc/source/installation/installation.rst +++ b/doc/source/installation/installation.rst @@ -51,7 +51,7 @@ The easiest way to install kmcos is to use one of the automatic installers:: bash install-kmcos-linux-venv.bash #use 'bash install-kmcos-linux-user.bash' if you are not using a venv. #For the develop branch, use install-kmcos-linux-venv-develop.bash or install-kmcos-linux-user-develop.bash -For personal computer usage (not on a supercomputer), it is a good idea to also run the following command, which will add the kmcos viewer:: +For personal computer usage (not on a supercomputer), it is a good idea to also run the following command, which will add the kmcos viewer and movie maker:: bash install-kmcos-complete-linux-venv-Ubuntu20.bash #this is for Ubuntu20. There is also an Ubuntu18 version. diff --git a/examples/MyFirstTPD_DesorptionOnly_local_smart/export_import_library.py b/examples/MyFirstTPD_DesorptionOnly_local_smart/export_import_library.py index 8beb3d7750a92c3adf4b852f63d38aecc8a2aeb9..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/examples/MyFirstTPD_DesorptionOnly_local_smart/export_import_library.py +++ b/examples/MyFirstTPD_DesorptionOnly_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") @@ -61,7 +61,20 @@ class module_export_import: module_var_val = module_var_val.strip() # Convert to an actual Python object - module_var_val = ast.literal_eval(module_var_val) + # Workaround for sets as literal_eval can't handle them in + # Python 2.7 + if 'set' in module_var_val: + # Extract the bracketed portion which can be converted into + # a list + module_var_val = module_var_val[4:-1] + + # Convert to a list + module_var_val = ast.literal_eval(module_var_val) + + # Convert list to a set + module_var_val = set(module_var_val) + else: + module_var_val = ast.literal_eval(module_var_val) # Store it in the module setattr(self.module, module_var, module_var_val) diff --git a/examples/MyFirstTPD_DesorptionOnly_local_smart/runfile2_TPD_desorptionOnlyWithExportImport.py b/examples/MyFirstTPD_DesorptionOnly_local_smart/runfile2_TPD_desorptionOnlyWithExportImport.py index 5c41d8563cfb32cedc32e118500c9adcc320ebb4..8487056c5b0528fba538aee02e9d78b523ac5252 100644 --- a/examples/MyFirstTPD_DesorptionOnly_local_smart/runfile2_TPD_desorptionOnlyWithExportImport.py +++ b/examples/MyFirstTPD_DesorptionOnly_local_smart/runfile2_TPD_desorptionOnlyWithExportImport.py @@ -166,7 +166,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = T_initial + T_ramp * (kmc_time - kmc_time_tpdinit) diff --git a/examples/MyFirstTPD_Precovered_local_smart/export_import_library.py b/examples/MyFirstTPD_Precovered_local_smart/export_import_library.py index b9869b27288b5cc3128ed6724817e9aac157de89..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/export_import_library.py +++ b/examples/MyFirstTPD_Precovered_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str or type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") @@ -61,7 +61,20 @@ class module_export_import: module_var_val = module_var_val.strip() # Convert to an actual Python object - module_var_val = ast.literal_eval(module_var_val) + # Workaround for sets as literal_eval can't handle them in + # Python 2.7 + if 'set' in module_var_val: + # Extract the bracketed portion which can be converted into + # a list + module_var_val = module_var_val[4:-1] + + # Convert to a list + module_var_val = ast.literal_eval(module_var_val) + + # Convert list to a set + module_var_val = set(module_var_val) + else: + module_var_val = ast.literal_eval(module_var_val) # Store it in the module setattr(self.module, module_var, module_var_val) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile1.py b/examples/MyFirstTPD_Precovered_local_smart/runfile1.py index 8a0dc9f8d2cb1968d9017eaec5e2605073afa187..f1b1b8645d3c18e3786bbec092c0f9b793fc9e5d 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile1.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile1.py @@ -64,7 +64,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile1b.py b/examples/MyFirstTPD_Precovered_local_smart/runfile1b.py index 472f34f144bcf48242e9e01207d32db29415f826..35e2197b491c834dd1772cf0bf7c5af20de3d5f2 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile1b.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile1b.py @@ -64,7 +64,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile2.py b/examples/MyFirstTPD_Precovered_local_smart/runfile2.py index 02f10e7b08024e053d3aaf44408df2249cf3d7e1..f107f48dab65296218a917cf9904700351a4308d 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile2.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile2.py @@ -63,7 +63,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile3.py b/examples/MyFirstTPD_Precovered_local_smart/runfile3.py index 70dccede2a9395a96e1add086065161b6cdfd081..ddf40a886bb3f4735957533c48ef130a825ccdb5 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile3.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile3.py @@ -64,7 +64,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile4.py b/examples/MyFirstTPD_Precovered_local_smart/runfile4.py index 5bdba9929c53b083bd6d0f074ccfbcc1573ce5d4..b23d86f4115c0a6f6075da6d53627ccbd744d7a1 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile4.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile4.py @@ -63,7 +63,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) @@ -131,7 +131,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile5.py b/examples/MyFirstTPD_Precovered_local_smart/runfile5.py index 0eafe9d18d8175febefd2e5ed0c5f4bbc8bee752..25e7b0c7caea71d1ad19bf87450fe534eef8f6eb 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile5.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile5.py @@ -64,7 +64,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile6.py b/examples/MyFirstTPD_Precovered_local_smart/runfile6.py index 30c234460b06116f0f04b71526a9210c451b18e5..664c683e72c03e9895fe7882143d3de60319e9b4 100644 --- a/examples/MyFirstTPD_Precovered_local_smart/runfile6.py +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile6.py @@ -64,7 +64,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = Ti + beta * (sg.kmc_time) diff --git a/examples/MyFirstTPD_Precovered_local_smart/runfile7.py b/examples/MyFirstTPD_Precovered_local_smart/runfile7.py new file mode 100644 index 0000000000000000000000000000000000000000..6a075e6f18f72b8263ad379997d7fcf87f505df4 --- /dev/null +++ b/examples/MyFirstTPD_Precovered_local_smart/runfile7.py @@ -0,0 +1,122 @@ +from kmcos.snapshots import * +import kmcos.snapshots_globals as sg +import kmcos.snapshots as snapshots +import kmcos.throttling_globals as tg +import kmcos.throttling as throttling +import os; import sys +import export_import_library as eil + + +##### SOME LINES THAT SHOULD NOT NORMALLY BE CHANGED#### + +# File names for loading/saving parameters (related to the export import module and throttling module) +# These will not really be used for this example, but are good lines to have. +# In the future, this may be done automatically within kmcos. +tg_load_file = sg.simulation_name + 'throttling_parameters.txt' +tg_save_file = sg.simulation_name + 'throttling_parameters.txt' +sg_load_file = sg.simulation_name + 'snapshots_parameters.txt' +sg_save_file = sg.simulation_name + 'snapshots_parameters.txt' + +# Export/import module objects for saving/loading data +tg_module = eil.module_export_import(tg_save_file, tg_load_file, tg) +sg_module = eil.module_export_import(sg_save_file, sg_load_file, sg) + + +#### LOADING AND INITIALIZING ##### +# Random seed +random_seed = -731543673 #can be any integer. only used if not loading a simulation. The PRNG_state that will be generated is a list, unlike the seed, which is an integer. + +load_simulation_state = False #This is a user setting that will affect what happens below. It is for continuing a simulation from where one left off. + +if load_simulation_state == False: #initialize the seed. + snapshots.seed_PRNG(restart=False, state=random_seed) +elif load_simulation_state == True: #load everything and set the SEED to be what it was before. + # Load modules + sg_module.load_params() + tg_module.load_params() + + # Reset number of steps and time + sg.model.base.set_kmc_time(sg.kmc_time) + sg.model.base.set_kmc_step(sg.steps_so_far) + sg.atoms.kmc_step = sg.steps_so_far + + # Load the lattice + sg.model._set_configuration(np.array(sg.config)) + sg.model._adjust_database() + + # Read the PRNG state, if available, and set it in the model + snapshots.seed_PRNG(restart=True, state=sg.PRNG_state) + # Update the snapshot number + tg.current_snapshot += 1 + + +##### GETTING SETTINGS READY FOR THE SIMULATION #### + +#Snapshots module options. +sg.parameters_of_interest = ['T'] #['T','R'] #<-- put the parameters you want exported with each snapshot here. Can also put activation energy etc. +sps = 1000 # <-- the kmc steps per snapshot. Defining this variable now is optional, and also the value can be changed later. +n_snapshots = 1 # <-- The total number of snapshots to do. Defining this variable now is optional, and also the value can be changed later. +tps = 1.0 #this sets the maximum amount of time per snapshot. + +#Generally, if a user needs to change a parameter after each snapshot, only do 1 snapshot at a time with n_snapshots=1. +#This example is a temperature programmed reaction example, so n_snapshots=1 makes sense, since we need to change the temperature after each snapshot. + +#Some simulation settings. +Ti = 300 #initial temperature +Tf = 345 #final temperature +beta = 5 #beta, heating rate in Kelvin per second. + + + +# Update the cutoff time +tg.cutoff_time = 1E6 #the throttling module's cutoff time is in seconds. We are not using that feature in this simulation, but it could be set here, for example. + + +if load_simulation_state == False: + sg.model.parameters.T = 300 #set the initial temperature. + T = Ti + sys.stdout.write("Starting a fresh simulation. Current temperature is " + + str(T) + " K. Current time is " + str(sg.kmc_time) + " s.\n") + #The kmcos Model is initialized in create_headers + create_headers() + #If it is desired to run snapshots without writing output to a file, set sg.write_output = 'False'. + #If you want to start writing again, set sg.write_output = 'True' before running + #more snapshots. +elif load_simulation_state == True: + #The temperatures will have been loaded already, but we need to set our runfile's variable. + T = sg.model.parameters.T + sys.stdout.write("Restarting from old simulation. Current temperature is " + + str(T) + " K. Current time is " + str(sg.kmc_time) + " s.\n") + + +# The following assignment (and similar assignments elsewhere) is *magical*. +# It invokes a hidden __setattr__ method of the Model_Parameters class that +# calls the set_rate_constants() function to update the values of the rate +# constants in the Fortran base module. Any other kind of assignment will +# require an explicit call to set_rate_constants(). *Failure to update the +# rate constants in the Fortran module will result in wrong results!* +sg.model.parameters.T = T + +#### SIMULATING AFTER ALL INITIALIZING IS DONE #### + +#Here is the TPD/TPR loop. +while T < Tf: + #Set the 'previous' Temperature and time variables before running any steps. + last_T = T + last_t = sg.atoms.kmc_time + #Run some steps as snapshots. + # Note that the below command will try to run n_snapshots with a certain 'steps per snapshot' + # but for any given snapshot, if thte time per snapshot (tps) is reached, the snapshot will be stopped. + # in this way, no snapshot will be greater than 1 second. + + do_snapshots(sps=sps, n_snapshots=n_snapshots, tps=tps) + #update the time and temperature for after the snaphsot is over. + t = sg.atoms.kmc_time + Tincr = beta*(t-last_t) # calculate the value to increment the temperature by + T = T + Tincr + sg.model.parameters.T = T + print("Snapshot taken. New temperature:", T) + + +#The final command below writes the simulation details to the logfile +create_log() diff --git a/examples/MyFirstThrottling/MyFirstThrottling.py b/examples/MyFirstThrottling/MyFirstThrottling.py index 1a954fc0e5540e3c92891264d362fd50a6dcc22b..e66c9dc328f957d1bcb100327991ffabce89435f 100644 --- a/examples/MyFirstThrottling/MyFirstThrottling.py +++ b/examples/MyFirstThrottling/MyFirstThrottling.py @@ -322,3 +322,4 @@ kmc_model.add_process(**temporary_kwargs_dictionary) kmc_model.filename = model_name + ".xml" kmc_model.save_model() +kmcos.compile(kmc_model) \ No newline at end of file diff --git a/examples/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py b/examples/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py index 8581f870c4ea8ffa9647a43e7c5ebeb9b118b356..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/examples/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py +++ b/examples/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str or type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") diff --git a/examples/MyThirdTPR_local_smart/export_import_library.py b/examples/MyThirdTPR_local_smart/export_import_library.py index 8beb3d7750a92c3adf4b852f63d38aecc8a2aeb9..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/examples/MyThirdTPR_local_smart/export_import_library.py +++ b/examples/MyThirdTPR_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") @@ -61,7 +61,20 @@ class module_export_import: module_var_val = module_var_val.strip() # Convert to an actual Python object - module_var_val = ast.literal_eval(module_var_val) + # Workaround for sets as literal_eval can't handle them in + # Python 2.7 + if 'set' in module_var_val: + # Extract the bracketed portion which can be converted into + # a list + module_var_val = module_var_val[4:-1] + + # Convert to a list + module_var_val = ast.literal_eval(module_var_val) + + # Convert list to a set + module_var_val = set(module_var_val) + else: + module_var_val = ast.literal_eval(module_var_val) # Store it in the module setattr(self.module, module_var, module_var_val) diff --git a/examples/MyThirdTPR_local_smart/runfile.py b/examples/MyThirdTPR_local_smart/runfile.py index 5c41d8563cfb32cedc32e118500c9adcc320ebb4..8487056c5b0528fba538aee02e9d78b523ac5252 100644 --- a/examples/MyThirdTPR_local_smart/runfile.py +++ b/examples/MyThirdTPR_local_smart/runfile.py @@ -166,7 +166,7 @@ if load_simulation_state: # The following assignment (and similar assignments elsewhere) is *magical*. # It invokes a hidden __setattr__ method of the Model_Parameters class that # calls the set_rate_constants() function to update the values of the rate - # constants in the Fortran base module. Any other kind of assignment will + # constants in the Fortran base module. Any other syntax of assignment will # require an explicit call to set_rate_constants(). *Failure to update the # rate constants in the Fortran module will result in wrong results!* T = T_initial + T_ramp * (kmc_time - kmc_time_tpdinit) diff --git a/kmcos/__init__.py b/kmcos/__init__.py index e378a8ea29eed5c3a1e1360bc53eae253600d467..2abc59a040f458e652269d3d27c15973e1455563 100644 --- a/kmcos/__init__.py +++ b/kmcos/__init__.py @@ -50,7 +50,7 @@ from __future__ import print_function -__version__ = "0.0.75" +__version__ = "1.0.0" VERSION = __version__ def evaluate_param_expression(param, parameters={}): diff --git a/kmcos/run/__init__.py b/kmcos/run/__init__.py index 72454801eea6ad260b7513f6eea253e1ee1a26e6..8a460b076d1bd6e888f95e93c6ebe392c0570545 100644 --- a/kmcos/run/__init__.py +++ b/kmcos/run/__init__.py @@ -837,9 +837,10 @@ class KMC_Model(Process): os.mkdir('exported_movie_images') image_folder = './exported_movie_images' # os.chdir(image_folder) + digitsLength = len(str(frames)) #we will need to add some zeros to get the images in order. for i in range(frames): self.do_steps(steps) - self.export_picture(filename = image_folder + "/" + filename + str(i), resolution = resolution, scale = scale) + self.export_picture(filename = image_folder + "/" + filename + str(i).zfill(digitsLength), resolution = resolution, scale = scale) #os.chdir("..") image_files = [os.path.join(image_folder,img) for img in os.listdir(image_folder) if img.endswith(".png")] clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps) diff --git a/tests/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py b/tests/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py index 8581f870c4ea8ffa9647a43e7c5ebeb9b118b356..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/tests/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py +++ b/tests/MyFirstThrottling/MyFirstThrottling_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str or type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") diff --git a/tests/SQERTSS_unit_tests/throttling_test_reaction_local_smart/export_import_library.py b/tests/SQERTSS_unit_tests/throttling_test_reaction_local_smart/export_import_library.py index b9869b27288b5cc3128ed6724817e9aac157de89..fe29e88487b4acbb244cfe31333a3aaba3fc5068 100644 --- a/tests/SQERTSS_unit_tests/throttling_test_reaction_local_smart/export_import_library.py +++ b/tests/SQERTSS_unit_tests/throttling_test_reaction_local_smart/export_import_library.py @@ -23,7 +23,7 @@ class module_export_import: with open(self.save_filename, 'wt') as f: for module_var in module_vars: module_var_val = getattr(self.module, module_var) - if type(module_var_val) is str or type(module_var_val) is str: + if (type(module_var_val) == type('str')) or (type(module_var_val) == type(b'str')): # We need to make sure string values get printed as string # values or they won't be read properly on reload f.write(module_var + " = '" + str(module_var_val) + "'\n") @@ -61,7 +61,20 @@ class module_export_import: module_var_val = module_var_val.strip() # Convert to an actual Python object - module_var_val = ast.literal_eval(module_var_val) + # Workaround for sets as literal_eval can't handle them in + # Python 2.7 + if 'set' in module_var_val: + # Extract the bracketed portion which can be converted into + # a list + module_var_val = module_var_val[4:-1] + + # Convert to a list + module_var_val = ast.literal_eval(module_var_val) + + # Convert list to a set + module_var_val = set(module_var_val) + else: + module_var_val = ast.literal_eval(module_var_val) # Store it in the module setattr(self.module, module_var, module_var_val)