Skip to main content

2012 | OriginalPaper | Buchkapitel

4. Input Data and Error Handling

verfasst von : Hans Petter Langtangen

Erschienen in: A Primer on Scientific Programming with Python

Verlag: Springer Berlin Heidelberg

Aktivieren Sie unsere intelligente Suche um passende Fachinhalte oder Patente zu finden.

search-config
loading …

Abstract

This chapter exemplifies various techniques for reading data into a program. Most emphasis is put on reading data from the command line. We introduce the concept of exceptions for dealing with invalid input or erroneous conditions in a program. The chapter also explains how to organize your Python code in your own modules (libraries).

Sie haben noch keine Lizenz? Dann Informieren Sie sich jetzt über unsere Produkte:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Fußnoten
1
Programmers know that any modification of the source code has a danger of introducing errors, so it is a good rule to change as little as possible in a program that works.
 
2
This property is quite remarkable in computer science – a string formula will in most other languages be much slower than if the formula were hardcoded inside a plain function.
 
3
The cml part of the name is an abbreviation for “command line”, and v1 denotes “version 1”, as usual.
 
4
We could write 9 instead of 9.0, in the formula for F, since C is guaranteed to be float, but it is safer to write 9.0. One could think of modifying the conversion of the command-line argument to eval(sys.argv[1]), and in that case C can easily be an int.
 
5
This implies that float is not called, and C is not initialized with a value.
 
6
The formula applies the so-called Actual/360 convention where the rate per day is computed as p/360, while n counts the actual number of days the money is in the bank. See “Day count convention” in Wikipedia for detailed information and page 560 for a Python module for computing the number of days between two dates.
 
7
The module file is executed from top to bottom during the import. With function definitions only in the module file, there will be no calculations or output from the import, just definitions of functions. This is the desirable behavior.
 
8
The join function on page 285 in Chapter 6.​3.​1, see also page 147, is more elegant and avoids the loop.
 
9
In this sample path, the slashes are Unix specific. On Windows you must use backward slashes and a raw string. A better solution is to express the path as os.path.join(os.pardir, os.pardir, ’mymodules’). This will work on all platforms.
 
10
This guarantees a root in [a,b]. However, f(a)f(b)<0 is not a necessary condition if there is an even number of roots in the initial interval.
 
11
It can be shown that (4.9) arises from (4.8) when the probability p of experiencing the event in a small time interval t/n is p=νt/n and we let n→∞.
 
Literatur
[2]
Zurück zum Zitat J. E. Grayson. Python and Tkinter Programming. Manning, Greenwich, 2000. J. E. Grayson. Python and Tkinter Programming. Manning, Greenwich, 2000.
[3]
Zurück zum Zitat D. Harms and K. McDonald. The Quick Python Book. Manning, Greenwich, 1999. D. Harms and K. McDonald. The Quick Python Book. Manning, Greenwich, 1999.
[5]
Zurück zum Zitat H. P. Langtangen. Python Scripting for Computational Science, volume 3 of Texts in Computational Science and Engineering. Springer, Berlin, 3rd edition, 2009. H. P. Langtangen. Python Scripting for Computational Science, volume 3 of Texts in Computational Science and Engineering. Springer, Berlin, 3rd edition, 2009.
[7]
Zurück zum Zitat M. Lutz. Programming Python. O’Reilly, Sebastopol, 2nd edition, 2001. M. Lutz. Programming Python. O’Reilly, Sebastopol, 2nd edition, 2001.
Metadaten
Titel
Input Data and Error Handling
verfasst von
Hans Petter Langtangen
Copyright-Jahr
2012
Verlag
Springer Berlin Heidelberg
DOI
https://doi.org/10.1007/978-3-642-30293-0_4