python类Set()的实例源码

multiset.py 文件源码 项目:multiset 作者: wheerd 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
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)
multiset.py 文件源码 项目:multiset 作者: wheerd 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __ior__(self, other):
        if isinstance(other, (BaseMultiset, set, frozenset)):
            pass
        elif not isinstance(other, Set):
            return NotImplemented
        self.union_update(other)
        return self
multiset.py 文件源码 项目:multiset 作者: wheerd 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __iand__(self, other):
        if isinstance(other, (BaseMultiset, set, frozenset)):
            pass
        elif not isinstance(other, Set):
            return NotImplemented
        self.intersection_update(other)
        return self
multiset.py 文件源码 项目:multiset 作者: wheerd 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __isub__(self, other):
        if isinstance(other, (BaseMultiset, set, frozenset)):
            pass
        elif not isinstance(other, Set):
            return NotImplemented
        self.difference_update(other)
        return self
multiset.py 文件源码 项目:multiset 作者: wheerd 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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
test_collections.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
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())
test_collections.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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))
test_collections.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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)
compat.py 文件源码 项目:mac-package-build 作者: persepolisdm 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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.)
typing.py 文件源码 项目:meta 作者: Ejhfast 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
typing.py 文件源码 项目:meta 作者: Ejhfast 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __subclasscheck__(self, cls):
        if issubclass(cls, Set):
            return False
        return super().__subclasscheck__(cls)
typing.py 文件源码 项目:meta 作者: Ejhfast 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __instancecheck__(self, obj):
        if issubclass(obj.__class__, Set):
            return False
        return super().__instancecheck__(obj)
emitjson.py 文件源码 项目:emitjson 作者: atsuoishimoto 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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


问题


面经


文章

微信
公众号

扫码关注公众号