Next / Previous / Contents / TCC Help System / NM Tech homepage

Abstract

Describes Python classes that assist in scanning and reporting errors on data read from a stream.

This publication is available in Web form and also as a PDF document. Please forward any comments to tcc-doc@nmt.edu.

Table of Contents

1. Introduction
2. Files available online
3. The singleton Log object
4. The Scan class: Managing progress through a stream
5. The code: Overview
6. Prologue to logscan.py
7. Imports
8. Manifest constants
8.1. DEFAULT_PREFIX
8.2. ERROR_KIND
8.3. WARNING_KIND
8.4. WHITE_PATTERN
8.5. INT_PATTERN
8.6. FLOAT_PATTERN
9. class Log
10. Log.addLogFile(): Log to another file
11. Log.setPrefix(): Change the prefix
12. Log.msgKind(): Send a message of a given kind
13. Log.error(): Send an error message
14. Log.warning(): Send a warning message
15. Log.message(): General message, not counted
16. Log.write(): Send a message to all current outputs
17. Log.fatal(): Write a message and stop
18. Log.count(): How many messages of a given kind?
19. logtest: Test driver for Log
20. The Scan class
21. Scan.__init__(): The constructor
22. Scan.__findInput(): Find the input stream
23. Scan.close(): Close the stream
24. Scan.atEndLine(): Are we at the end of the current line?
25. Scan.nextLine(): Advance to the next line
26. Scan.error(): Issue an error message
27. Scan.msgKind(): General message writer
28. Scan.move(): Advance the scan position
29. Scan.tab(): Move to a specific position
30. Scan.__effPos(): Calculate an effective position
31. Scan.isPos(): Is the current position a given value?
32. Scan.find(): Search for a constant string
33. Scan.upToRe(): Search for a regular expression match
34. Scan.deblankFile(): Skip whitespace over multiple lines
35. Scan.deblankLine(): Skip whitespace on the current line
36. Scan.match(): Match a specific string
37. Scan.matchArb(): Case-insensitive match
38. Scan.tabMatch(): Advance if there is a match
39. Scan.tabMatchArb(): Case-insensitive match and move
40. Scan.reMatch(): Match a regular expression
41. Scan.tabReMatch(): Match and advance the position
42. Scan.integer(): Parse an integer
43. Scan.fixed(): Parse a float constant
44. Scan.flatInt(): Parse a fixed-size integer field
45. The singleton class: A classic design pattern
45.1. Code prologue for singleton.py
45.2. class Singleton
45.3. The .__new__() method
46. Test driver for singleton

1. Introduction

This document describes three related Python classes that the author has found useful for many kinds of input processing:

  • The singleton Log object is used to centralize the processing of errors and error messages.

  • The Scan class is used to coordinate the scanning of an input stream with the generation of error messages related to that stream.

In the first sections of this document, we will describe the interfaces to these classes. Later sections will walk you through the actual code.