Nice programing

현재 파이썬 세션의 모든 변수를 어떻게 저장할 수 있습니까?

nicepro 2020. 10. 14. 20:56
반응형

현재 파이썬 세션의 모든 변수를 어떻게 저장할 수 있습니까?


현재 파이썬 환경의 모든 변수를 저장하고 싶습니다. 한 가지 옵션은 'pickle'모듈을 사용하는 것 같습니다. 그러나 다음 두 가지 이유로이 작업을 수행하고 싶지 않습니다.

1) 각 변수에 대해 pickle.dump ()를 호출해야합니다.
2) 변수 를 검색하려면 변수를 저장 한 순서를 기억하고 각 변수를 검색하기 위해 pickle.load ()를 수행해야합니다.

이 저장된 세션을로드 할 때 모든 변수가 복원되도록 전체 세션을 저장하는 명령을 찾고 있습니다. 이것이 가능한가?

감사합니다!
Gaurav

편집 : 저장하고 싶은 각 변수에 대해 pickle.dump ()를 호출하는 것을 신경 쓰지 않지만 변수가 저장된 정확한 순서를 기억하는 것은 큰 제한처럼 보입니다. 나는 그것을 피하고 싶다.


shelve 를 사용 shelve하면 사전과 같은 객체를 제공 하므로 객체가 절인 순서를 기억할 필요가 없습니다 .

작업을 보류하려면 :

import shelve

T='Hiya'
val=[1,2,3]

filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'n') # 'n' for new

for key in dir():
    try:
        my_shelf[key] = globals()[key]
    except TypeError:
        #
        # __builtins__, my_shelf, and imported modules can not be shelved.
        #
        print('ERROR shelving: {0}'.format(key))
my_shelf.close()

복원하려면 :

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]
my_shelf.close()

print(T)
# Hiya
print(val)
# [1, 2, 3]

여기에 앉아서 globals()딕셔너리를 사전 으로 저장하지 못해서 dill 라이브러리를 사용하여 세션을 피클 할 수 있음을 발견했습니다.

다음을 사용하여 수행 할 수 있습니다.

import dill                            #pip install dill --user
filename = 'globalsave.pkl'
dill.dump_session(filename)

# and to load the session again:
dill.load_session(filename)

귀하의 요구를 충족시킬 수있는 매우 쉬운 방법입니다. 나를 위해 그것은 꽤 잘했습니다.

변수 탐색기 (Spider 오른쪽)에서이 아이콘을 클릭하기 만하면됩니다.

* .spydata 형식으로 모든 변수 저장

모든 변수 또는 사진 등을로드합니다.


Here is a way saving the Spyder workspace variables using the spyderlib functions

#%%  Load data from .spydata file
from spyderlib.utils.iofuncs import load_dictionary

globals().update(load_dictionary(fpath)[0])
data = load_dictionary(fpath)



#%% Save data to .spydata file
from spyderlib.utils.iofuncs import save_dictionary
def variablesfilter(d):
    from spyderlib.widgets.dicteditorutils import globalsfilter
    from spyderlib.plugins.variableexplorer import VariableExplorer
    from spyderlib.baseconfig import get_conf_path, get_supported_types

    data = globals()
    settings = VariableExplorer.get_settings()

    get_supported_types()
    data = globalsfilter(data,                   
                         check_all=True,
                         filters=tuple(get_supported_types()['picklable']),
                         exclude_private=settings['exclude_private'],
                         exclude_uppercase=settings['exclude_uppercase'],
                         exclude_capitalized=settings['exclude_capitalized'],
                         exclude_unsupported=settings['exclude_unsupported'],
                         excluded_names=settings['excluded_names']+['settings','In'])
    return data

def saveglobals(filename):
    data = globalsfiltered()
    save_dictionary(data,filename)


#%%

savepath = 'test.spydata'

saveglobals(savepath) 

Let me know if it works for you. David B-H


What you're trying to do is to hibernate your process. This was discussed already. The conclusion is that there are several hard-to-solve problems exist while trying to do so. For example with restoring open file descriptors.

It is better to think about serialization/deserialization subsystem for your program. It is not trivial in many cases, but is far better solution in long-time perspective.

Although if I've exaggerated the problem. You can try to pickle your global variables dict. Use globals() to access the dictionary. Since it is varname-indexed you haven't to bother about the order.


If you want the accepted answer abstracted to function you can use:

    import shelve

    def save_workspace(filename, names_of_spaces_to_save, dict_of_values_to_save):
    '''
        filename = location to save workspace.
        names_of_spaces_to_save = use dir() from parent to save all variables in previous scope.
            -dir() = return the list of names in the current local scope
        dict_of_values_to_save = use globals() or locals() to save all variables.
            -globals() = Return a dictionary representing the current global symbol table.
            This is always the dictionary of the current module (inside a function or method,
            this is the module where it is defined, not the module from which it is called).
            -locals() = Update and return a dictionary representing the current local symbol table.
            Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

        Example of globals and dir():
            >>> x = 3 #note variable value and name bellow
            >>> globals()
            {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'x': 3, '__doc__': None, '__package__': None}
            >>> dir()
            ['__builtins__', '__doc__', '__name__', '__package__', 'x']
    '''
    print 'save_workspace'
    print 'C_hat_bests' in names_of_spaces_to_save
    print dict_of_values_to_save
    my_shelf = shelve.open(filename,'n') # 'n' for new
    for key in names_of_spaces_to_save:
        try:
            my_shelf[key] = dict_of_values_to_save[key]
        except TypeError:
            #
            # __builtins__, my_shelf, and imported modules can not be shelved.
            #
            #print('ERROR shelving: {0}'.format(key))
            pass
    my_shelf.close()

    def load_workspace(filename, parent_globals):
        '''
            filename = location to load workspace.
            parent_globals use globals() to load the workspace saved in filename to current scope.
        '''
        my_shelf = shelve.open(filename)
        for key in my_shelf:
            parent_globals[key]=my_shelf[key]
        my_shelf.close()

an example script of using this:
import my_pkg as mp

x = 3

mp.save_workspace('a', dir(), globals())

to get/load the workspace:

import my_pkg as mp

x=1

mp.load_workspace('a', globals())

print x #print 3 for me

it worked when I ran it. I will admit I don't understand dir() and globals() 100% so I am not sure if there might be some weird caveat, but so far it seems to work. Comments are welcome :)


after some more research if you call save_workspace as I suggested with globals and save_workspace is within a function it won't work as expected if you want to save the veriables in a local scope. For that use locals(). This happens because globals takes the globals from the module where the function is defined, not from where it is called would be my guess.


You can save it as a text file or a CVS file. People use Spyder for example to save variables but it has a known issue: for specific data types it fails to import down in the road.

참고URL : https://stackoverflow.com/questions/2960864/how-can-i-save-all-the-variables-in-the-current-python-session

반응형