Re: a short-cut command for globals().clear() ??

2008-09-23 Thread Terry Reedy

MRAB wrote:


How about something like this:

def clear_workspace():
keep_set = set(['__builtins__', '__doc__', '__name__',
'clear_workspace'])


For 2.6/3.0, add __package__ to the list to be kept.


for x in globals().keys():
if x not in keep_set:
del globals()[x]
--
http://mail.python.org/mailman/listinfo/python-list



--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-23 Thread Ricardo Aráoz
Terry Reedy wrote:
 MRAB wrote:

 How about something like this:

 def clear_workspace():
 keep_set = set(['__builtins__', '__doc__', '__name__',
 'clear_workspace'])
 
 For 2.6/3.0, add __package__ to the list to be kept.
 
 for x in globals().keys():
 if x not in keep_set:
 del globals()[x]


Or... you might have a script clearWorkspace.py :

-
initGlobals = globals().keys()

def clearWorkspace() :
for gVar in globals().keys() :
if gVar not in initGlobals :
del globals()[gVar]
-

Which you run before doing anything else. Then you don't mind if
additions were made to the list to keep, or if you are using something
like pyCrust which has its own initial globals, or if you want to keep
some global vars of your own (in which case you run clearWorkspace AFTER
you instantiate your global vars).


--
http://mail.python.org/mailman/listinfo/python-list


a short-cut command for globals().clear() ??

2008-09-22 Thread CapnBearbossa
hi all,

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:

 x=3
 y=4
 z=[]
 dir()
['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

 some_command_or_function()

 dir()
['__builtins__', '__doc__', '__name__']

thanks,
   1 desperate snake oil programmer 
--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-22 Thread Matimus
On Sep 22, 2:31 pm, [EMAIL PROTECTED] wrote:
 hi all,

 forgive me , but the RTFM and Google search approaches are not
 yielding an answer on this question.  I need to know if there's a top
 level python interpreter command that clears all user variables (not
 built-ins) from the global namespace.  In other words a statement, or
 some_command_or_function(), that does this:

  x=3
  y=4
  z=[]
  dir()

 ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

  some_command_or_function()
  dir()

 ['__builtins__', '__doc__', '__name__']

 thanks,
    1 desperate snake oil programmer 

I don't think you will find anything. The interpreter is essentially
the same whether you are in interactive mode or not. That is, there is
very little use for a method that clears globals in general, so why
would we add it just so that it could be used by the interpreter.
There is almost* nothing available to the interactive interpreter
which isn't part of the core language.

* The only difference I can think of is the _ variable, which is
added to __builtins__ and contains the last value returned in
interactive mode. If you have ever tried to run code that uses the
locale module from the interpreter you will see why having any
differences between the interactive and non-interactive interpreter
can be a pain.

Matt
--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-22 Thread CapnBearbossa
On Sep 22, 5:52 pm, Matimus [EMAIL PROTECTED] wrote:
 On Sep 22, 2:31 pm, [EMAIL PROTECTED] wrote:



  hi all,

  forgive me , but the RTFM and Google search approaches are not
  yielding an answer on this question.  I need to know if there's a top
  level python interpreter command that clears all user variables (not
  built-ins) from the global namespace.  In other words a statement, or
  some_command_or_function(), that does this:

   x=3
   y=4
   z=[]
   dir()

  ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

   some_command_or_function()
   dir()

  ['__builtins__', '__doc__', '__name__']

  thanks,
     1 desperate snake oil programmer 

 I don't think you will find anything. The interpreter is essentially
 the same whether you are in interactive mode or not. That is, there is
 very little use for a method that clears globals in general, so why
 would we add it just so that it could be used by the interpreter.
 There is almost* nothing available to the interactive interpreter
 which isn't part of the core language.

 * The only difference I can think of is the _ variable, which is
 added to __builtins__ and contains the last value returned in
 interactive mode. If you have ever tried to run code that uses the
 locale module from the interpreter you will see why having any
 differences between the interactive and non-interactive interpreter
 can be a pain.

 Matt

ok. thanks! guess i'll be off to define my own function ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-22 Thread Terry Reedy

[EMAIL PROTECTED] wrote:


forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:


x=3
y=4
z=[]
dir()

['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']


some_command_or_function()



dir()

['__builtins__', '__doc__', '__name__']


First, a WARNING to other readers deceived by the subject line. 
Globals().clear() clears everything and leaves nothing, so Capn... is 
looking for something that works that is a shortcut for deleting 
bindings one-by-one.


To your question.  The short answer is no.

In batch mode, only create what you need and delete (unbind) large 
objects that are not automatically deleted (unbound) when you are done 
with them.  Remember that only reference-counted implementations will 
guarantee immediate destruction and space-freeing when the last 
reference goes away. Check the gc module (and some posts in the 
archives) for more specialized control.


In interactive mode, restart the interpreter if you really need a clean 
slate and have too many bindings that you must delete to do something 
quick like 'del x,y,z' as in your example above.  In IDLE, cntl-F6 
restarts the shell with a clean slate.  I presume IPython has something 
similar.


tjr

--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-22 Thread Aaron Castironpi Brady
On Sep 22, 5:44 pm, Terry Reedy [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  forgive me , but the RTFM and Google search approaches are not
  yielding an answer on this question.  I need to know if there's a top
  level python interpreter command that clears all user variables (not
  built-ins) from the global namespace.  In other words a statement, or
  some_command_or_function(), that does this:

  x=3
  y=4
  z=[]
  dir()
  ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

  some_command_or_function()

  dir()
  ['__builtins__', '__doc__', '__name__']

 First, a WARNING to other readers deceived by the subject line.
 Globals().clear() clears everything and leaves nothing, so Capn... is
 looking for something that works that is a shortcut for deleting
 bindings one-by-one.

 To your question.  The short answer is no.

 In batch mode, only create what you need and delete (unbind) large
 objects that are not automatically deleted (unbound) when you are done
 with them.  Remember that only reference-counted implementations will
 guarantee immediate destruction and space-freeing when the last
 reference goes away. Check the gc module (and some posts in the
 archives) for more specialized control.

 In interactive mode, restart the interpreter if you really need a clean
 slate and have too many bindings that you must delete to do something
 quick like 'del x,y,z' as in your example above.  In IDLE, cntl-F6
 restarts the shell with a clean slate.  I presume IPython has something
 similar.

 tjr

I guess you have a few hackish options.

1) Define a wrapper that calls its argument immediately, and use it as
you would use anonymous blocks.

@call_imm
def block( ):
   code
   code
   code

@call_imm
def block( ):
   code
   code
   code

You have no globals when you are done.

2) If you need them to be global, use some introspection, and delete
the variables upon exiting the scope.

