[Tutor] Setting Command Line Arguments in IDLE
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? Is there any way to define the working directory for the program, or will it always be the directory the script is in (it will be typically run using the PATH, so not the same directory as the script)? 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? -- Richard Damon ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Setting Command Line Arguments in IDLE
On 2019-05-25 18:55, Richard Damon wrote: Is there any way to define the working directory for the program, or will it always be the directory the script is in (it will be typically run using the PATH, so not the same directory as the script)? import os cwd = os.getcwd() os.chdir(path) Sorry, can't help you re Idle. ___ 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
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
Re: [Tutor] Fwd: Re: Setting Command Line Arguments in IDLE
> 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
Re: [Tutor] Setting Command Line Arguments in IDLE
Hello Richard, In addition to the answers you have received from Alex and Alan, I'll add a bit longer of an answer mostly around how to separate the command-line invocation / argument handling part of your program from the pure-Python logic. While I, also, cannot help terribly with the direct question about your IDLE environment, I hope this explanation is generally helpful and useful. >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? I can't really help so much with the IDLE part of the question, but I do have a solution that has worked very well for me for testing programs that are usually invoked by command-line. My answer will focus on that angle rather than the IDLE angle. >Is there any way to define the working directory for the program, >or will it always be the directory the script is in (it will be >typically run using the PATH, so not the same directory as the >script)? Defining working directory. --- You cannot control the initial working directory when your program has been executed. It was executed by another process (or human, or alien from the Ophiucus Cluster) so your working directory is whatever was in the parent parent process just before forking/execution of your program. However, you are free to chdir() once your program is running: >>> os.getcwd() '/home/mabrown' >>> os.chdir('/home/mabrown/tmp/') >>> os.getcwd() '/home/mabrown/tmp' One bit of care that is warranted when you are calling os.chdir() (besides the usual error-checking, 'Can the user running this program actually chdir() to that directory?)' is to think about whether you want your program to use relative paths or absolute paths. It's worth thinking about early because if you don't, you can end up with a mess of relative and absolute paths. That way madness, bugs and possible data loss lie. What is my install directory? - If you need to know what directory your Python program is in, you can use os.path.dirname(os.path.abspath(__file__)). Sometimes, there are reasons to find your data (or other code?) in the same directory as (or nearby) your Python program. Think carefully about this and consider how that might work with your Python packaging solution, if you are going down that path. In general, I have tried to use command-line options to find data directories or other files needed by a Python program, as that is more intelligible to users (and to me when I come back to the code a year later) than the automatic discovery of files that just happen to be in the same directory as my Python program. Random note on $PATH and full pathnames --- Have you ever been trying to diagnose a problem with a script and you realized about four months two late (because that 20 minute debugging session where you think you are going insane feels like four months by the time you are done), and you realized at the end that the problem was that you thought you were running a copy of script A in directory A, but you were actually running older copy B in directory B. This meant that none of your changes were being reflected in the output and you couldn't figure it out. Well, I'll tell you, I have never, ever had that experience! Nope. Not even once. Yeah. Anyway, I have found that logging os.path.abspath(__file__) to the system log (or to STDERR or elsewhere) what the full path is to the file that is executing. This is useful to avoid the "I think I'm running my dev copy, but am actually running something else." sort of problem. >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? I'll give you an example (see below) of how I write the Python to call most of my programs that are invoked directly from the command-line. The basic idea (which I think I picked up here) is to turn anything that you want to be able to test in your program into Python variables almost as soon as the program begins. That way, things like STDIN, STDOUT and the arguments become simply file objects and lists of strings. With file objects and lists of strings you can write testing functions to see how your program behaves when invoked a certain way. So, this is fairly unexciting code: def cli(me, fin, fout, argv): config, args = collectconfiguration(me, argv) rc = process_something(fin, fout, config) if rc == 0: return os.EX_OK return 1 if __name__ == '__main__': me = os.path.basename(sys.argv[0]) sys.exit(cli(me, sys.stdin, sys.stdout, sys.argv[1:])) You will see that I did not add os.environ to the argument list to the function cli(), but if my program(s) did anything with environment variables, I would add that
[Tutor] tweeting from python
Hi there, I was wondering if anyone had some tips that might direct me to answers to this problem: I have a program which one downloads to your computer, and you install. Then, I want to add into this program a share button. This share button would send a tweet to your twitter saying, "I am watching episode x of podcast y using program z, check it out at programurl." Now, I think I am looking at tweepy for this, maybe oauth2, but all the tutorials on google are for creating one use apps. This is an app that would role out to users. I am guessing it would need to use some kind of, authorsation to allow it to tweet, but... That's about as far as I can get. Any help would be appreciated greatly. Thanks Nate ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tweeting from python
On 26-5-2019 16:59, nathan tech wrote: > Hi there, > > I was wondering if anyone had some tips that might direct me to answers > to this problem: > > I have a program which one downloads to your computer, and you install. > > Then, I want to add into this program a share button. > > This share button would send a tweet to your twitter saying, "I am > watching episode x of podcast y using program z, check it out at > programurl." > > > Now, I think I am looking at tweepy for this, maybe oauth2, but all the > tutorials on google are for creating one use apps. > Nathan, you may want to give twython a look https://github.com/ryanmcgrath/twython it has Oauth etc. There are tutorials for writing twitter bots with it that you could adapt to your purpose, for example: https://geekswipe.net/technology/computing/code-python-twitter-bot-in-ten-minutes/ Ingo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor