def __ne__(self, other):
if isinstance(other, BaseMultiset):
return self._total != other._total or self._elements != other._elements
if isinstance(other, (set, frozenset)):
pass
elif not isinstance(other, Set):
return NotImplemented
if self._total != len(other):
return True
return not self._issubset(other, False)
python类Set()的实例源码
def __ior__(self, other):
if isinstance(other, (BaseMultiset, set, frozenset)):
pass
elif not isinstance(other, Set):
return NotImplemented
self.union_update(other)
return self
def __iand__(self, other):
if isinstance(other, (BaseMultiset, set, frozenset)):
pass
elif not isinstance(other, Set):
return NotImplemented
self.intersection_update(other)
return self
def __isub__(self, other):
if isinstance(other, (BaseMultiset, set, frozenset)):
pass
elif not isinstance(other, Set):
return NotImplemented
self.difference_update(other)
return self
def __ixor__(self, other):
if isinstance(other, (BaseMultiset, set, frozenset)):
pass
elif not isinstance(other, Set):
return NotImplemented
self.symmetric_difference_update(other)
return self
def test_Set(self):
for sample in [set, frozenset]:
self.assertIsInstance(sample(), Set)
self.assertTrue(issubclass(sample, Set))
self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__')
class MySet(Set):
def __contains__(self, x):
return False
def __len__(self):
return 0
def __iter__(self):
return iter([])
self.validate_comparison(MySet())
def test_hash_Set(self):
class OneTwoThreeSet(Set):
def __init__(self):
self.contents = [1, 2, 3]
def __contains__(self, x):
return x in self.contents
def __len__(self):
return len(self.contents)
def __iter__(self):
return iter(self.contents)
def __hash__(self):
return self._hash()
a, b = OneTwoThreeSet(), OneTwoThreeSet()
self.assertTrue(hash(a) == hash(b))
def test_issue16373(self):
# Recursion error comparing comparable and noncomparable
# Set instances
class MyComparableSet(Set):
def __contains__(self, x):
return False
def __len__(self):
return 0
def __iter__(self):
return iter([])
class MyNonComparableSet(Set):
def __contains__(self, x):
return False
def __len__(self):
return 0
def __iter__(self):
return iter([])
def __le__(self, x):
return NotImplemented
def __lt__(self, x):
return NotImplemented
cs = MyComparableSet()
ncs = MyNonComparableSet()
self.assertFalse(ncs < cs)
self.assertTrue(ncs <= cs)
self.assertFalse(ncs > cs)
self.assertTrue(ncs >= cs)
def machine():
"""
Return machine suffix to use in directory name when looking
for bootloader.
PyInstaller is reported to work even on ARM architecture. For that
case functions system() and architecture() are not enough.
Path to bootloader has to be composed from system(), architecture()
and machine() like:
'Linux-32bit-arm'
"""
mach = platform.machine()
if mach.startswith('arm'):
return 'arm'
else:
# Assume x86/x86_64 machine.
return None
# Set and get environment variables does not handle unicode strings correctly
# on Windows.
# Acting on os.environ instead of using getenv()/setenv()/unsetenv(),
# as suggested in <http://docs.python.org/library/os.html#os.environ>:
# "Calling putenv() directly does not change os.environ, so it's
# better to modify os.environ." (Same for unsetenv.)
def __new__(cls, *args, **kwds):
if _geqv(cls, Set):
raise TypeError("Type Set cannot be instantiated; "
"use set() instead")
return set.__new__(cls, *args, **kwds)
def __subclasscheck__(self, cls):
if issubclass(cls, Set):
return False
return super().__subclasscheck__(cls)
def __instancecheck__(self, obj):
if issubclass(obj.__class__, Set):
return False
return super().__instancecheck__(obj)
def repository():
@singledispatch
def _repogitory(obj):
return obj
def _register(cls, func=None):
if func is None:
return lambda f: _register(cls, f)
if isinstance(func, type):
if issubclass(func, ObjConverter):
func = func(cls)
if isinstance(func, ObjConverter):
func.repogitory = _repogitory
func = func.run
return _repogitory.org_register(cls, func)
_repogitory.org_register = _repogitory.register
_repogitory.register = _register
def fromSQLAlchemyModel(model, attrs=None, ignores=None):
names = [col.name for col in model.__table__.columns]
ObjConverter.build(_repogitory, model, names, attrs, ignores)
_repogitory.fromSQLAlchemyModel = fromSQLAlchemyModel
def fromDjangoModel(model, attrs, ignores):
ObjConverter.build(_repogitory, model, _django_get_all_field_names(model),
attrs, ignores)
_repogitory.fromDjangoModel = fromDjangoModel
def raw(obj):
return obj
_repogitory.register(str, raw)
def conv_seq(obj):
return tuple(_repogitory(o) for o in obj)
_repogitory.register(abc.Sequence, conv_seq)
_repogitory.register(abc.Set, conv_seq)
@_repogitory.register(abc.Mapping)
def conv_mapping(obj):
return {_repogitory(k):_repogitory(v) for k, v in obj.items()}
def conv_date(obj):
return obj.isoformat()
_repogitory.register(datetime.date, conv_date)
_repogitory.register(datetime.datetime, conv_date)
return _repogitory