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