Skip to main content
Top

2012 | OriginalPaper | Chapter

4. Input Data and Error Handling

Author : Hans Petter Langtangen

Published in: A Primer on Scientific Programming with Python

Publisher: Springer Berlin Heidelberg

Activate our intelligent search to find suitable subject content or patents.

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).

Dont have a licence yet? Then find out more about our products and how to get one now:

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!

Footnotes
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→∞.
 
Literature
[2]
go back to reference J. E. Grayson. Python and Tkinter Programming. Manning, Greenwich, 2000. J. E. Grayson. Python and Tkinter Programming. Manning, Greenwich, 2000.
[3]
go back to reference 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]
go back to reference 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]
go back to reference M. Lutz. Programming Python. O’Reilly, Sebastopol, 2nd edition, 2001. M. Lutz. Programming Python. O’Reilly, Sebastopol, 2nd edition, 2001.
Metadata
Title
Input Data and Error Handling
Author
Hans Petter Langtangen
Copyright Year
2012
Publisher
Springer Berlin Heidelberg
DOI
https://doi.org/10.1007/978-3-642-30293-0_4

Premium Partner