Ask Your Question
0

Sending parameters

asked 2019-11-10 23:54:14 -0600

kevin gravatar image

Hi, how are you? I would like to know if there is any way to send parameters to a sage script.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2019-11-12 00:42:04 -0600

Emmanuel Charpentier gravatar image

updated 2019-11-12 04:33:10 -0600

Iguananaut gravatar image

From the Python docs:

sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

To loop over the standard input, or the list of files given on the command line, see the fileinput module.

Note On Unix, command line arguments are passed by bytes from OS. Python decodes them with filesystem encoding and “surrogateescape” error handler. When you need original bytes, you can get it by [os.fsencode(arg) for arg in sys.argv].

edit flag offensive delete link more

Comments

[Edited to remove unnecessary metacommentary.]

Iguananaut gravatar imageIguananaut ( 2019-11-12 04:33:22 -0600 )edit

The metacommentary is, IMHO, necessary, not only for this case, but for a multitude of other cases (reviewiong "old" quiestions is instructive...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-11-12 06:55:18 -0600 )edit
1

Please don't browbeat people for asking questions.

Iguananaut gravatar imageIguananaut ( 2019-11-13 07:20:43 -0600 )edit
1

answered 2019-11-12 04:36:11 -0600

Iguananaut gravatar image

To add to Emmanuel's answer, in addition to inspecting sys.argv directly, you can easily implement clean and well-documented command line argument handling with argparse (it takes a little getting used to, but the examples at the top of the documentation contain most of what you need to know to get started).

Keep in mind that "Sage" code, including .sage scripts, is just the Python programming language with a few specialized bells and whistles, so any question you might have about how to do X with Sage, that isn't explicitly mathematics-related, is a question you can also ask about how to do that in Python and hence find myriad existing resources.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2019-11-10 23:54:14 -0600

Seen: 127 times

Last updated: Nov 12 '19