Skip to content

Errata Chapter 8: Modules and Packages #18

Open

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

Activity

  1. jeff5 commented on Apr 16, 2020

    @jeff5
    Member

    This is another section that is quite different in the paper book. In this case, we have not yet fixed the on-line version under issue #14.

  2. heywiki commented on Apr 16, 2020

    @heywiki
    Author

    Thank you. I forked the version to keep the rationale for my example repo at https://github.com/heywiki/jython-book-examples for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions