| .. Copyright (C) 2001-2023 NLTK Project
|
| .. For license information, see LICENSE.TXT
|
|
|
| ==========================================
|
| Unit tests for the nltk.utilities module
|
| ==========================================
|
|
|
| overridden()
|
| ~~~~~~~~~~~~
|
| >>> from nltk.internals import overridden
|
|
|
| The typical use case is in defining methods for an interface or
|
| abstract base class, in such a way that subclasses don
|
| implement all of the methods:
|
|
|
| >>> class EaterI(object):
|
| ...
|
| ... def eat(self, food):
|
| ... if overridden(self.batch_eat):
|
| ... return self.batch_eat([food])[0]
|
| ... else:
|
| ... raise NotImplementedError()
|
| ... def batch_eat(self, foods):
|
| ... return [self.eat(food) for food in foods]
|
|
|
| As long as a subclass implements one method, it will be used to
|
| perform the other method:
|
|
|
| >>> class GoodEater1(EaterI):
|
| ... def eat(self, food):
|
| ... return
|
| >>> GoodEater1().eat(
|
|
|
| >>> GoodEater1().batch_eat([
|
| [
|
|
|
| >>> class GoodEater2(EaterI):
|
| ... def batch_eat(self, foods):
|
| ... return [
|
| >>> GoodEater2().eat(
|
|
|
| >>> GoodEater2().batch_eat([
|
| [
|
|
|
| But if a subclass doesn
|
| error when they try to call them. (nb this is better than infinite
|
| recursion):
|
|
|
| >>> class BadEater1(EaterI):
|
| ... pass
|
| >>> BadEater1().eat(
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
| >>> BadEater1().batch_eat([
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
|
|
| Trying to use the abstract base class itself will also result in an
|
| error:
|
|
|
| >>> class EaterI(EaterI):
|
| ... pass
|
| >>> EaterI().eat(
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
| >>> EaterI().batch_eat([
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
|
|
| It
|
|
|
| >>> class AbstractEater(EaterI):
|
| ... pass
|
|
|
| >>> class GoodEater3(AbstractEater):
|
| ... def eat(self, food):
|
| ... return
|
| ...
|
| >>> GoodEater3().eat(
|
|
|
| >>> GoodEater3().batch_eat([
|
| [
|
|
|
| >>> class GoodEater4(AbstractEater):
|
| ... def batch_eat(self, foods):
|
| ... return [
|
| >>> GoodEater4().eat(
|
|
|
| >>> GoodEater4().batch_eat([
|
| [
|
|
|
| >>> class BadEater2(AbstractEater):
|
| ... pass
|
| >>> BadEater2().eat(
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
| >>> BadEater2().batch_eat([
|
| Traceback (most recent call last):
|
| . . .
|
| NotImplementedError
|
|
|
| Here
|
|
|
| >>> class A(object):
|
| ... def f(x): pass
|
| >>> class B(A):
|
| ... def f(x): pass
|
| >>> class C(A): pass
|
| >>> class D(B): pass
|
|
|
| >>> overridden(A().f)
|
| False
|
| >>> overridden(B().f)
|
| True
|
| >>> overridden(C().f)
|
| False
|
| >>> overridden(D().f)
|
| True
|
|
|
| It works for classic classes, too:
|
|
|
| >>> class A:
|
| ... def f(x): pass
|
| >>> class B(A):
|
| ... def f(x): pass
|
| >>> class C(A): pass
|
| >>> class D(B): pass
|
| >>> overridden(A().f)
|
| False
|
| >>> overridden(B().f)
|
| True
|
| >>> overridden(C().f)
|
| False
|
| >>> overridden(D().f)
|
| True
|
|
|
|
|
| read_str()
|
| ~~~~~~~~~~~~
|
| >>> from nltk.internals import read_str
|
|
|
| Test valid scenarios
|
|
|
| >>> read_str("'valid string'", 0)
|
| (
|
|
|
| Now test invalid scenarios
|
|
|
| >>> read_str("should error", 0)
|
| Traceback (most recent call last):
|
| ...
|
| nltk.internals.ReadError: Expected open quote at 0
|
| >>> read_str("'should error", 0)
|
| Traceback (most recent call last):
|
| ...
|
| nltk.internals.ReadError: Expected close quote at 1
|
|
|