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
  Problem with variables JCB 3 147 Nov-13-2019, 07:20 PM
Last Post: the_ophidian_order
  modifying variables in local or global space Skaperen 2 210 Aug-14-2019, 07:13 AM
Last Post: Skaperen
  common code, def a function vs exec() a string Skaperen 7 424 May-27-2019, 10:13 AM
Last Post: heiner55
  A problem with defining variables meru120 5 647 Apr-16-2019, 03:13 PM
Last Post: buran
  Problem with variables Steffenwolt 4 840 Jul-14-2018, 07:27 PM
Last Post: Steffenwolt
  using function through exec() kiyoshi7 9 1,607 Mar-03-2018, 09:25 PM
Last Post: Larz60+
  [split] Problem using global variables RedSkeleton007 1 874 Nov-10-2017, 09:13 PM
Last Post: sparkz_alot
  Vending Machine Program: Problem with variables icabero0225 2 3,850 Jun-08-2017, 11:04 AM
Last Post: buran
  namespaces in comprehension in exec() Skaperen 4 1,523 Mar-28-2017, 03:31 AM
Last Post: Skaperen
  MySQLdb, problem with query with user-defined variables buran 6 2,483 Feb-03-2017, 06:16 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)