##// END OF EJS Templates
New magic: %reset_selective....
Brad Reisfeld -
Show More
@@ -1052,6 +1052,27 b' class InteractiveShell(Component, Magic):'
1052 # Restore the default and user aliases
1052 # Restore the default and user aliases
1053 self.alias_manager.init_aliases()
1053 self.alias_manager.init_aliases()
1054
1054
1055 def reset_selective(self, regex=None):
1056 """Clear selective variables from internal namespaces based on a specified regular expression.
1057
1058 Parameters
1059 ----------
1060 regex : string or compiled pattern, optional
1061 A regular expression pattern that will be used in searching variable names in the users
1062 namespaces.
1063 """
1064 if regex is not None:
1065 try:
1066 m = re.compile(regex)
1067 except TypeError:
1068 raise TypeError('regex must be a string or compiled pattern')
1069 # Search for keys in each namespace that match the given regex
1070 # If a match is found, delete the key/value pair.
1071 for ns in self.ns_refs_table:
1072 for var in ns:
1073 if m.search(var):
1074 del ns[var]
1075
1055 def push(self, variables, interactive=True):
1076 def push(self, variables, interactive=True):
1056 """Inject a group of variables into the IPython user namespace.
1077 """Inject a group of variables into the IPython user namespace.
1057
1078
@@ -1112,6 +1112,71 b' Currently the magic system has the following functions:\\n"""'
1112 # execution protection
1112 # execution protection
1113 self.shell.clear_main_mod_cache()
1113 self.shell.clear_main_mod_cache()
1114
1114
1115 def magic_reset_selective(self, parameter_s=''):
1116 """Resets the namespace by removing names defined by the user.
1117
1118 Input/Output history are left around in case you need them.
1119
1120 %reset_selective [-f] regex
1121
1122 No action is taken if regex is not included
1123
1124 Options
1125 -f : force reset without asking for confirmation.
1126
1127 Examples
1128 --------
1129
1130 In [1]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1131
1132 In [2]: who_ls
1133 Out[2]: ['a', 'b', 'b1', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1134
1135 In [3]: %reset_selective -f b[2-3]m
1136
1137 In [4]: who_ls
1138 Out[4]: ['a', 'b', 'b1', 'b1m', 'b2s', 'c']
1139
1140 In [5]: %reset_selective -f d
1141
1142 In [6]: who_ls
1143 Out[6]: ['a', 'b', 'b1', 'b1m', 'b2s', 'c']
1144
1145 In [7]: %reset_selective -f c
1146
1147 In [8]: who_ls
1148 Out[8]:['a', 'b', 'b1', 'b1m', 'b2s']
1149
1150 In [9]: %reset_selective -f b
1151
1152 In [10]: who_ls
1153 Out[10]: ['a']
1154
1155 """
1156
1157 opts, regex = self.parse_options(parameter_s,'f')
1158
1159 if opts.has_key('f'):
1160 ans = True
1161 else:
1162 ans = self.shell.ask_yes_no(
1163 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1164 if not ans:
1165 print 'Nothing done.'
1166 return
1167 user_ns = self.shell.user_ns
1168 if not regex:
1169 print 'No regex pattern specified. Nothing done.'
1170 return
1171 else:
1172 try:
1173 m = re.compile(regex)
1174 except TypeError:
1175 raise TypeError('regex must be a string or compiled pattern')
1176 for i in self.magic_who_ls():
1177 if m.search(i):
1178 del(user_ns[i])
1179
1115 def magic_logstart(self,parameter_s=''):
1180 def magic_logstart(self,parameter_s=''):
1116 """Start logging anywhere in a session.
1181 """Start logging anywhere in a session.
1117
1182
General Comments 0
You need to be logged in to leave comments. Login now