Brett Cannon | 23a4a7b | 2008-05-12 00:56:28 +0000 | [diff] [blame] | 1 | __all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList', |
| 2 | 'UserString'] |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3 | # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. |
| 4 | # They should however be considered an integral part of collections.py. |
| 5 | from _abcoll import * |
| 6 | import _abcoll |
| 7 | __all__ += _abcoll.__all__ |
| 8 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 9 | from _collections import deque, defaultdict |
| 10 | from operator import itemgetter as _itemgetter |
| 11 | from keyword import iskeyword as _iskeyword |
| 12 | import sys as _sys |
| 13 | |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 14 | ################################################################################ |
| 15 | ### namedtuple |
| 16 | ################################################################################ |
| 17 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 18 | def namedtuple(typename, field_names, verbose=False): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | """Returns a new subclass of tuple with named fields. |
| 20 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 21 | >>> Point = namedtuple('Point', 'x y') |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 22 | >>> Point.__doc__ # docstring for the new class |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | 'Point(x, y)' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 24 | >>> p = Point(11, y=22) # instantiate with positional args or keywords |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 25 | >>> p[0] + p[1] # indexable like a plain tuple |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | 33 |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 27 | >>> x, y = p # unpack like a regular tuple |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 28 | >>> x, y |
| 29 | (11, 22) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 30 | >>> p.x + p.y # fields also accessable by name |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 31 | 33 |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 32 | >>> d = p._asdict() # convert to a dictionary |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 33 | >>> d['x'] |
| 34 | 11 |
| 35 | >>> Point(**d) # convert from a dictionary |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 36 | Point(x=11, y=22) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 37 | >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 38 | Point(x=100, y=22) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | |
| 40 | """ |
| 41 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 42 | # Parse and validate the field names. Validation serves two purposes, |
| 43 | # generating informative error messages and preventing template injection attacks. |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 44 | if isinstance(field_names, str): |
| 45 | field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas |
| 46 | field_names = tuple(field_names) |
| 47 | for name in (typename,) + field_names: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 48 | if not all(c.isalnum() or c=='_' for c in name): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 49 | raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name) |
| 50 | if _iskeyword(name): |
| 51 | raise ValueError('Type names and field names cannot be a keyword: %r' % name) |
| 52 | if name[0].isdigit(): |
| 53 | raise ValueError('Type names and field names cannot start with a number: %r' % name) |
| 54 | seen_names = set() |
| 55 | for name in field_names: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 56 | if name.startswith('_'): |
| 57 | raise ValueError('Field names cannot start with an underscore: %r' % name) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 58 | if name in seen_names: |
| 59 | raise ValueError('Encountered duplicate field name: %r' % name) |
| 60 | seen_names.add(name) |
| 61 | |
| 62 | # Create and fill-in the class template |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 63 | numfields = len(field_names) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 64 | argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 65 | reprtxt = ', '.join('%s=%%r' % name for name in field_names) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 66 | dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 67 | template = '''class %(typename)s(tuple): |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 68 | '%(typename)s(%(argtxt)s)' \n |
| 69 | __slots__ = () \n |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 70 | _fields = %(field_names)r \n |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 71 | def __new__(cls, %(argtxt)s): |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 72 | return tuple.__new__(cls, (%(argtxt)s)) \n |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 73 | @classmethod |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 74 | def _make(cls, iterable, new=tuple.__new__, len=len): |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 75 | 'Make a new %(typename)s object from a sequence or iterable' |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 76 | result = new(cls, iterable) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 77 | if len(result) != %(numfields)d: |
| 78 | raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result)) |
| 79 | return result \n |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 80 | def __repr__(self): |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 81 | return '%(typename)s(%(reprtxt)s)' %% self \n |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 82 | def _asdict(t): |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 83 | 'Return a new dict which maps field names to their values' |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 84 | return {%(dicttxt)s} \n |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 85 | def _replace(self, **kwds): |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 86 | 'Return a new %(typename)s object replacing specified fields with new values' |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 87 | result = self._make(map(kwds.pop, %(field_names)r, self)) |
| 88 | if kwds: |
| 89 | raise ValueError('Got unexpected field names: %%r' %% kwds.keys()) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 90 | return result \n |
| 91 | def __getnewargs__(self): |
| 92 | return tuple(self) \n\n''' % locals() |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 93 | for i, name in enumerate(field_names): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 94 | template += ' %s = property(itemgetter(%d))\n' % (name, i) |
| 95 | if verbose: |
| 96 | print(template) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 97 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 98 | # Execute the template string in a temporary namespace and |
| 99 | # support tracing utilities by setting a value for frame.f_globals['__name__'] |
| 100 | namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 101 | try: |
| 102 | exec(template, namespace) |
| 103 | except SyntaxError as e: |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 104 | raise SyntaxError(e.msg + ':\n' + template) from e |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 105 | result = namespace[typename] |
| 106 | |
| 107 | # For pickling to work, the __module__ variable needs to be set to the frame |
| 108 | # where the named tuple is created. Bypass this step in enviroments where |
| 109 | # sys._getframe is not defined (Jython for example). |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 110 | if hasattr(_sys, '_getframe'): |
| 111 | result.__module__ = _sys._getframe(1).f_globals['__name__'] |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 113 | return result |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 116 | |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 117 | ################################################################################ |
| 118 | ### UserDict |
| 119 | ################################################################################ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 120 | |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 121 | class UserDict(MutableMapping): |
| 122 | |
| 123 | # Start by filling-out the abstract methods |
| 124 | def __init__(self, dict=None, **kwargs): |
| 125 | self.data = {} |
| 126 | if dict is not None: |
| 127 | self.update(dict) |
| 128 | if len(kwargs): |
| 129 | self.update(kwargs) |
| 130 | def __len__(self): return len(self.data) |
| 131 | def __getitem__(self, key): |
| 132 | if key in self.data: |
| 133 | return self.data[key] |
| 134 | if hasattr(self.__class__, "__missing__"): |
| 135 | return self.__class__.__missing__(self, key) |
| 136 | raise KeyError(key) |
| 137 | def __setitem__(self, key, item): self.data[key] = item |
| 138 | def __delitem__(self, key): del self.data[key] |
| 139 | def __iter__(self): |
| 140 | return iter(self.data) |
| 141 | |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 142 | # Modify __contains__ to work correctly when __missing__ is present |
| 143 | def __contains__(self, key): |
| 144 | return key in self.data |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 145 | |
| 146 | # Now, add the methods in dicts but not in MutableMapping |
| 147 | def __repr__(self): return repr(self.data) |
| 148 | def copy(self): |
| 149 | if self.__class__ is UserDict: |
| 150 | return UserDict(self.data.copy()) |
| 151 | import copy |
| 152 | data = self.data |
| 153 | try: |
| 154 | self.data = {} |
| 155 | c = copy.copy(self) |
| 156 | finally: |
| 157 | self.data = data |
| 158 | c.update(self) |
| 159 | return c |
| 160 | @classmethod |
| 161 | def fromkeys(cls, iterable, value=None): |
| 162 | d = cls() |
| 163 | for key in iterable: |
| 164 | d[key] = value |
| 165 | return d |
| 166 | |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | ################################################################################ |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 170 | ### UserList |
| 171 | ################################################################################ |
| 172 | |
| 173 | class UserList(MutableSequence): |
| 174 | """A more or less complete user-defined wrapper around list objects.""" |
| 175 | def __init__(self, initlist=None): |
| 176 | self.data = [] |
| 177 | if initlist is not None: |
| 178 | # XXX should this accept an arbitrary sequence? |
| 179 | if type(initlist) == type(self.data): |
| 180 | self.data[:] = initlist |
| 181 | elif isinstance(initlist, UserList): |
| 182 | self.data[:] = initlist.data[:] |
| 183 | else: |
| 184 | self.data = list(initlist) |
| 185 | def __repr__(self): return repr(self.data) |
| 186 | def __lt__(self, other): return self.data < self.__cast(other) |
| 187 | def __le__(self, other): return self.data <= self.__cast(other) |
| 188 | def __eq__(self, other): return self.data == self.__cast(other) |
| 189 | def __ne__(self, other): return self.data != self.__cast(other) |
| 190 | def __gt__(self, other): return self.data > self.__cast(other) |
| 191 | def __ge__(self, other): return self.data >= self.__cast(other) |
| 192 | def __cast(self, other): |
| 193 | return other.data if isinstance(other, UserList) else other |
| 194 | def __cmp__(self, other): |
| 195 | return cmp(self.data, self.__cast(other)) |
| 196 | def __contains__(self, item): return item in self.data |
| 197 | def __len__(self): return len(self.data) |
| 198 | def __getitem__(self, i): return self.data[i] |
| 199 | def __setitem__(self, i, item): self.data[i] = item |
| 200 | def __delitem__(self, i): del self.data[i] |
| 201 | def __add__(self, other): |
| 202 | if isinstance(other, UserList): |
| 203 | return self.__class__(self.data + other.data) |
| 204 | elif isinstance(other, type(self.data)): |
| 205 | return self.__class__(self.data + other) |
| 206 | return self.__class__(self.data + list(other)) |
| 207 | def __radd__(self, other): |
| 208 | if isinstance(other, UserList): |
| 209 | return self.__class__(other.data + self.data) |
| 210 | elif isinstance(other, type(self.data)): |
| 211 | return self.__class__(other + self.data) |
| 212 | return self.__class__(list(other) + self.data) |
| 213 | def __iadd__(self, other): |
| 214 | if isinstance(other, UserList): |
| 215 | self.data += other.data |
| 216 | elif isinstance(other, type(self.data)): |
| 217 | self.data += other |
| 218 | else: |
| 219 | self.data += list(other) |
| 220 | return self |
| 221 | def __mul__(self, n): |
| 222 | return self.__class__(self.data*n) |
| 223 | __rmul__ = __mul__ |
| 224 | def __imul__(self, n): |
| 225 | self.data *= n |
| 226 | return self |
| 227 | def append(self, item): self.data.append(item) |
| 228 | def insert(self, i, item): self.data.insert(i, item) |
| 229 | def pop(self, i=-1): return self.data.pop(i) |
| 230 | def remove(self, item): self.data.remove(item) |
| 231 | def count(self, item): return self.data.count(item) |
| 232 | def index(self, item, *args): return self.data.index(item, *args) |
| 233 | def reverse(self): self.data.reverse() |
| 234 | def sort(self, *args, **kwds): self.data.sort(*args, **kwds) |
| 235 | def extend(self, other): |
| 236 | if isinstance(other, UserList): |
| 237 | self.data.extend(other.data) |
| 238 | else: |
| 239 | self.data.extend(other) |
| 240 | |
| 241 | |
| 242 | |
| 243 | ################################################################################ |
Raymond Hettinger | b3a65f8 | 2008-02-21 22:11:37 +0000 | [diff] [blame] | 244 | ### UserString |
| 245 | ################################################################################ |
| 246 | |
| 247 | class UserString(Sequence): |
| 248 | def __init__(self, seq): |
| 249 | if isinstance(seq, str): |
| 250 | self.data = seq |
| 251 | elif isinstance(seq, UserString): |
| 252 | self.data = seq.data[:] |
| 253 | else: |
| 254 | self.data = str(seq) |
| 255 | def __str__(self): return str(self.data) |
| 256 | def __repr__(self): return repr(self.data) |
| 257 | def __int__(self): return int(self.data) |
| 258 | def __long__(self): return int(self.data) |
| 259 | def __float__(self): return float(self.data) |
| 260 | def __complex__(self): return complex(self.data) |
| 261 | def __hash__(self): return hash(self.data) |
| 262 | |
| 263 | def __eq__(self, string): |
| 264 | if isinstance(string, UserString): |
| 265 | return self.data == string.data |
| 266 | return self.data == string |
| 267 | def __ne__(self, string): |
| 268 | if isinstance(string, UserString): |
| 269 | return self.data != string.data |
| 270 | return self.data != string |
| 271 | def __lt__(self, string): |
| 272 | if isinstance(string, UserString): |
| 273 | return self.data < string.data |
| 274 | return self.data < string |
| 275 | def __le__(self, string): |
| 276 | if isinstance(string, UserString): |
| 277 | return self.data <= string.data |
| 278 | return self.data <= string |
| 279 | def __gt__(self, string): |
| 280 | if isinstance(string, UserString): |
| 281 | return self.data > string.data |
| 282 | return self.data > string |
| 283 | def __ge__(self, string): |
| 284 | if isinstance(string, UserString): |
| 285 | return self.data >= string.data |
| 286 | return self.data >= string |
| 287 | |
| 288 | def __contains__(self, char): |
| 289 | if isinstance(char, UserString): |
| 290 | char = char.data |
| 291 | return char in self.data |
| 292 | |
| 293 | def __len__(self): return len(self.data) |
| 294 | def __getitem__(self, index): return self.__class__(self.data[index]) |
| 295 | def __add__(self, other): |
| 296 | if isinstance(other, UserString): |
| 297 | return self.__class__(self.data + other.data) |
| 298 | elif isinstance(other, str): |
| 299 | return self.__class__(self.data + other) |
| 300 | return self.__class__(self.data + str(other)) |
| 301 | def __radd__(self, other): |
| 302 | if isinstance(other, str): |
| 303 | return self.__class__(other + self.data) |
| 304 | return self.__class__(str(other) + self.data) |
| 305 | def __mul__(self, n): |
| 306 | return self.__class__(self.data*n) |
| 307 | __rmul__ = __mul__ |
| 308 | def __mod__(self, args): |
| 309 | return self.__class__(self.data % args) |
| 310 | |
| 311 | # the following methods are defined in alphabetical order: |
| 312 | def capitalize(self): return self.__class__(self.data.capitalize()) |
| 313 | def center(self, width, *args): |
| 314 | return self.__class__(self.data.center(width, *args)) |
| 315 | def count(self, sub, start=0, end=_sys.maxsize): |
| 316 | if isinstance(sub, UserString): |
| 317 | sub = sub.data |
| 318 | return self.data.count(sub, start, end) |
| 319 | def encode(self, encoding=None, errors=None): # XXX improve this? |
| 320 | if encoding: |
| 321 | if errors: |
| 322 | return self.__class__(self.data.encode(encoding, errors)) |
| 323 | return self.__class__(self.data.encode(encoding)) |
| 324 | return self.__class__(self.data.encode()) |
| 325 | def endswith(self, suffix, start=0, end=_sys.maxsize): |
| 326 | return self.data.endswith(suffix, start, end) |
| 327 | def expandtabs(self, tabsize=8): |
| 328 | return self.__class__(self.data.expandtabs(tabsize)) |
| 329 | def find(self, sub, start=0, end=_sys.maxsize): |
| 330 | if isinstance(sub, UserString): |
| 331 | sub = sub.data |
| 332 | return self.data.find(sub, start, end) |
| 333 | def format(self, *args, **kwds): |
| 334 | return self.data.format(*args, **kwds) |
| 335 | def index(self, sub, start=0, end=_sys.maxsize): |
| 336 | return self.data.index(sub, start, end) |
| 337 | def isalpha(self): return self.data.isalpha() |
| 338 | def isalnum(self): return self.data.isalnum() |
| 339 | def isdecimal(self): return self.data.isdecimal() |
| 340 | def isdigit(self): return self.data.isdigit() |
| 341 | def isidentifier(self): return self.data.isidentifier() |
| 342 | def islower(self): return self.data.islower() |
| 343 | def isnumeric(self): return self.data.isnumeric() |
| 344 | def isspace(self): return self.data.isspace() |
| 345 | def istitle(self): return self.data.istitle() |
| 346 | def isupper(self): return self.data.isupper() |
| 347 | def join(self, seq): return self.data.join(seq) |
| 348 | def ljust(self, width, *args): |
| 349 | return self.__class__(self.data.ljust(width, *args)) |
| 350 | def lower(self): return self.__class__(self.data.lower()) |
| 351 | def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) |
| 352 | def partition(self, sep): |
| 353 | return self.data.partition(sep) |
| 354 | def replace(self, old, new, maxsplit=-1): |
| 355 | if isinstance(old, UserString): |
| 356 | old = old.data |
| 357 | if isinstance(new, UserString): |
| 358 | new = new.data |
| 359 | return self.__class__(self.data.replace(old, new, maxsplit)) |
| 360 | def rfind(self, sub, start=0, end=_sys.maxsize): |
| 361 | return self.data.rfind(sub, start, end) |
| 362 | def rindex(self, sub, start=0, end=_sys.maxsize): |
| 363 | return self.data.rindex(sub, start, end) |
| 364 | def rjust(self, width, *args): |
| 365 | return self.__class__(self.data.rjust(width, *args)) |
| 366 | def rpartition(self, sep): |
| 367 | return self.data.rpartition(sep) |
| 368 | def rstrip(self, chars=None): |
| 369 | return self.__class__(self.data.rstrip(chars)) |
| 370 | def split(self, sep=None, maxsplit=-1): |
| 371 | return self.data.split(sep, maxsplit) |
| 372 | def rsplit(self, sep=None, maxsplit=-1): |
| 373 | return self.data.rsplit(sep, maxsplit) |
| 374 | def splitlines(self, keepends=0): return self.data.splitlines(keepends) |
| 375 | def startswith(self, prefix, start=0, end=_sys.maxsize): |
| 376 | return self.data.startswith(prefix, start, end) |
| 377 | def strip(self, chars=None): return self.__class__(self.data.strip(chars)) |
| 378 | def swapcase(self): return self.__class__(self.data.swapcase()) |
| 379 | def title(self): return self.__class__(self.data.title()) |
| 380 | def translate(self, *args): |
| 381 | return self.__class__(self.data.translate(*args)) |
| 382 | def upper(self): return self.__class__(self.data.upper()) |
| 383 | def zfill(self, width): return self.__class__(self.data.zfill(width)) |
| 384 | |
| 385 | |
| 386 | |
| 387 | ################################################################################ |
Raymond Hettinger | 48b8b66 | 2008-02-05 01:53:00 +0000 | [diff] [blame] | 388 | ### Simple tests |
| 389 | ################################################################################ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 390 | |
| 391 | if __name__ == '__main__': |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 392 | # verify that instances can be pickled |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 393 | from pickle import loads, dumps |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 394 | Point = namedtuple('Point', 'x, y', True) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 395 | p = Point(x=10, y=20) |
| 396 | assert p == loads(dumps(p)) |
| 397 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 398 | # test and demonstrate ability to override methods |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 399 | class Point(namedtuple('Point', 'x y')): |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 400 | __slots__ = () |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 401 | @property |
| 402 | def hypot(self): |
| 403 | return (self.x ** 2 + self.y ** 2) ** 0.5 |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 404 | def __str__(self): |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 405 | return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 406 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 407 | for p in Point(3, 4), Point(14, 5/7.): |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 408 | print (p) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 409 | |
| 410 | class Point(namedtuple('Point', 'x y')): |
| 411 | 'Point class with optimized _make() and _replace() without error-checking' |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 412 | __slots__ = () |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 413 | _make = classmethod(tuple.__new__) |
| 414 | def _replace(self, _map=map, **kwds): |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 415 | return self._make(_map(kwds.get, ('x', 'y'), self)) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 416 | |
| 417 | print(Point(11, 22)._replace(x=100)) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 418 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 419 | Point3D = namedtuple('Point3D', Point._fields + ('z',)) |
| 420 | print(Point3D.__doc__) |
| 421 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 422 | import doctest |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 423 | TestResults = namedtuple('TestResults', 'failed attempted') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 424 | print(TestResults(*doctest.testmod())) |