_multidict_base.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from collections.abc import ItemsView, Iterable, KeysView, Set, ValuesView
  2. def _abc_itemsview_register(view_cls):
  3. ItemsView.register(view_cls)
  4. def _abc_keysview_register(view_cls):
  5. KeysView.register(view_cls)
  6. def _abc_valuesview_register(view_cls):
  7. ValuesView.register(view_cls)
  8. def _viewbaseset_richcmp(view, other, op):
  9. if op == 0: # <
  10. if not isinstance(other, Set):
  11. return NotImplemented
  12. return len(view) < len(other) and view <= other
  13. elif op == 1: # <=
  14. if not isinstance(other, Set):
  15. return NotImplemented
  16. if len(view) > len(other):
  17. return False
  18. for elem in view:
  19. if elem not in other:
  20. return False
  21. return True
  22. elif op == 2: # ==
  23. if not isinstance(other, Set):
  24. return NotImplemented
  25. return len(view) == len(other) and view <= other
  26. elif op == 3: # !=
  27. return not view == other
  28. elif op == 4: # >
  29. if not isinstance(other, Set):
  30. return NotImplemented
  31. return len(view) > len(other) and view >= other
  32. elif op == 5: # >=
  33. if not isinstance(other, Set):
  34. return NotImplemented
  35. if len(view) < len(other):
  36. return False
  37. for elem in other:
  38. if elem not in view:
  39. return False
  40. return True
  41. def _viewbaseset_and(view, other):
  42. if not isinstance(other, Iterable):
  43. return NotImplemented
  44. if isinstance(view, Set):
  45. view = set(iter(view))
  46. if isinstance(other, Set):
  47. other = set(iter(other))
  48. if not isinstance(other, Set):
  49. other = set(iter(other))
  50. return view & other
  51. def _viewbaseset_or(view, other):
  52. if not isinstance(other, Iterable):
  53. return NotImplemented
  54. if isinstance(view, Set):
  55. view = set(iter(view))
  56. if isinstance(other, Set):
  57. other = set(iter(other))
  58. if not isinstance(other, Set):
  59. other = set(iter(other))
  60. return view | other
  61. def _viewbaseset_sub(view, other):
  62. if not isinstance(other, Iterable):
  63. return NotImplemented
  64. if isinstance(view, Set):
  65. view = set(iter(view))
  66. if isinstance(other, Set):
  67. other = set(iter(other))
  68. if not isinstance(other, Set):
  69. other = set(iter(other))
  70. return view - other
  71. def _viewbaseset_xor(view, other):
  72. if not isinstance(other, Iterable):
  73. return NotImplemented
  74. if isinstance(view, Set):
  75. view = set(iter(view))
  76. if isinstance(other, Set):
  77. other = set(iter(other))
  78. if not isinstance(other, Set):
  79. other = set(iter(other))
  80. return view ^ other
  81. def _itemsview_isdisjoint(view, other):
  82. "Return True if two sets have a null intersection."
  83. for v in other:
  84. if v in view:
  85. return False
  86. return True
  87. def _itemsview_repr(view):
  88. lst = []
  89. for k, v in view:
  90. lst.append("{!r}: {!r}".format(k, v))
  91. body = ", ".join(lst)
  92. return "{}({})".format(view.__class__.__name__, body)
  93. def _keysview_isdisjoint(view, other):
  94. "Return True if two sets have a null intersection."
  95. for k in other:
  96. if k in view:
  97. return False
  98. return True
  99. def _keysview_repr(view):
  100. lst = []
  101. for k in view:
  102. lst.append("{!r}".format(k))
  103. body = ", ".join(lst)
  104. return "{}({})".format(view.__class__.__name__, body)
  105. def _valuesview_repr(view):
  106. lst = []
  107. for v in view:
  108. lst.append("{!r}".format(v))
  109. body = ", ".join(lst)
  110. return "{}({})".format(view.__class__.__name__, body)
  111. def _mdrepr(md):
  112. lst = []
  113. for k, v in md.items():
  114. lst.append("'{}': {!r}".format(k, v))
  115. body = ", ".join(lst)
  116. return "<{}({})>".format(md.__class__.__name__, body)