My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Natural Language Toolkit: Compatibility Functions
#
# Copyright (C) 2001-2011 NLTK Project
# Author: Steven Bird <sb@csse.unimelb.edu.au>
# Edward Loper <edloper@gradient.cis.upenn.edu>
# URL: <http://www.nltk.org/>
# For license information, see LICENSE.TXT

"""
Backwards compatibility with previous versions of Python.

This module provides backwards compatibility by defining
functions and classes that were not available in earlier versions of
Python. Intented usage:

>>> from nltk.compat import *

Currently, NLTK requires Python 2.4 or later.
"""

######################################################################
# New in Python 2.5
######################################################################

# ElementTree

try:
from xml.etree import ElementTree
except ImportError:
from nltk.etree import ElementTree

# collections.defaultdict
# originally contributed by Yoav Goldberg <yoav.goldberg@gmail.com>
# new version by Jason Kirtland from Python cookbook.
# <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523034>
try:
from collections import defaultdict
except ImportError:
class defaultdict(dict):
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not hasattr(default_factory, '__call__')):
raise TypeError('first argument must be callable')
dict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.iteritems()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
def __repr__(self):
return 'defaultdict(%s, %s)' % (self.default_factory,
dict.__repr__(self))

# [XX] to make pickle happy in python 2.4:
import collections
collections.defaultdict = defaultdict

# all, any

try:
all([True])
all = all
except NameError:
def all(iterable):
for i in iterable:
if not i:
return False
else:
return True

try:
any([True])
any = any
except NameError:
def any(iterable):
for i in iterable:
if i:
return True
else:
return False


__all__ = ['ElementTree', 'defaultdict', 'all', 'any']

Change log

r8730 by StevenBird1 on Mar 7, 2011   Diff
Updated NLTK copyright year range from
2001-2010 to 2001-2011
Go to: 
Sign in to write a code review

Older revisions

r8479 by StevenBird1 on Jan 12, 2010   Diff
Updated copyright period to 2001-2010
r8316 by StevenBird1 on Sep 4, 2009   Diff
Fixed compat.py definition of all(),
following suggestion from Peter
Ljunglof; added any()
r8312 by StevenBird1 on Aug 26, 2009   Diff
Fixes for Python 2.4, for  issue 196 .
All revisions of this file

File info

Size: 3139 bytes, 103 lines
Powered by Google Project Hosting