Re: [Tutor] Fwd: Re: Setting Command Line Arguments in IDLE

2019-05-26 Thread Mats Wichmann


> On 26/05/2019 02:55, Richard Damon wrote:
>> I am working on a python script that will be provided arguments when run
>> from the system command line. Is there any place in IDLE to provide
>> equivalent arguments for testing while developing in IDLE?
>>
> 
> There used to be a dialog for that but in the latest version
> of IDLE I can't find it. I wonder when it disappeared and why?
> 
> The best place to ask is probably on the IDLE-dev list.
> 
> It can be found here:
> 
> https://mail.python.org/mailman/listinfo/idle-dev

I've seen this question come up on stack overflow, can't recall I've
seen a completely satisfactory answer.

I would suggest, however, that doing the testing you're considering
should be written as unit tests.  You can invoke unit tests from inside
the program by adding something like this (I'm a pytest fan, but it
could be unittest as well of course):

import pytest

# your code here

if __name__ == "__main__":
pytest.main(["--capture=sys", "name-of-unittest-script.py"])

You can write your own argument array by fiddling with sys.argv; pytest
also provides a mechansim for injecting arguments (I think it's called
pytest_addoption).

The somewhat hacky way for a script to find out that it's running inside
IDLE (note: every time someone asks how to do this, a crowd of people
pop up and say "you don't want to be doing that".  But enabling a
testing scenario might actually be a time you want to?):

import sys

if "idlelib" in sys.modules:
print("We're running in IDLE")



These aren't really an answer to what you're asking for, but maybe some
tools you might use to think further about the problem?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: Setting Command Line Arguments in IDLE

2019-05-26 Thread Alan Gauld via Tutor
Oops, Forgot to include the list!



 Forwarded Message 
Subject:Re: Setting Command Line Arguments in IDLE
Date:   Sun, 26 May 2019 09:03:36 +0100
From:   Alan Gauld 
To: Richard Damon 



On 26/05/2019 02:55, Richard Damon wrote:
> I am working on a python script that will be provided arguments when run
> from the system command line. Is there any place in IDLE to provide
> equivalent arguments for testing while developing in IDLE?
>

There used to be a dialog for that but in the latest version
of IDLE I can't find it. I wonder when it disappeared and why?

The best place to ask is probably on the IDLE-dev list.

It can be found here:

https://mail.python.org/mailman/listinfo/idle-dev

> Is there any way to define the working directory for the program, 

os.getcwd() # read current dir
os.chdir() # set current dir

> If not, is there an easy way to detect that I am running in IDLE so I
> can fake the command line arguments when testing?

Not that I'm aware, but the idle-dev gurus may have some ideas.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor