-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathitoFunctions.py
175 lines (145 loc) · 5.13 KB
/
itoFunctions.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# ***********************************************************************
# itom software
# URL: http://www.uni-stuttgart.de/ito
# Copyright (C) 2016, Institut für Technische Optik (ITO),
# Universität Stuttgart, Germany#
#
# This file is part of itom.
#
# itom is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public Licence as published by
# the Free Software Foundation; either version 2 of the Licence, or (at
# your option) any later version.
#
# itom is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
# General Public Licence for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with itom. If not, see <http://www.gnu.org/licenses/>.
# ***********************************************************************
import sys
import inspect
import gc
import __main__
from types import (
ModuleType,
FunctionType,
MethodType,
BuiltinMethodType,
BuiltinFunctionType,
)
def getModules():
mods = sys.modules
result = [[key] + getModuleFile(value) for key, value in mods.items()]
result = sorted(result, key=lambda item: item[0])
return result
def getModuleFile(mod):
try:
# print(mod)
p = inspect.getfile(mod)
# print(p)
if p.startswith((sys.prefix, sys.exec_prefix)):
return [p, 2]
else:
return [p, 0]
except Exception as e:
# print("Error:", e)
return ["<built-in>", 1]
def reloadModules(modNames):
import imp
res = []
for i in modNames:
if sys.modules[i] != None:
try:
imp.reload(sys.modules[i])
except SyntaxError as err:
s = (
"module %s could not be reloaded. Invalid syntax in file %s, line %i: %s (character %i)"
% (
str(sys.modules[i]),
err.filename,
err.lineno,
err.text,
err.offset,
)
)
print(s)
except Exception as err:
print(
"error while reloading module", str(sys.modules[i]), ":", str(err)
)
else:
res.append(i)
return res
def at(addr):
"""Return an object at a given memory address.
The reverse of id(obj):
>>> at(id(obj)) is obj
True
Note that this function does not work on objects that are not tracked by
the GC (e.g. ints or strings).
"""
for o in gc.get_objects():
if id(o) == addr:
return o
return None
def importMatlabMatAsDataObject(value):
"""
This method is called by loadMatlabMat if the containing element is a numpy-array with a field itomMetaInformation
Then the fields are analyzed and assumed to be tags and additional information for the dataObject.
"""
import numpy as np
import itom
if type(value) is np.ndarray:
fields = value.dtype.fields
itomMetaInformation = value[
"itomMetaInformation"
] # str( value.getfield( *(fields["itomMetaInformation"]) ) )
if itomMetaInformation == "dataObject":
res = itom.dataObject(value["dataObject"].flat[0])
# res.valueUnit = float(value["valueUnit"])
elif itomMetaInformation == "npDataObject":
res = itom.npDataObject(value["dataObject"].flat[0])
else:
raise RuntimeError("itomMetaInformation unknown")
else:
raise RuntimeError("value must be a numpy ndarray")
return res
def clearAll():
"""
Clears all the global variables from the workspace except for all function, modules, classes, itom variables and items stored in clearAllState...
"""
if not clearAllState:
raise RuntimeError("No initial state found")
return
else:
deleted_keywords = []
for var in __main__.__dict__:
if var[0] == "_":
continue
# ignore the three constants defined by the itom module
if var in ["BUTTON", "MENU", "SEPARATOR"] or var in clearAllState:
continue
item = __main__.__dict__[var]
if (
isinstance(item, ModuleType)
or isinstance(item, FunctionType)
or isinstance(item, MethodType)
or isinstance(item, BuiltinMethodType)
or isinstance(item, BuiltinFunctionType)
or isinstance(item, type)
):
continue
deleted_keywords.append(var)
for key in deleted_keywords:
del __main__.__dict__[key]
# call garbage collector to really and immediately remove all flagged variables
gc.collect()
clearAllState = None
def getClearAllValues():
global clearAllState
clearAllState = []
for var in __main__.__dict__:
clearAllState.append(var)