Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problem using exec to save local variables
#1
Hi everyone.
i'm trying to use the exec() function to be able to save variables defined between two editor lines in such a way:
###
exec(string_to_get_local_variables_into_a_dictionary)
a=1
b=2
...
exec(string_to_get_local_variables_again_and_get_the_difference_between_this_current_dictionary_and_previous_one)
###
in order to get the above strings i've created two functions: save_tic(), save_toc().
however something weird happens -
1. when i simply execute - exec(save_toc()) doesn't work and it says i haven't defined variables which i did
2. when i debug to that point, manually return the string from save_toc() and execute it then it DOES work.
does anybody know what's going on?

###################################################
### Functions: ####
def save_tic(baseline_name_finale):
    get_baseline_variables_str = '_baseline_variables = locals().copy()\n' \
                                   '_baseline_variables_keys = list(_baseline_variables.keys())\n' \
                                   '_baseline_variables_values = list(_baseline_variables.values())\n' \
                                   '_baseline_variables_keys_set' + str(baseline_name_finale) + ' = set(_baseline_variables_keys)\n'
    return get_baseline_variables_str;


def save_toc(base_finale, post_finale, prefix_to_delete=None):
    saved_dictionary_name = 'saved_dictionary' + str(post_finale)

    execution_string = '_final_variables = locals().copy();\n' \
        '_final_variables_keys = list(_final_variables.keys());\n' \
        '_final_variables_values = list(_final_variables.values());\n' \
        '_final_variables_keys_set = set(_final_variables_keys);\n' \
        'final_variables_dictionary = dict();\n' \
        'for k,v in zip(_final_variables_keys,_final_variables_values):\n' \
        '    final_variables_dictionary[k] = v;\n' \
        'relevant_keys_set = _final_variables_keys_set ^ _baseline_variables_keys_set' + str(base_finale) + ';\n' \
        'relevant_keys_set = [element for element in relevant_keys_set if element.startswith(\'_\')==False]\n' \
        'relevant_keys_set = list(relevant_keys_set);\n'   \
        'relevant_keys_set_modified = relevant_keys_set.copy(); \n' \
        'for i in range(0,len(relevant_keys_set_modified),1): \n' \
        '   if relevant_keys_set_modified[i].split(\'_\')[0]==\'' + str(prefix_to_delete) + '\': \n' \
        '        joint_string = \' \'; \n'  \
        '        string_list = relevant_keys_set_modified[i].split(\'_\')[1:] \n' \
        '        for i in range(0, len(string_list), 1): \n' \
        '            if i > 0: \n'  \
        '                joint_string += \'_\'; \n' \
        '            joint_string += string_list[i]; \n' \
        '        relevant_keys_set_modified[i] = joint_string \n' \
        'saved_dictionary' + str(post_finale) + ' = EasyDict() \n' \
        'for key_modified,key_original in zip(relevant_keys_set_modified,relevant_keys_set): \n' \
        '   saved_dictionary' + str(post_finale) + '[key_modified]=final_variables_dictionary[key_original] \n'
    
     return execution_string 
###################################################


###################################################
def some_function():
   exec(save_tic(1))
   a = 1
   b = 1
   exec(save_toc(1,1))  #suppose to finally return a variable called "saved_dictionary1" which has: saved_dictionary1={'a':1, 'b':1'}
   return saved_dictionary1

if __name__ == "__main__":
    variables_in_function = some_function()
###################################################
Gribouillis wrote Dec-01-2019, 11:14 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use code tags on future posts.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Save as problem Kristenl2784 2 185 Jun-17-2020, 04:31 PM
Last Post: Kristenl2784
  exec() in class, NameError niski1996 6 348 Apr-20-2020, 07:14 PM
Last Post: niski1996
  Question regarding local and global variables donmerch 12 516 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 337 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Creating local variables from a string peckjonk 2 289 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  Is this use of exec pythonic? psolar 1 225 Feb-07-2020, 12:23 PM
Last Post: buran
  Problem with variables JCB 3 308 Nov-13-2019, 07:20 PM
Last Post: the_ophidian_order
  modifying variables in local or global space Skaperen 2 366 Aug-14-2019, 07:13 AM
Last Post: Skaperen
  common code, def a function vs exec() a string Skaperen 7 652 May-27-2019, 10:13 AM
Last Post: heiner55
  A problem with defining variables meru120 5 864 Apr-16-2019, 03:13 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)