1
2
3
4
5
6
7
8
9 """
10 Backwards compatibility with previous versions of Python.
11
12 This module provides backwards compatibility by defining
13 functions and classes that were not available in earlier versions of
14 Python. Intented usage:
15
16 >>> from nltk.compat import *
17
18 Currently, NLTK requires Python 2.4 or later.
19 """
20
21
22
23
24
25
26
27 try:
28 from xml.etree import ElementTree
29 except ImportError:
30 from nltk.etree import ElementTree
31
32
33
34
35
36 try:
37 from collections import defaultdict
38 except ImportError:
40 - def __init__(self, default_factory=None, *a, **kw):
41 if (default_factory is not None and
42 not hasattr(default_factory, '__call__')):
43 raise TypeError('first argument must be callable')
44 dict.__init__(self, *a, **kw)
45 self.default_factory = default_factory
52 if self.default_factory is None:
53 raise KeyError(key)
54 self[key] = value = self.default_factory()
55 return value
57 if self.default_factory is None:
58 args = tuple()
59 else:
60 args = self.default_factory,
61 return type(self), args, None, None, self.iteritems()
63 return self.__copy__()
65 return type(self)(self.default_factory, self)
67 import copy
68 return type(self)(self.default_factory,
69 copy.deepcopy(self.items()))
71 return 'defaultdict(%s, %s)' % (self.default_factory,
72 dict.__repr__(self))
73
74
75 import collections
76 collections.defaultdict = defaultdict
77
78 __all__ = ['ElementTree', 'defaultdict']
79