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