Python Programming Scripting

3y ago
103.83 KB
28 Pages
Last View : 17d ago
Last Download : 4m ago
Upload by : Mara Blakely

Python programming — ScriptingFinn Årup NielsenDTU ComputeTechnical University of DenmarkJuly 4, 2014

Python scriptingOverviewHow to make a command-line script (as oppose to a module)?HeaderArgument parsingmainCommand-line inputStandard input/output and pipingFinn Årup Nielsen1July 4, 2014

Python scriptingNamingIt is not necessary to call a script run from the command-line with the’.py’ extension.Actually it might be better to hide the implementation (that it is writtenin python) from the user (for some operating systems).Finn Årup Nielsen2July 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(think virtualenv):#!/usr/bin/env pythonFinn Årup Nielsen3July 4, 2014

Python scriptingHeader in Windows-like environmentHashbang does not work in Windows.If you instead maintain the .py extension then you are able to ASSOC andFTYPE commands to associate a filetype to a specific program (such asthe python program. See the suggestion on Stack Overflow.Finn Årup Nielsen4July 4, 2014

Python scriptingCommand-line argument basicsCommand-line arguments are available in the sys.argv variable.With myscript consisting of#!/usr/bin/env pythonimport sysprint(sys.argv)Called with 3 command-line arguments: ./myscript --verbose -a 34 datafile.txt[’myscript’, ’--verbose’, ’-a 34’, ’datafile.txt’]Note there are four items in the list: The first element is the Pythonprogram name.Finn Årup Nielsen5July 4, 2014

Python scriptingArgument parsing in the old daysFor reading/parsing the command-line arguments in sys.argv you canwrite your own code, but there are developers who have written moduleto ease the handling of the arguments.In the old days you would have:getopt — Module in the standard library modeled after the C getoptfunction/library. Not necessarily recommended.optparse — In the standard library. Not necessarily recommended.argparse — Added to standard library from 2.7/3.2 see PEP 389. Newestmodule in the standard library and—argued—better than getopt andoptparse.Finn Årup Nielsen6July 4, 2014

Python scriptingargparse exampleA lot of code goes here.Finn Årup Nielsen7July 4, 2014

Python scriptingBut nowFinn Årup Nielsen8July 4, 2014

Python scriptingdocoptFinn Årup Nielsen9July 4, 2014

Python scriptingDocoptIdea: Use the documentation to describe the command-line interface —both for humans and the argument parsing code.Available for a number of programming languages.Reference implementation in Python by Vladimir Keleshev.No longer necessary to write much code only:import docoptargs docopt.docopt( doc , version version )The rest is documentation (and the code for actually using the commandline arguments)Finn Årup Nielsen10July 4, 2014

Python scriptingDocopt example#!/usr/bin/env python"""mydocopter.Usage: mydocopter [options] filename Options:-v --verbose Log messages-o OUTPUT --output OUTPUTOutput 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 3.2" datafile.txt ./mydocopter --verbose datafile.txt0.315471154631 -1.51271481921 5.64476836068Description:Fit a polynomial to data. The datafile should have x y values in each row"""Finn Årup Nielsen11July 4, 2014

Python scriptingWith just the following two lines you get ’usage’ and ’help’ working:import docoptargs docopt.docopt( doc , version 1.0)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 --helpmydocopter.Usage: mydocopter [options] filename . (and the rest of the docstring)Finn Årup Nielsen12July 4, 2014

Python scriptingWhat is in args?With this programimport docoptargs docopt.docopt( doc , version 1.0)print(args)Example outputs: mydocopter datafile.txt{’--output’: None, ’--verbose’: False, ’-a’: ’1.’, ’-b’: ’1.’,’-c’: ’1.’, ’ filename ’: ’datafile.txt’} mydocopter --verbose -b 3 datafile.txt{’--output’: None, ’--verbose’: True, ’-a’: ’1.’, ’-b’: ’3’,’-c’: ’1.’, ’ filename ’: ’datafile.txt’}Finn Årup Nielsen13July 4, 2014

Python scriptingthe code of a working programFinn Årup Nielsen14July 4, 2014

Python scriptingimport docopt, logging, scipy.optimizeargs docopt.docopt( doc , version 1.0)if (logging.INFO)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, line.split()) for line in open(args[’ filename ’]).readlines()]def cost function((a, b, c), data):return sum(map(lambda (x, y): (a*x**2 b*x c - y)**2, data))parameters scipy.optimize.fmin(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 f:f.write(" ".join(map(str, parameters)))Finn Årup Nielsen15July 4, 2014

Python scriptingDocopt detailsNotice short and long forms (-v and --verbose, -h and --help)Required fixed arguments, required variable argument ( filename ) andoptional (e.g., -a 3)Options with (e.g., -a 3) and without values (e.g., --verbose)Options with default values [default: 1.]Furthermore:You can have “or” arguments, e.g., (set remove)You can have multiple input arguments to a single name with “.”, e.g.,program filename . with parsed command-line element available in alist ’ filename ’: [’a.txt’, ’b.txt’, ’c.txt’]Finn Årup Nielsen16July 4, 2014

Python scripting‘Variable constants’ and input argumentsDo not usually use ‘constants that varies’(!?) in programs. Put them asinput arguments. It might be filename for output and input:"""Usage:myprogram [--output filename ] input """# Here goes program.Rather than hardcoded ‘constants’:# Here goes programINPUT FILENAME ’data 2014 first recording.csv’OUTPUT FILENAME ’data 2014 first recording analysis results.txt’.Finn Årup Nielsen17July 4, 2014

Python scriptingThe “ifname ’ main ’:” thingTo distinguish a script from a module.print(’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 hasthe .py extension.Documentation tools that require import (but does not execute the code)will benefit from this trick.Finn Årup Nielsen18July 4, 2014

Python scriptingThe def main() thingInstead of putting code in the name ’ main ’ block add a function(usual name: main), e.g., here with a module named main():print("This is the main function")if name ’ main ’:main()This construct allows you to call the “script” (i.e., fromanother module, e.g., like:import onemoduleonemodule.main()This would not have been possible if you put the line with print in theblock with name ’ main ’. See also python - why use def main()and the Google Python Style Guide.Finn Årup Nielsen19July 4, 2014

Python scriptingCommand-line inputPython interactive command-line interface to Python with coloring of ’5’import blessings, re, readline, rlcompleterreadline.parse and bind("tab: complete")term blessings.Terminal()# For tab completion# For coloring text outputwhile True:expr raw input(" ")try: eval(expr)print(re.sub(’5’, term.bold red on green(’5’), str( ),flags re.UNICODE))except:exec(expr)Note the behavior and existence of raw input() and input() is differentbetween Python 2 and Python 3.Finn Årup Nielsen20July 4, 2014

Python scriptingInput/output streamsraw input (Python 2) in Python 3 called inputinput (Python 2), the same as eval(input())getpass.getpass Input with hidden outputsys.stdin Standard input stream for interpreter inputsys.stdout Standard output streamsys.stderr Standard error streamThe original objects of the three latter are in sys. stdin etc.Finn Årup Nielsen21July 4, 2014

Python scriptingUnix pipe exampleExample with a Unix pipe: echo "Hallo" python -c "import sys; sys.stdout.write(’ ’ \ ’ \n’)" Hallo Finn Årup Nielsen22July 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 sys.stdin.fileno()old settings termios.tcgetattr(fd)try:tty.setraw(fd)ch, termios.TCSADRAIN, old settings)return chFinn Årup Nielsen23July 4, 2014

Python scripting. . . Reading unbuffereddef getchs():while True:yield getch()import sys# see also getpass module.for ch in getchs():sys.stdout.write(’*’)if ch ’c’:breakelif ch Finn Årup Nielsen24July 4, 2014

Python scriptingMore informationVladimir Keleshev’s YouTube video about docopt: PyCon UK 2012: Create *beautiful* command-line interfaces with PythonFinn Årup Nielsen25July 4, 2014

Python scriptingSummaryUse docopt: Easiest handling of command-line arguments, forces you todocument your script, ensures that your documentation and implementation do not get out of sync.Use name ’ main ’ main() blocks rather than placing code inthe global namespace of the module.Consider different ways of getting input: command-line arguments, interactive input, standard out and in, files.Put ‘variable constants’ as input arguments.Finn Årup Nielsen26July 4, 2014

ReferencesReferencesMartelli, A., Ravenscroft, A. M., and Ascher, D., editors (2005). Python Cookbook. O’Reilly, Sebastopol,California, 2nd edition.Finn Årup Nielsen27July 4, 2014

Available for a number of programming languages. Reference implementation in PythonbyVladimir Keleshev. No longer necessary to write much code only: import docopt args docopt.docopt(_doc_, version _version_) The rest is documentation (and the code for actually using the command-line arguments) Finn Arup Nielsen 10 July 4, 2014. Python scripting Docopt example #!/usr/bin/env python .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Python Programming - This is a textbook in Python Programming with lots of Practical Examples and Exercises. You will learn the necessary foundation for basic programming with focus on Python. Python for Science and Engineering - This is a textbook in Python Programming with lots of Examples, Exercises, and Practical Applications

Applications of traditional scripting languages are: 1. system administration, 2. experimental programming, 3. controlling applications. Application areas : Four main usage areas for scripting languages: 1. Command scripting languages 2.Application scripting languages 3.Markup language 4. Universal scripting languages 1.

CR ASH COURSE PY THON CR ASH COURSE 2ND EDITION ERIC MATTHES SHELVE IN: PROGRAMMING LANGUAGES/ PYTHON 39.95 ( 53.95 CDN) LEARN PYTHON— FAST! COVERS PYTHON 3.X Python Crash Course is the world's best-selling guide to the Python programming language. This fast-paced, thorough introduction to programming with Python will