3) Declare all your variables in a namespace, and just delete the
namespace.

a= type('blank',(),{})()
a.varA= 0
a.varB= 'abc'
del a

--
http://mail.python.org/mailman/listinfo/python-list


Re: a short-cut command for globals().clear() ??

2008-09-22 Thread MRAB
On Sep 22, 11:07 pm, [EMAIL PROTECTED] wrote:
 On Sep 22, 5:52 pm, Matimus [EMAIL PROTECTED] wrote:



  On Sep 22, 2:31 pm, [EMAIL PROTECTED] wrote:

   hi all,

   forgive me , but the RTFM and Google search approaches are not
   yielding an answer on this question.  I need to know if there's a top
   level python interpreter command that clears all user variables (not
   built-ins) from the global namespace.  In other words a statement, or
   some_command_or_function(), that does this:

x=3
y=4
z=[]
dir()

   ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

some_command_or_function()
dir()

   ['__builtins__', '__doc__', '__name__']

   thanks,
      1 desperate snake oil programmer 

  I don't think you will find anything. The interpreter is essentially
  the same whether you are in interactive mode or not. That is, there is
  very little use for a method that clears globals in general, so why
  would we add it just so that it could be used by the interpreter.
  There is almost* nothing available to the interactive interpreter
  which isn't part of the core language.

  * The only difference I can think of is the _ variable, which is
  added to __builtins__ and contains the last value returned in
  interactive mode. If you have ever tried to run code that uses the
  locale module from the interpreter you will see why having any
  differences between the interactive and non-interactive interpreter
  can be a pain.

  Matt

 ok. thanks! guess i'll be off to define my own function ...

How about something like this:

def clear_workspace():
keep_set = set(['__builtins__', '__doc__', '__name__',
'clear_workspace'])
for x in globals().keys():
if x not in keep_set:
del globals()[x]
--
http://mail.python.org/mailman/listinfo/python-list