#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Clear all equivalent


    This should be a really quick easy question but one I cannot find an answer to. My only programming experience is with MatLab and Python. In MatLab if I run a program that say reads some data and then graphs it if I want to run the script again I can type:
    Code:
    clear all;
    or just add that to the beginning of the code. In python the only way I have manage to figure out how to re-run the script on another file or re-run it if I have to edit something in the code is to exit Python completely form the command line and then restart it back up. Is there an equivalent command in Python?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    Idle


    I use the IDLE (Integrated DeveLopment Environment) Python shell to run my code from. Make a code change, then punch F5 to run it.
    Last edited by WynnDeezl; March 28th, 2013 at 12:36 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    That's what I would like to use but for some reason it opens up then exits immediately (OSX). I think I'll try a reinstall (currently using the enthought distribution since it's free for students). What drove me away from idle the first time though was I couldn't navigate to different folders like I can in the terminal. For example cd and ls do not work and I couldn't figure it out. I will give the reinstall a try though to see if that works but if there is a simple command I can throw on the beginning of all my programs that would also be great if anyone knows. Thanks!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Why not just put the script in a good Text Editor with python syntax highlighting and run it in the command line as many times as you want? or run the python interpreter and then do import scriptName (without .py file extension).

    Of course, this is just my personal taste. I know many people love IDLE


    Regards,
    Dariyoosh
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by dariyoosh
    Why not just put the script in a good Text Editor with python syntax highlighting and run it in the command line as many times as you want? or run the python interpreter and then do import scriptName (without .py file extension).

    Of course, this is just my personal taste. I know many people love IDLE


    Regards,
    Dariyoosh
    I think this is exactly what I am doing right now. I currently edit in TextMate which is an OSX editor like notepad++. This is what my command line looks like:

    Code:
    128-110-71-148:Original Steve1$ python
    Enthought Python Distribution -- www.enthought.com
    Version: 7.3-2 (64-bit)
    
    Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 12 2012, 11:14:05) 
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "credits", "demo" or "enthought" for more information.
    >>> from flux_plotter import reports
    >>> reports('141.4_flux.txt')
    plots created
    >>> from flux_plotter import reports
    >>> reports('141.4_flux.txt')
    plots created
    So you can see I created two plots. The first plot was a semilog plot. The second one I changed it from semilog to loglog BUT the graph I get is still semilog. So the only way I can circumvent that as far as I know is to to exit python then start it back up.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    reload advice


    in python2

    >>> import script
    >>> script.main('test.dat')
    blah blah fail
    <...(edit edit edit SAVE)...>
    >>> reload(script); script.main('test.dat')
    blah blech SUCCESS!


    in python3 use the imp module

    >>> import imp
    >>> import script
    >>> script.main('test.dat')
    blah blah fail
    <...(edit edit edit SAVE)...>
    >>> imp.reload(script); script.main('test.dat')
    blah blech SUCCESS!



    Beware, reload can cause huge confusion.
    >>> a = myModule.myClass()
    <... (edit myMethod in myClass.py)...>
    >>> imp.reload(myModule)
    >>> b = myModule.myClass()

    a.myMethod didn't change; it is still the original.
    b.myMethod incorporates your changes.

    You may as well learn this now to gain from my lost half-day experience. "What the heck is going on? I changed the source!"
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by b49P23TIvg
    in python2

    >>> import script
    >>> script.main('test.dat')
    blah blah fail
    <...(edit edit edit SAVE)...>
    >>> reload(script); script.main('test.dat')
    blah blech SUCCESS!


    in python3 use the imp module

    >>> import imp
    >>> import script
    >>> script.main('test.dat')
    blah blah fail
    <...(edit edit edit SAVE)...>
    >>> imp.reload(script); script.main('test.dat')
    blah blech SUCCESS!



    Beware, reload can cause huge confusion.
    >>> a = myModule.myClass()
    <... (edit myMethod in myClass.py)...>
    >>> imp.reload(myModule)
    >>> b = myModule.myClass()

    a.myMethod didn't change; it is still the original.
    b.myMethod incorporates your changes.

    You may as well learn this now to gain from my lost half-day experience. "What the heck is going on? I changed the source!"
    Thanks! That's exactly what I was looking for. It still sounds like using Idle is the easiest method but until I can reinstall python this is what I need.
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    idle? I use emacs and run programs in a shell buffer.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by b49P23TIvg
    idle? I use emacs and run programs in a shell buffer.
    Yeah but just admit it you're old school! I tired to learn and like emacs but I don't like it :P
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    I admit that GUIs make simple only the operations that the GUI programmers thought would be common. I admit that fingers leaving the keyboard to grab a mouse is slow. That's why I use a thinkpad---for the little thumb stick in the middle of the keyboard with the finger pad disabled. I don't program with voice recognition. That might make me "old school".
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo