1.7. GESTIÓN TURÍSTICA COMO ESTRATEGIAS DE ACCIÓN PARA EL
1.7.1. Tipos de Modelos de Gestión y Aspectos positivos de su aplicación
There is considerable flexibility in the language; two people can arrive at different presentations of Python source. Throughout this book we will present the guidelines for formatting, taken from the Python Enhance- ment Proposal (PEP) 8, posted onhttp://python.org/dev/peps/pep-0008/.
We’ll include guidelines that will make your programming consistent with the Python modules that are already part of your Python environment. These guidelines should also also make your programming look like other third-party programs available from vendors and posted on the Internet.
Python programs are meant to be readable. The language borrows a lot from common mathematical notation and from other programming languages. Many languages (C++ and Java) for instance, don’t require any particular formatting; line breaks and indendentation become merely conventions; bad-looking, hard-to-read programs are common. On the other hand, Python makes the line breaks and indentations part of the language, forcing you to create programs that are easier on the eyes.
General Notes. We’ll touch on many aspects of good Python style as we introduce each piece of Python programming. We haven’t seen much Python yet, but we do need some guidance to prevent a few tiny problems that could crop up.
First, Python (like all of Linux) is case sensitive. Some languages that are either all uppercase, or insensitive to case. We have worked with programmers who actually find it helpful to hse the Caps Lock key on their keyboard to expedite working in an all-upper-case world. Please don’t do this. Python should look like English, where lower-case letters predominate.
Second, Python makes use of indentation. Most programmers indent very nicely, and the compiler or interpreter ignores this. Python doesn’t ignore it. Indentation is useful for write clear, meaning documents and programs are no different.
Finally, your operating system allows a fairly large number of characters to appear in a file name. Until we start writing modules and packages, we can call our files anything that the operating system will tolerate. Starting inComponents, Modules and Packages, however, we’ll have to limit ourselves to filename that use only letters, digits and ‘_’‘s. There can be just one ending for the file: .py.
A file name likeexercise_1.pyis better than the nameexecise-1.py. We can run both programs equally well from the command line, but the name with the hyphen limits our ability to write larger and more sophisticated programs.
FIVE
SIMPLE NUMERIC EXPRESSIONS
AND OUTPUT
The print Statement and Numeric Operations
Basic expressions are the most central and useful feature of modern programming languages. To see the results of expressions, we’ll use theprintstatement.
This chapter starts out withSeeing Output with the print() Function (or print Statement), which covers the printstatement. Numeric Types and Operatorscovers the basic numeric data types and operators that are integral to writing expressions Python. Numeric Conversion (or “Factory”) Functions covers conversions between the various numeric types. Built-In Math Functions covers some of the built-in functions that Python provides.
5.1 Seeing Output with the
print()
Function (or print Statement)
Before delving into expressions and numbers, we’ll look at theprintstatement. We’ll cover just the essential syntax of theprintstatement; it has some odd syntax quirks that are painful to explain.
Note: Python 3.0
Python 3.0 will replace the irregular print statement with a built-in print() function that is perfectly regular, making it simpler to explain and use.
In order to use theprint() function instead of the print statement, your script (orIDLE session) must start off with the following.
from __future__ import print_function
This replaces theprint statement, with it’s irregular syntax with theprint()function.
5.1.1 print Statement Syntax Overview
Theprintstatement takes a list of values and, well, prints them. Speaking strictly, it does two things: 1. it converts the objects to strings and
2. puts the characters of those strings onstandard output.
Generally, standard output is the console window where Python was started, although there are ways to change this that are beyond the scope of this book.
Here’s a quick summary of the more important features of printstatement syntax. In short, the keyword, ‘print’, is followed by a comma-separated list of expressions.
print expression 〈 , ... 〉 Note: Syntax Summary
This syntax summary isn’t completely correct because it implies that the list of expressions is terminated with a comma. Rather than fuss around with complex syntax diagrams (that’s what the Python reference manual is for) we’ve shown an approximation that is close enough.
The ‘,’ in aprintstatement is used toseparatethe various expressions.
A ‘,’ can also be used at the end of theprint statement to change the formatting; this is an odd-but-true feature that is unique toprintstatement syntax.
It’s hard to capture this sublety in a single syntax diagram. Further, this is completely solved by using the
print()function.
One of the simplest kind of expressions is a quoted string. You can use either apostrophes (‘'’) or quotes (‘"’) to surround strings. This gives you some flexibility in your strings. You can put an apostrophe into a quoted string, and you can put quotes into an apostrophe’d string without the specialescapesthat some other languages require. The full set of quoting rules and alternatives, however, will have to wait forStrings. For example, the following trivial program prints three strings and two numbers.
print "Hi, Mom", "Isn't it lovely?", 'I said, "Hi".', 42, 91056
Multi-Line Output. Ordinarily, eachprintstatement produces one line of output. You can end theprint statement with a trailing ,to combine the results of multiple printstatements into a single line. Here are two examples.
print "335/113=", print 335.0/113.0
print "Hi, Mom", "Isn't it lovely?", print 'I said, "Hi".', 42, 91056
Since the first print statement ends with a , it does not produce a complete line of output. The second printstatement finishes the line of output.
Redirecting Output. The printstatement’s output goes to the operating system’s standard output file. How do we send output to the system’s standard error file? This involves some more advanced concepts, so we’ll introduce it with a two-part recipe that we need to look at in more depth. We’ll revisit these topics in Components, Modules and Packages.
First, you’ll need access to the standard error object; you get this via the following statement. import sys
Second, there is an unusual piece of syntax called a “chevron print” which can be used to redirect output to standard error. ‘>>’
Two common files aresys.stdoutand sys.stderr. We’ll return to files inFiles. Here is an example of a small script which produces messages on both stderr and stdout.
mixedout.py
#!/usr/bin/env python
"""Mixed output in stdout and stderr."""
import sys
print >>sys.stderr, "This is an error message" print "This is stdout"
print >>sys.stdout, "This is also stdout"
When you run this inside IDLE, you’ll notice that the stderr is colored red, where the stdout is colored black. You’ll also notice that the order of the output in IDLE doesn’t match the order in our program. Most POSIX operating systems buffer stdout, but does not buffer stderr. Consequently, stdout messages don’t appear until the buffer is full, or the program exits.
5.1.2 The
print()
Function
Python 3 replaces the relatively complex and irregularprintstatement with a simple and regularprint() function.
In Python 2.6 we can use this new function by doing the following: from __future__ import print_function
This statement must be one of the first executable statements in your script file. It makes a small – but profuound – change to Python syntax. The Python processor must be notified of this intended change up front.
This provides us with the following:
print([object, ...], [sep=’ ’], [end=’n’], [file=sys.stdout])
This will convert each object to a string, and then write the characters on the given file.
The separator between objects is – by default – a single space. Setting a value forsepwill set a different separator.
The end-of-line character is – by default – a single newline. Setting a value for endwill set a different end-of-line character.
To change output files, provide a value forfile.
Multiline Output. To create multiline output, do the following: from __future__ import print_function
print( "335/113=", end="" ) print( 335.0/113.0 )
print( "Hi, Mom", "Isn't it lovely?", end="" ) print( 'I said, "Hi".', 42, 91056 )
Redirecting Output. The printstatement’s output goes to the operating system’s standard output file. How do we send output to the system’s standard error file? This involves some more advanced concepts, so
we’ll introduce it with a two-part recipe that we need to look at in more depth. We’ll revisit these topics in Components, Modules and Packages.
First, you’ll need access to the standard error object.
Second, you’ll provide thefile option to theprint()function. from __future__ import print_function
import sys
print( "This is an error message", file=sys.stderr ) print( "This is stdout" )
print( "This is also stdout", file=sys.stdout )
Adding Features. You can – with some care – add features to theprint()function.
When we look at function definitions, we’ll look at how we can override the built-inprint()function to add our own unique features.
5.1.3 print Notes and Hints
A program produces a number of kinds of output. The print()function (or print statement) is a handy jumping-off point. Generally, we’ll replace this with more advanced techiques.
• Final Reports. Our desktop applications may produce text-based report files. These are often done withprint statements.
• PDF or other format output files. A desktop application which produces PDF or other format files will need to use additional libraries to produce PDF files. For example,ReportLaboffers PDF-production libraries. These applications won’t make extensive use of printstatements.
• Error messages and processing logs. Logs and errors are often directed to the standard error file. You won’t often use the printstatement for this, but use thelogginglibrary.
• Debugging messages. Debugging messages are often handled by the logginglibrary.
Theprint statement (orprint()function) is a very basic tool for debugging a complex Python program. Feel free to use print statements heavily to create a clear picture of what a program is actually doing. Ultimately, you are likely to replaceprintstatements with other, more sophisticated methods.