Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errata Chapter 8: Modules and Packages #18

Open
heywiki opened this issue Apr 15, 2020 · 2 comments
Open

Errata Chapter 8: Modules and Packages #18

heywiki opened this issue Apr 15, 2020 · 2 comments

Comments

@heywiki
Copy link

heywiki commented Apr 15, 2020

"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
@jeff5
Copy link
Member

jeff5 commented Apr 16, 2020

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.

@heywiki
Copy link
Author

heywiki commented Apr 16, 2020

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants