Transcription of Python programming | Scripting
1 Python programming ScriptingFinn Arup NielsenDTU ComputeTechnical University of DenmarkJuly 4, 2014 Python scriptingOverviewHow to make a command-line script (as oppose to a module)?HeaderArgument parsing__main__Command-line inputStandard input/output and pipingFinn Arup Nielsen1 July 4, 2014 Python scriptingNamingIt is not necessary to call a script run from the command-line with the .py it might be better to hide the implementation (that it is writtenin Python ) from the user (for some operating systems).Finn Arup Nielsen2 July 4, 2014 Python scriptingHeader in Linux-like environmentThe hash-bang at the top#!/usr/bin/pythonenabling you to run the script like (after setting of the ecexcution bit withchmod a+x myscript):$ myscriptrather than$ Python myscriptor if you are afraid the Python program you want is not installed in /usr/bin(thinkvirtualenv):#!
2 /usr/bin/env pythonFinn Arup Nielsen3 July 4, 2014 Python scriptingHeader in Windows-like environmentHashbang does not work in you instead maintain then you are able toASSOCandFTYPE commands to associate a filetype to a specific program (such asthe Python program. See the suggestion on Stack Arup Nielsen4 July 4, 2014 Python scriptingCommand-line argument basicsCommand-line arguments are available in of#!/usr/bin/env pythonimport sysprint( )Called with 3 command-line arguments:$ ./myscript --verbose -a=34 [ myscript , --verbose , -a=34 , ]Note there are four items in the list: The first element is the Pythonprogram Arup Nielsen5 July 4, 2014 Python scriptingArgument parsing in the old daysFor reading/parsing the command-line arguments canwrite your own code, but there are developers who have written moduleto ease the handling of the the old days you would have:getopt Module in the standard library modeled after the C getoptfunction/library.)
3 Not necessarily In the standard library. Not necessarily Added to standard library from see PEP 389. Newestmodule in the standard library and argued better Arup Nielsen6 July 4, 2014 Python scriptingargparse exampleA lot of code goes Arup Nielsen7 July 4, 2014 Python scriptingBut nowFinn Arup Nielsen8 July 4, 2014 Python scriptingdocoptFinn Arup Nielsen9 July 4, 2014 Python scriptingDocoptIdea: Use the documentation to describe the command-line interface both for humans and the argument parsing for a number of programming implementation in Python by Vladimir longer necessary to write much code only:import docoptargs = (__doc__, version=__version__)The rest is documentation (and the code for actually using the command-line arguments)Finn Arup Nielsen10 July 4, 2014 Python scriptingDocopt example#!
4 /usr/bin/env Python """ : mydocopter [options] <filename>Options:-v --verbose Log messages-o OUTPUT --output=OUTPUT Output file-a <a> Initial coefficient for second order term [default: 1.]-b <b> Initial coefficient for first order term [default: 1.]-c <c> Initial coefficient for constant term [default: 1.]Example:$ echo -e "1 4\\n2 5\\n6 8\\n3 " > $ ./mydocopter --verbose :Fit a polynomial to data. The datafile should have x y values in each row"""Finn Arup Nielsen11 July 4, 2014 Python scriptingWith just the following two lines you get usage and help working:import docoptargs = (__doc__, version= )Calling the program with wrong arguments (here<filename>is missing):$ Python mydocopterUsage: mydocopter [options] <filename>Calling the program for help (-h or help) prints the docstring:$ Python mydocopter : mydocopter [options] <filename>.
5 (and the rest of the docstring)Finn Arup Nielsen12 July 4, 2014 Python scriptingWhat is inargs?With this programimport docoptargs = (__doc__, version= )print(args)Example outputs:$ mydocopter { --output : None, --verbose : False, -a : 1. , -b : 1. , -c : 1. , <filename> : }$ mydocopter --verbose -b 3 { --output : None, --verbose : True, -a : 1. , -b : 3 , -c : 1. , <filename> : }Finn Arup Nielsen13 July 4, 2014 Python scriptingthe code of a working programFinn Arup Nielsen14 July 4, 2014 Python scriptingimport docopt, logging, = (__doc__, version= )if args[ --verbose ] ().setLevel( )a, b, c = (float(args[ - + coef]) for coef in [ a , b , c ]) ("Setting a to %f" % a) ( Reading data from + args[ <filename> ])data = [ map(float, ()) for line in open(args[ <filename> ]).]
6 Readlines()]def cost_function((a, b, c), data):return sum(map(lambda (x, y): (a*x**2 + b*x + c - y)**2, data))parameters = (cost_function, [a, b, c],args=(data,), disp=False)if args[ --output ] is None:print(" ".join(map(str, parameters)))else:with open(args[ --output ], w ) as (" ".join(map(str, parameters)))Finn Arup Nielsen15 July 4, 2014 Python scriptingDocopt detailsNotice short and long forms (-vand--verbose,-hand--help)Required fixed arguments, required variable argument (<filename>) andoptional ( ,-a 3)Options with ( ,-a 3) and without values ( ,--verbose)Options with default values[default: 1.]Furthermore:You can have or arguments, ,(set|remove)You can have multiple input arguments to a single name with .. , ,program <filename>..with parsed command-line element available in alist <filename> : [ , , ]Finn Arup Nielsen16 July 4, 2014 Python Scripting Variable constants and input argumentsDo not usually use constants that varies (!)
7 ?) in programs. Put them asinput arguments. It might be filename for output and input:"""Usage:myprogram [--output=<filename>] <input>"""# Here goes than hardcoded constants :# Here goes programINPUT_FILENAME = OUTPUT_FILENAME = ..Finn Arup Nielsen17 July 4, 2014 Python scriptingThe ifname== main : thingTo distinguish a script from a ( This is executed when the file is executed or imported )if __name__ == __main__ :print( This is executed when the file is executed, not when imported )It allows a script to be used both as a script as well as a module (if it tools that require import (but does not execute the code)will benefit from this Arup Nielsen18 July 4, 2014 Python scriptingThedef main()thingInstead of putting code in the__name__ == __main__ block add a function(usual name:main), , here with a module :def main():print("This is the main function")if __name__ == __main__ :main()This construct allows you to call the script ( , ) fromanother module, , like.
8 Import ()This would not have been possible if you put the line withprintin theblock with__name__ == __main__ . See also Python - why use def main()and the Google Python Style Arup Nielsen19 July 4, 2014 Python scriptingCommand-line inputPython interactive command-line interface to Python with coloring of 5 import blessings, re, readline, ("tab: complete") # For tab completion_term = () # For coloring text outputwhile True:expr = raw_input(">>> ")try:_ = eval(expr)print( ( 5 , ( 5 ), str(_),flags= ))except:exec(expr)Note the behavior and existence ofrawinput()andinput()is differentbetween Python 2 and Python Arup Nielsen20 July 4, 2014 Python scriptingInput/output streamsraw_input( Python 2) in Python 3 calledinputinput( Python 2), the same aseval(input()) with hidden input stream for interpreter output error streamThe original objects of the three latter are Arup Nielsen21 July 4, 2014 Python scriptingUnix pipe exampleExample with a Unix pipe:$ echo "Hallo" | Python -c "import sys; ( < + \ ().)
9 Strip() + >\n )"<Hallo>Finn Arup Nielsen22 July 4, 2014 Python scriptingReading unbuffered Reading an Unbuffered character in a cross-platform way by Danny Yoo(Martelli et al., 2005, page 98)try:from msvcrt import getchexcept ImportError:def getch():import sys, tty, termiosfd = ()old_settings = (fd) (fd)ch = (1) (fd, , old_settings)return chFinn Arup Nielsen23 July 4, 2014 Python Scripting .. Reading unbuffereddef getchs():while True:yield getch()import sys# see also getpass ch in getchs() ( * )if ch == c :breakelif ch == \r ( \n-------------\n )Finn Arup Nielsen24 July 4, 2014 Python scriptingMore informationVladimir Keleshev s YouTube video about docopt: PyCon UK 2012: Cre-ate *beautiful* command-line interfaces with PythonFinn Arup Nielsen25 July 4, 2014 Python scriptingSummaryUsedocopt: Easiest handling of command-line arguments, forces you todocument your script, ensures that your documentation and implemen-tation do not get out of == __main__ +main()blocks rather than placing code inthe global namespace of the different ways of getting input.
10 Command-line arguments, inter-active input, standard out and in, variable constants as input Arup Nielsen26 July 4, 2014 ReferencesReferencesMartelli, A., Ravenscroft, A. M., and Ascher, D., editors (2005). Python Cookbook. O Reilly, Sebastopol,California, 2nd Arup Nielsen27 July 4, 2014