Clearing pythonwin environment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Network Ninja

    #1

    Clearing pythonwin environment

    I want to restart the environment occasionally to default (like I
    restarted pythonwin). I wrote this script. I know why it doesn't work,
    cause it deletes my variable (item) on each iteration. My question is:
    is it possible to do this? What other things might I try?

    # Reset Pythonwin enviroment

    # Variables
    keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']

    print
    for item in dir():
    if item not in keep:
    del item

    print
    print dir()

  • Steven Bethard

    #2
    Re: Clearing pythonwin environment

    Network Ninja wrote:[color=blue]
    > I want to restart the environment occasionally to default (like I
    > restarted pythonwin). I wrote this script. I know why it doesn't work,
    > cause it deletes my variable (item) on each iteration. My question is:
    > is it possible to do this? What other things might I try?
    >
    > # Reset Pythonwin enviroment
    >
    > # Variables
    > keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']
    >
    > print
    > for item in dir():
    > if item not in keep:
    > del item[/color]
    ^^^^^^^^
    del globals()[item]

    I believe that should work.

    Note that this won't cause imported modules to be reloaded though, so I
    wouldn't rely on it too heavily. The imported modules problem can only
    really be solved by restarting the interpreter.

    STeVe

    Comment

    • Scott David Daniels

      #3
      Re: Clearing pythonwin environment

      Network Ninja wrote:[color=blue]
      > I want to restart the environment occasionally to default (like I
      > restarted pythonwin). I wrote this script. I know why it doesn't work,
      > cause it deletes my variable (item) on each iteration. My question is:
      > is it possible to do this? What other things might I try?
      >
      > keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']
      > for item in dir():
      > if item not in keep:
      > del item[/color]

      keep = set(['__builtins__', '__doc__', '__name__',
      'pywin', 'keep', 'item', 'globs']) # Faster to test
      globs = globals()
      for item in dir():
      if item not in keep:
      del globs[item]
      del globs, item

      Note that this leaves all imported modules imported.

      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      • Network Ninja

        #4
        Re: Clearing pythonwin environment


        Scott David Daniels wrote:[color=blue]
        > keep = set(['__builtins__', '__doc__', '__name__',
        > 'pywin', 'keep', 'item', 'globs']) # Faster to test
        > globs = globals()
        > for item in dir():
        > if item not in keep:
        > del globs[item]
        > del globs, item[/color]

        This did not work, it gave an error saying "NameError: name 'globs' is
        not defined".

        However, Steven's del globals()[item] did the trick perfectly. Thanks a
        million for the replies, both of you.

        So can you explain the [] after globals()? How does that work? The help
        docs don't mention anything about that in the function.

        Comment

        • Scott David Daniels

          #5
          Re: Clearing pythonwin environment

          Network Ninja wrote:[color=blue]
          > Scott David Daniels wrote:[color=green]
          >> keep = set(['__builtins__', '__doc__', '__name__',
          >> 'pywin', 'keep', 'item', 'globs']) # Faster to test
          >> globs = globals()
          >> for item in dir():
          >> if item not in keep:
          >> del globs[item]
          >> del globs, item[/color]
          >
          > This did not work, it gave an error saying "NameError: name 'globs' is
          > not defined".[/color]

          I suspect that was because you did not include the last entry in the
          "keep" set above.

          By the way, another way to clean up:

          def cleanup(keep=se t(['__builtins__', '__doc__', '__name__',
          'pywin', 'cleanup']):
          globs = globals()
          for item in set(globs) - keep:
          del globs[item]

          Then, simply call:

          cleanup()
          [color=blue]
          > So can you explain the [] after globals()? How does that work?[/color]
          globals() returns a dict (or dict-like object) that reflects and
          access the current global environment. Imagine:

          globs = dict(a=1, b='2', c='iii')
          print globs
          del globs['b']
          print globs


          --Scott David Daniels
          scott.daniels@a cm.org

          Comment

          Working...