This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dmp.py
119 lines (107 loc) · 3.73 KB
/
dmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
########### SVN repository information ###################
# $Date: $
# $Author: $
# $Revision: $
# $URL: $
# $Id: $
########### SVN repository information ###################
"""
Routines to move information between Python interpreters for code
development/debugging purposes. This is helpful to move numpy
data objects to Jupyter notebooks.
This will need more work to be used on Windows [where it will need
to use C:/TEMP or the equilvalent, see tempfile.gettempdir()]
These routines are imported into GSAS-II module GSASIIfiles, but
this module is kept separate and small so that it can be copied
to locations outside of the GSASII project directory where it
may be imported easily.
Created on Fri Jul 21 10:40:08 2023 by Brian Toby
"""
import sys
import pickle
def dump2tmp(uselocals=True, useglobals=True, usefunctions=False):
'''
Places variables from current interpreter into a scratch pickle file
that can be read into another Python interpreter.
Parameters
----------
uselocals : bool, optional
If True, include objects defined at the local level.
The default is True.
useglobals : bool, optional
If True, include objects defined at the global level.
The default is True.
usefunctions : bool, optional
Normally callable functions will not included in the pickled file,
but if set to True these functions will be included. Reading the
pickle file may then require that the sys.path include references
to the location of the modules containing these functions.
The default is False.
Returns
-------
None.
'''
if sys.platform.startswith('win'):
raise ImportError("Module dmp needs work for Windows")
fp = open('/tmp/pickledump.pickle', 'wb')
if uselocals: # get locals in caller
import inspect
frame = inspect.currentframe()
callerLocals = frame.f_back.f_locals
for o in callerLocals:
if o.startswith('_'): continue
if (not usefunctions) and callable(callerLocals[o]): continue
try:
pickle.dump((o,callerLocals[o]), fp)
print('dumpped',o)
except:
print('no dump for ', o, type(callerLocals[o]))
del frame
if useglobals:
for o in globals():
if o.startswith('_'): continue
if (not usefunctions) and callable(globals()[o]): continue
try:
pickle.dump((o,globals()[o]), fp)
print('dumpped',o)
except:
print('no dump for ', o, type(globals()[o]))
fp.close()
#dumpStuff()
def undumptmp(setglobals=True):
'''
Reads variables saved from another Python interpreter via a
scratch pickle file into the current Python interpreter.
Parameters
----------
setglobals : bool, optional
When True variables read will be declared as global.
The default is True.
Returns
-------
A dict with all the variables read from the pickle file.
'''
import inspect
frame = inspect.currentframe().f_back
vars = {}
fp = open('/tmp/pickledump.pickle', 'rb')
while True:
try:
nam,obj = pickle.load(fp)
vars[nam] = obj
if setglobals:
frame.f_globals[nam] = obj
#exec(f'global {nam}; {nam} = obj')
# print('global loaded',nam)
except EOFError:
break
except ModuleNotFoundError:
print('Ending read due to ModuleNotFoundError')
break
except Exception as msg:
print(nam,'error',msg)
pass
fp.close()
return vars