《笨办法学python》(learn python the hard way 3thrd)ex49的代码实现以及疑惑

本文记录了在学习《笨办法学python》时遇到的ParserError问题,并通过调整引用方式成功解决。介绍了parser.py中错误处理类ParserError的使用场景及测试文件parser_tests.py的具体测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在跟随《笨办法学python》(learn python the hard way)学习python的过程中,做到习题49的时候,需要写测试文件。但是测试过程中ParserError是一个类,但是解释器却报错说出现了全局变量未定义的问题,我不太明白,请大神帮忙解答一下。
这是解析文档,来自于书本的源文件,parser.py

 #-*- coding: utf-8 -*-

class ParserError(Exception):
    pass

class Sentence(object):

    def __init__(self, subject, verb, object):
        self.subject = subject[1]
        self.verb = verb[1]
        self.object = object[1]

def peek(word_list):
    if word_list:
        word = word_list[0]
        return word[0]
    else:
        return None
#取出来下一个元组,原来的列表减1,如果类型匹配,返回元组
def match(word_list, expecting):
    if word_list:
        word = word_list.pop(0)

        if word[0] == expecting:
            return word
        else:
            return None
    else:
        return None

def skip(word_list, word_type):
    while peek(word_list) == word_type:
        return match(word_list, word_type)

def parse_verb(word_list):
    skip(word_list, 'stop')

    if peek(word_list) == 'verb':
        return match(word_list, 'verb')
    else:
        raise ParserError("Expected a verb next.")

def parse_object(word_list):
    skip(word_list, 'stop')
    next = peek(word_list)

    if next == 'noun':
        return match(word_list, 'noun')
    if next == 'direction':
        return match(word_list, 'direction')
    else:
        raise ParserError("Expected a noun or direction next.")

def parse_subject(word_list, subj):
    verb = parse_verb(word_list)
    obj = parse_object(word_list)

    return Sentence(subj, verb, obj)

def parse_sentence(word_list):
    skip(word_list, 'stop')

    start = peek(word_list)

    if start == 'noun':
        subj = match(word_list, 'noun')
        return parse_subject(word_list, subj)
    elif start == 'verb':
        #assume the subject is the player then
        return parse_subject(word_list, ('noun', 'player'))
    else:
        raise ParserError("Must start with subject, object, or verb not: %s" % start )

这是我自己写的测试文档,parser_tests.py

 from nose.tools import *
from ex49 import parser


def test_peek():
    test1 = [('stop', 'the'), ('noun', 'door')]
    result = parser.peek(test1)
    assert_equal(result, 'stop')

def test_match():
    test1 = [('stop', 'the'), ('noun', 'door')]
    result = parser.match(test1, 'stop')
    assert_equal(result, ('stop', 'the'))

def test_skip():
    test1 = [('direction', 'north'), ('noun', 'door')]  
    result = parser.skip(test1, 'direction')
    assert_equal(result, ('direction', 'north'))


def test_parse_verb():
    test1 = [('stop', 'the'), ('verb', 'go'), ('noun', 'door')] 

    result = parser.parse_verb(test1)
    assert_equal(result, ('verb', 'go'))

    assert_raises(ParserError, parser.parse_verb, test1)


def test_parse_object():
    test1 = [('stop', 'the'),('noun', 'door'), ('direction', 'north'),('verb', 'go') ]  

    result = parser.parse_object(test1)
    assert_equal(result, ('noun', 'door'))

    result = parser.parse_object(test1)
    assert_equal(result, ('direction', 'north'))

    assert_raises(ParserError, parser.parse_object, test1)


def test_parse_subject():
    test1 = [('stop', 'the'),('verb', 'go'), ('noun', 'door')]  

    result = parser.parse_subject(test1, ('noun', 'wswsr'))
    assert_equal(result.subject, 'wswsr')
    assert_equal(result.verb, 'go')
    assert_equal(result.object, 'door')

def test_parse_sentence():
    test1 = [('stop', 'the'), ('noun', 'door'), ('verb', 'go'), ('noun', 'bear')]   

    result = parser.parse_sentence(test1)
    assert_equal(result.subject, 'door')
    assert_equal(result.verb, 'go')
    assert_equal(result.object, 'bear')

    test2 = [('stop', 'the'), ('verb', 'go'), ('noun', 'bear')] 

    result = parser.parse_sentence(test2)
    assert_equal(result.subject, 'player')
    assert_equal(result.verb, 'go')
    assert_equal(result.object, 'bear')

    test3 = [('direction', 'north'), ('noun', 'bear')]  

    assert_raises(ParserError, parser.parse_sentence, test3)

我在powershell中输入nosetests tests\parser_tests.py出现了该问题,如图
图片说明

请大神帮忙解答一下,非常感谢!!

总结:

经过最近一段时间对python的学习,自己的编程能力有了不少提高。python这门语言的确非常简洁易用,十分值得学习!《笨办法学python》这本书我就看到49讲,50-52讲是讲关于web编程的,距离我的使用目的相差十万八千里,语言是一个工具,是用来解决实际问题的,秉持这个原则,我打算下一步开始入门学习机器学习方面的模块和理论知识了。
最后,摘抄Zed A.Shaw的<老程序员的建议>中的一些话,以供自己借鉴。

编程语言这东西并不重要,重要的是你用这些语言做的事情。事实上,我一直清楚这一点,不过以前我会周期性地被各种语言分神而忘记了这一点。现在我是永远不会忘记这一点了,你也不应该忘记这一点。
你学的和用的编程语言并不重要。不要被围绕某一种语言的“宗教”把你扯进去,这只会让你忘掉语言的真正目的–作为你的工具来实现有趣的事情。

—————————————-我是分界线—————————————————————————————-

感谢网友“yangzetuoyuan”提出的宝贵意见,将ParserError改为parser.ParserError以后结果就正常了。具体背后的原因是什么可能还需要查看源代码,以后有机会再去探究吧,也希望有哪位网友找到原因后能告知一下,不胜感激!
这里写图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值