Skip to content

Errata Chapter 8: Modules and Packages #18

@heywiki

Description

@heywiki

"The code for this program can be downloaded at XXX." -> so i created that on my own, see https://github.com/heywiki/jython-book-examples/tree/master/chapter7

what i discovered:

users have to start the program not by scanner.py like described, but by

jython searchdir.py . term

In searchdir.py you have to call a static method on ScanResults: scanner.ScanResults.scan(), not an object one, and exit() was not working, had to use sys.exit()

#from scanner import ScanResults
import search.scanner as scanner
import sys

help = """
Usage: search.py directory terms...
"""

args = sys.argv

if args == None or len(args) < 2:
    print help
    sys.exit()

dir = args[1]
terms = args[2:]
scan = scanner.ScanResults.scan(dir, terms)
scan.display()

Accordingly you have to add the @staticmethod annotation to ScanResults.scan

from search.walker import DirectoryWalker
from javax.swing import JFrame, JTable, WindowConstants

class ScanResults(object):
    def __init__(self):
        self.results = []

    def add(self, file, line):
        self.results.append((file, line))

    def display(self):
        colnames = ['file', 'line']
        table = JTable(self.results, colnames)
        frame = JFrame("%i Results" % len(self.results))
        frame.getContentPane().add(table)
        frame.size = 400, 300
        frame.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
        frame.visible = True

    @staticmethod
    def scan(dir, terms):
        results = ScanResults()
        for filename in DirectoryWalker(dir):
            for line in open(filename):
                for term in terms:
                    if term in line:
                        results.add(filename,line)
        return results

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions