[Python-checkins] r54987 - sandbox/trunk/abc/abc.py
guido.van.rossum
python-checkins at python.org
Thu Apr 26 20:26:12 CEST 2007
Author: guido.van.rossum
Date: Thu Apr 26 20:26:08 2007
New Revision: 54987
Modified:
sandbox/trunk/abc/abc.py
Log:
Make it match the current PEP 3119 a little more (e.g. SizedMapping->Mapping).
Experiment with implementing set composition operations a little differently.
Modified: sandbox/trunk/abc/abc.py
==============================================================================
--- sandbox/trunk/abc/abc.py (original)
+++ sandbox/trunk/abc/abc.py Thu Apr 26 20:26:08 2007
@@ -15,6 +15,7 @@
__author__ = "Guido van Rossum <guido at python.org>"
import sys
+import itertools
### ABC SUPPORT FRAMEWORK ###
@@ -252,25 +253,33 @@
# XXX Alternatively, we might make these abstract.
+ @staticmethod
+ def fromiterable(it):
+ return frozenset(it)
+
def __and__(self, other):
- new = set(self)
- new.intersection_update(other)
- return frozenset(new)
+ if not isinstance(other, Iterable):
+ return NotImplemented
+ return self.fromiterable(value for value in other if value in self)
def __or__(self, other):
- new = set(self)
- new.update(other)
- return frozenset(new)
+ return self.fromiterable(itertools.chain(self, other))
def __xor__(self, other):
- new = set(self)
- new.symmetric_difference_update(other)
- return frozenset(new)
+ if not isinstance(other, Set):
+ if not isinstance(other, Iterable):
+ return NotImplemented
+ other = self.fromiterable(other)
+ return self.fromiterable(itertool.chain(
+ (value for value in self if value not in other),
+ (other for value in other if value not in self)))
def __sub__(self, other):
- new = set(self)
- new.difference_update(other)
- return frozenset(new)
+ if not isinstance(other, Set):
+ if not isinstance(other, Iterable):
+ return NotImplemented
+ other = self.fromiterable(other)
+ return self.fromiterable(value for value in self if value not in other)
# XXX Should this derive from Set instead of from ComposableSet?
@@ -448,19 +457,19 @@
yield self._mapping[key]
-class SizedMapping(IterableMapping, Sized):
+class Mapping(IterableMapping, Sized):
def keys(self):
- return SizedKeysView(self)
+ return KeysView(self)
def items(self):
- return SizedItemsView(self)
+ return ItemsView(self)
def values(self):
- return SizedValuesView(self)
+ return ValuesView(self)
def __eq__(self, other):
- if not isinstance(other, SizedMapping):
+ if not isinstance(other, Mapping):
return NotImplemented
if len(other) != len(self):
return False
@@ -476,21 +485,21 @@
return True
-class _SizedMappingView(_MappingView, Sized):
+class _MappingView(_MappingView, Sized):
def __len__(self):
return len(self._mapping)
-class SizedKeysView(_SizedMappingView, KeysView, Set):
+class KeysView(_MappingView, KeysView, Set):
pass
-class SizedItemsView(_SizedMappingView, ItemsView, Set):
+class ItemsView(_MappingView, ItemsView, Set):
pass
-class SizedValuesView(_SizedMappingView, ValuesView):
+class ValuesView(_MappingView, ValuesView):
def __eq__(self, other):
if not (isinstance(other, Sized) and isinstance(other, Iterable)):
@@ -723,10 +732,10 @@
return len(self.adaptee)
-class AdaptToMapping(SizedMapping):
+class AdaptToMapping(Mapping):
def __new__(cls, adaptee):
- self = SizedMapping.__new__(cls)
+ self = Mapping.__new__(cls)
self.adaptee = adaptee
return self
More information about the Python-checkins
mailing list