associationproxy.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. # ext/associationproxy.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Contain the ``AssociationProxy`` class.
  8. The ``AssociationProxy`` is a Python property object which provides
  9. transparent proxied access to the endpoint of an association object.
  10. See the example ``examples/association/proxied_association.py``.
  11. """
  12. import itertools
  13. import operator
  14. import weakref
  15. from .. import exc, orm, util
  16. from ..orm import collections, interfaces
  17. from ..sql import not_, or_
  18. def association_proxy(target_collection, attr, **kw):
  19. r"""Return a Python property implementing a view of a target
  20. attribute which references an attribute on members of the
  21. target.
  22. The returned value is an instance of :class:`.AssociationProxy`.
  23. Implements a Python property representing a relationship as a collection
  24. of simpler values, or a scalar value. The proxied property will mimic
  25. the collection type of the target (list, dict or set), or, in the case of
  26. a one to one relationship, a simple scalar value.
  27. :param target_collection: Name of the attribute we'll proxy to.
  28. This attribute is typically mapped by
  29. :func:`~sqlalchemy.orm.relationship` to link to a target collection, but
  30. can also be a many-to-one or non-scalar relationship.
  31. :param attr: Attribute on the associated instance or instances we'll
  32. proxy for.
  33. For example, given a target collection of [obj1, obj2], a list created
  34. by this proxy property would look like [getattr(obj1, *attr*),
  35. getattr(obj2, *attr*)]
  36. If the relationship is one-to-one or otherwise uselist=False, then
  37. simply: getattr(obj, *attr*)
  38. :param creator: optional.
  39. When new items are added to this proxied collection, new instances of
  40. the class collected by the target collection will be created. For list
  41. and set collections, the target class constructor will be called with
  42. the 'value' for the new instance. For dict types, two arguments are
  43. passed: key and value.
  44. If you want to construct instances differently, supply a *creator*
  45. function that takes arguments as above and returns instances.
  46. For scalar relationships, creator() will be called if the target is None.
  47. If the target is present, set operations are proxied to setattr() on the
  48. associated object.
  49. If you have an associated object with multiple attributes, you may set
  50. up multiple association proxies mapping to different attributes. See
  51. the unit tests for examples, and for examples of how creator() functions
  52. can be used to construct the scalar relationship on-demand in this
  53. situation.
  54. :param \*\*kw: Passes along any other keyword arguments to
  55. :class:`.AssociationProxy`.
  56. """
  57. return AssociationProxy(target_collection, attr, **kw)
  58. ASSOCIATION_PROXY = util.symbol('ASSOCIATION_PROXY')
  59. """Symbol indicating an :class:`InspectionAttr` that's
  60. of type :class:`.AssociationProxy`.
  61. Is assigned to the :attr:`.InspectionAttr.extension_type`
  62. attibute.
  63. """
  64. class AssociationProxy(interfaces.InspectionAttrInfo):
  65. """A descriptor that presents a read/write view of an object attribute."""
  66. is_attribute = False
  67. extension_type = ASSOCIATION_PROXY
  68. def __init__(self, target_collection, attr, creator=None,
  69. getset_factory=None, proxy_factory=None,
  70. proxy_bulk_set=None, info=None):
  71. """Construct a new :class:`.AssociationProxy`.
  72. The :func:`.association_proxy` function is provided as the usual
  73. entrypoint here, though :class:`.AssociationProxy` can be instantiated
  74. and/or subclassed directly.
  75. :param target_collection: Name of the collection we'll proxy to,
  76. usually created with :func:`.relationship`.
  77. :param attr: Attribute on the collected instances we'll proxy
  78. for. For example, given a target collection of [obj1, obj2], a
  79. list created by this proxy property would look like
  80. [getattr(obj1, attr), getattr(obj2, attr)]
  81. :param creator: Optional. When new items are added to this proxied
  82. collection, new instances of the class collected by the target
  83. collection will be created. For list and set collections, the
  84. target class constructor will be called with the 'value' for the
  85. new instance. For dict types, two arguments are passed:
  86. key and value.
  87. If you want to construct instances differently, supply a 'creator'
  88. function that takes arguments as above and returns instances.
  89. :param getset_factory: Optional. Proxied attribute access is
  90. automatically handled by routines that get and set values based on
  91. the `attr` argument for this proxy.
  92. If you would like to customize this behavior, you may supply a
  93. `getset_factory` callable that produces a tuple of `getter` and
  94. `setter` functions. The factory is called with two arguments, the
  95. abstract type of the underlying collection and this proxy instance.
  96. :param proxy_factory: Optional. The type of collection to emulate is
  97. determined by sniffing the target collection. If your collection
  98. type can't be determined by duck typing or you'd like to use a
  99. different collection implementation, you may supply a factory
  100. function to produce those collections. Only applicable to
  101. non-scalar relationships.
  102. :param proxy_bulk_set: Optional, use with proxy_factory. See
  103. the _set() method for details.
  104. :param info: optional, will be assigned to
  105. :attr:`.AssociationProxy.info` if present.
  106. .. versionadded:: 1.0.9
  107. """
  108. self.target_collection = target_collection
  109. self.value_attr = attr
  110. self.creator = creator
  111. self.getset_factory = getset_factory
  112. self.proxy_factory = proxy_factory
  113. self.proxy_bulk_set = proxy_bulk_set
  114. self.owning_class = None
  115. self.key = '_%s_%s_%s' % (
  116. type(self).__name__, target_collection, id(self))
  117. self.collection_class = None
  118. if info:
  119. self.info = info
  120. @property
  121. def remote_attr(self):
  122. """The 'remote' :class:`.MapperProperty` referenced by this
  123. :class:`.AssociationProxy`.
  124. .. versionadded:: 0.7.3
  125. See also:
  126. :attr:`.AssociationProxy.attr`
  127. :attr:`.AssociationProxy.local_attr`
  128. """
  129. return getattr(self.target_class, self.value_attr)
  130. @property
  131. def local_attr(self):
  132. """The 'local' :class:`.MapperProperty` referenced by this
  133. :class:`.AssociationProxy`.
  134. .. versionadded:: 0.7.3
  135. See also:
  136. :attr:`.AssociationProxy.attr`
  137. :attr:`.AssociationProxy.remote_attr`
  138. """
  139. return getattr(self.owning_class, self.target_collection)
  140. @property
  141. def attr(self):
  142. """Return a tuple of ``(local_attr, remote_attr)``.
  143. This attribute is convenient when specifying a join
  144. using :meth:`.Query.join` across two relationships::
  145. sess.query(Parent).join(*Parent.proxied.attr)
  146. .. versionadded:: 0.7.3
  147. See also:
  148. :attr:`.AssociationProxy.local_attr`
  149. :attr:`.AssociationProxy.remote_attr`
  150. """
  151. return (self.local_attr, self.remote_attr)
  152. def _get_property(self):
  153. return (orm.class_mapper(self.owning_class).
  154. get_property(self.target_collection))
  155. @util.memoized_property
  156. def target_class(self):
  157. """The intermediary class handled by this :class:`.AssociationProxy`.
  158. Intercepted append/set/assignment events will result
  159. in the generation of new instances of this class.
  160. """
  161. return self._get_property().mapper.class_
  162. @util.memoized_property
  163. def scalar(self):
  164. """Return ``True`` if this :class:`.AssociationProxy` proxies a scalar
  165. relationship on the local side."""
  166. scalar = not self._get_property().uselist
  167. if scalar:
  168. self._initialize_scalar_accessors()
  169. return scalar
  170. @util.memoized_property
  171. def _value_is_scalar(self):
  172. return not self._get_property().\
  173. mapper.get_property(self.value_attr).uselist
  174. @util.memoized_property
  175. def _target_is_object(self):
  176. return getattr(self.target_class, self.value_attr).impl.uses_objects
  177. def __get__(self, obj, class_):
  178. if self.owning_class is None:
  179. self.owning_class = class_ and class_ or type(obj)
  180. if obj is None:
  181. return self
  182. if self.scalar:
  183. target = getattr(obj, self.target_collection)
  184. return self._scalar_get(target)
  185. else:
  186. try:
  187. # If the owning instance is reborn (orm session resurrect,
  188. # etc.), refresh the proxy cache.
  189. creator_id, proxy = getattr(obj, self.key)
  190. if id(obj) == creator_id:
  191. return proxy
  192. except AttributeError:
  193. pass
  194. proxy = self._new(_lazy_collection(obj, self.target_collection))
  195. setattr(obj, self.key, (id(obj), proxy))
  196. return proxy
  197. def __set__(self, obj, values):
  198. if self.owning_class is None:
  199. self.owning_class = type(obj)
  200. if self.scalar:
  201. creator = self.creator and self.creator or self.target_class
  202. target = getattr(obj, self.target_collection)
  203. if target is None:
  204. setattr(obj, self.target_collection, creator(values))
  205. else:
  206. self._scalar_set(target, values)
  207. else:
  208. proxy = self.__get__(obj, None)
  209. if proxy is not values:
  210. proxy.clear()
  211. self._set(proxy, values)
  212. def __delete__(self, obj):
  213. if self.owning_class is None:
  214. self.owning_class = type(obj)
  215. delattr(obj, self.key)
  216. def _initialize_scalar_accessors(self):
  217. if self.getset_factory:
  218. get, set = self.getset_factory(None, self)
  219. else:
  220. get, set = self._default_getset(None)
  221. self._scalar_get, self._scalar_set = get, set
  222. def _default_getset(self, collection_class):
  223. attr = self.value_attr
  224. _getter = operator.attrgetter(attr)
  225. getter = lambda target: _getter(target) if target is not None else None
  226. if collection_class is dict:
  227. setter = lambda o, k, v: setattr(o, attr, v)
  228. else:
  229. setter = lambda o, v: setattr(o, attr, v)
  230. return getter, setter
  231. def _new(self, lazy_collection):
  232. creator = self.creator and self.creator or self.target_class
  233. self.collection_class = util.duck_type_collection(lazy_collection())
  234. if self.proxy_factory:
  235. return self.proxy_factory(
  236. lazy_collection, creator, self.value_attr, self)
  237. if self.getset_factory:
  238. getter, setter = self.getset_factory(self.collection_class, self)
  239. else:
  240. getter, setter = self._default_getset(self.collection_class)
  241. if self.collection_class is list:
  242. return _AssociationList(
  243. lazy_collection, creator, getter, setter, self)
  244. elif self.collection_class is dict:
  245. return _AssociationDict(
  246. lazy_collection, creator, getter, setter, self)
  247. elif self.collection_class is set:
  248. return _AssociationSet(
  249. lazy_collection, creator, getter, setter, self)
  250. else:
  251. raise exc.ArgumentError(
  252. 'could not guess which interface to use for '
  253. 'collection_class "%s" backing "%s"; specify a '
  254. 'proxy_factory and proxy_bulk_set manually' %
  255. (self.collection_class.__name__, self.target_collection))
  256. def _inflate(self, proxy):
  257. creator = self.creator and self.creator or self.target_class
  258. if self.getset_factory:
  259. getter, setter = self.getset_factory(self.collection_class, self)
  260. else:
  261. getter, setter = self._default_getset(self.collection_class)
  262. proxy.creator = creator
  263. proxy.getter = getter
  264. proxy.setter = setter
  265. def _set(self, proxy, values):
  266. if self.proxy_bulk_set:
  267. self.proxy_bulk_set(proxy, values)
  268. elif self.collection_class is list:
  269. proxy.extend(values)
  270. elif self.collection_class is dict:
  271. proxy.update(values)
  272. elif self.collection_class is set:
  273. proxy.update(values)
  274. else:
  275. raise exc.ArgumentError(
  276. 'no proxy_bulk_set supplied for custom '
  277. 'collection_class implementation')
  278. @property
  279. def _comparator(self):
  280. return self._get_property().comparator
  281. def any(self, criterion=None, **kwargs):
  282. """Produce a proxied 'any' expression using EXISTS.
  283. This expression will be a composed product
  284. using the :meth:`.RelationshipProperty.Comparator.any`
  285. and/or :meth:`.RelationshipProperty.Comparator.has`
  286. operators of the underlying proxied attributes.
  287. """
  288. if self._target_is_object:
  289. if self._value_is_scalar:
  290. value_expr = getattr(
  291. self.target_class, self.value_attr).has(
  292. criterion, **kwargs)
  293. else:
  294. value_expr = getattr(
  295. self.target_class, self.value_attr).any(
  296. criterion, **kwargs)
  297. else:
  298. value_expr = criterion
  299. # check _value_is_scalar here, otherwise
  300. # we're scalar->scalar - call .any() so that
  301. # the "can't call any() on a scalar" msg is raised.
  302. if self.scalar and not self._value_is_scalar:
  303. return self._comparator.has(
  304. value_expr
  305. )
  306. else:
  307. return self._comparator.any(
  308. value_expr
  309. )
  310. def has(self, criterion=None, **kwargs):
  311. """Produce a proxied 'has' expression using EXISTS.
  312. This expression will be a composed product
  313. using the :meth:`.RelationshipProperty.Comparator.any`
  314. and/or :meth:`.RelationshipProperty.Comparator.has`
  315. operators of the underlying proxied attributes.
  316. """
  317. if self._target_is_object:
  318. return self._comparator.has(
  319. getattr(self.target_class, self.value_attr).
  320. has(criterion, **kwargs)
  321. )
  322. else:
  323. if criterion is not None or kwargs:
  324. raise exc.ArgumentError(
  325. "Non-empty has() not allowed for "
  326. "column-targeted association proxy; use ==")
  327. return self._comparator.has()
  328. def contains(self, obj):
  329. """Produce a proxied 'contains' expression using EXISTS.
  330. This expression will be a composed product
  331. using the :meth:`.RelationshipProperty.Comparator.any`
  332. , :meth:`.RelationshipProperty.Comparator.has`,
  333. and/or :meth:`.RelationshipProperty.Comparator.contains`
  334. operators of the underlying proxied attributes.
  335. """
  336. if self.scalar and not self._value_is_scalar:
  337. return self._comparator.has(
  338. getattr(self.target_class, self.value_attr).contains(obj)
  339. )
  340. else:
  341. return self._comparator.any(**{self.value_attr: obj})
  342. def __eq__(self, obj):
  343. # note the has() here will fail for collections; eq_()
  344. # is only allowed with a scalar.
  345. if obj is None:
  346. return or_(
  347. self._comparator.has(**{self.value_attr: obj}),
  348. self._comparator == None
  349. )
  350. else:
  351. return self._comparator.has(**{self.value_attr: obj})
  352. def __ne__(self, obj):
  353. # note the has() here will fail for collections; eq_()
  354. # is only allowed with a scalar.
  355. return self._comparator.has(
  356. getattr(self.target_class, self.value_attr) != obj)
  357. class _lazy_collection(object):
  358. def __init__(self, obj, target):
  359. self.ref = weakref.ref(obj)
  360. self.target = target
  361. def __call__(self):
  362. obj = self.ref()
  363. if obj is None:
  364. raise exc.InvalidRequestError(
  365. "stale association proxy, parent object has gone out of "
  366. "scope")
  367. return getattr(obj, self.target)
  368. def __getstate__(self):
  369. return {'obj': self.ref(), 'target': self.target}
  370. def __setstate__(self, state):
  371. self.ref = weakref.ref(state['obj'])
  372. self.target = state['target']
  373. class _AssociationCollection(object):
  374. def __init__(self, lazy_collection, creator, getter, setter, parent):
  375. """Constructs an _AssociationCollection.
  376. This will always be a subclass of either _AssociationList,
  377. _AssociationSet, or _AssociationDict.
  378. lazy_collection
  379. A callable returning a list-based collection of entities (usually an
  380. object attribute managed by a SQLAlchemy relationship())
  381. creator
  382. A function that creates new target entities. Given one parameter:
  383. value. This assertion is assumed::
  384. obj = creator(somevalue)
  385. assert getter(obj) == somevalue
  386. getter
  387. A function. Given an associated object, return the 'value'.
  388. setter
  389. A function. Given an associated object and a value, store that
  390. value on the object.
  391. """
  392. self.lazy_collection = lazy_collection
  393. self.creator = creator
  394. self.getter = getter
  395. self.setter = setter
  396. self.parent = parent
  397. col = property(lambda self: self.lazy_collection())
  398. def __len__(self):
  399. return len(self.col)
  400. def __bool__(self):
  401. return bool(self.col)
  402. __nonzero__ = __bool__
  403. def __getstate__(self):
  404. return {'parent': self.parent, 'lazy_collection': self.lazy_collection}
  405. def __setstate__(self, state):
  406. self.parent = state['parent']
  407. self.lazy_collection = state['lazy_collection']
  408. self.parent._inflate(self)
  409. class _AssociationList(_AssociationCollection):
  410. """Generic, converting, list-to-list proxy."""
  411. def _create(self, value):
  412. return self.creator(value)
  413. def _get(self, object):
  414. return self.getter(object)
  415. def _set(self, object, value):
  416. return self.setter(object, value)
  417. def __getitem__(self, index):
  418. if not isinstance(index, slice):
  419. return self._get(self.col[index])
  420. else:
  421. return [self._get(member) for member in self.col[index]]
  422. def __setitem__(self, index, value):
  423. if not isinstance(index, slice):
  424. self._set(self.col[index], value)
  425. else:
  426. if index.stop is None:
  427. stop = len(self)
  428. elif index.stop < 0:
  429. stop = len(self) + index.stop
  430. else:
  431. stop = index.stop
  432. step = index.step or 1
  433. start = index.start or 0
  434. rng = list(range(index.start or 0, stop, step))
  435. if step == 1:
  436. for i in rng:
  437. del self[start]
  438. i = start
  439. for item in value:
  440. self.insert(i, item)
  441. i += 1
  442. else:
  443. if len(value) != len(rng):
  444. raise ValueError(
  445. "attempt to assign sequence of size %s to "
  446. "extended slice of size %s" % (len(value),
  447. len(rng)))
  448. for i, item in zip(rng, value):
  449. self._set(self.col[i], item)
  450. def __delitem__(self, index):
  451. del self.col[index]
  452. def __contains__(self, value):
  453. for member in self.col:
  454. # testlib.pragma exempt:__eq__
  455. if self._get(member) == value:
  456. return True
  457. return False
  458. def __getslice__(self, start, end):
  459. return [self._get(member) for member in self.col[start:end]]
  460. def __setslice__(self, start, end, values):
  461. members = [self._create(v) for v in values]
  462. self.col[start:end] = members
  463. def __delslice__(self, start, end):
  464. del self.col[start:end]
  465. def __iter__(self):
  466. """Iterate over proxied values.
  467. For the actual domain objects, iterate over .col instead or
  468. just use the underlying collection directly from its property
  469. on the parent.
  470. """
  471. for member in self.col:
  472. yield self._get(member)
  473. return
  474. def append(self, value):
  475. item = self._create(value)
  476. self.col.append(item)
  477. def count(self, value):
  478. return sum([1 for _ in
  479. util.itertools_filter(lambda v: v == value, iter(self))])
  480. def extend(self, values):
  481. for v in values:
  482. self.append(v)
  483. def insert(self, index, value):
  484. self.col[index:index] = [self._create(value)]
  485. def pop(self, index=-1):
  486. return self.getter(self.col.pop(index))
  487. def remove(self, value):
  488. for i, val in enumerate(self):
  489. if val == value:
  490. del self.col[i]
  491. return
  492. raise ValueError("value not in list")
  493. def reverse(self):
  494. """Not supported, use reversed(mylist)"""
  495. raise NotImplementedError
  496. def sort(self):
  497. """Not supported, use sorted(mylist)"""
  498. raise NotImplementedError
  499. def clear(self):
  500. del self.col[0:len(self.col)]
  501. def __eq__(self, other):
  502. return list(self) == other
  503. def __ne__(self, other):
  504. return list(self) != other
  505. def __lt__(self, other):
  506. return list(self) < other
  507. def __le__(self, other):
  508. return list(self) <= other
  509. def __gt__(self, other):
  510. return list(self) > other
  511. def __ge__(self, other):
  512. return list(self) >= other
  513. def __cmp__(self, other):
  514. return cmp(list(self), other)
  515. def __add__(self, iterable):
  516. try:
  517. other = list(iterable)
  518. except TypeError:
  519. return NotImplemented
  520. return list(self) + other
  521. def __radd__(self, iterable):
  522. try:
  523. other = list(iterable)
  524. except TypeError:
  525. return NotImplemented
  526. return other + list(self)
  527. def __mul__(self, n):
  528. if not isinstance(n, int):
  529. return NotImplemented
  530. return list(self) * n
  531. __rmul__ = __mul__
  532. def __iadd__(self, iterable):
  533. self.extend(iterable)
  534. return self
  535. def __imul__(self, n):
  536. # unlike a regular list *=, proxied __imul__ will generate unique
  537. # backing objects for each copy. *= on proxied lists is a bit of
  538. # a stretch anyhow, and this interpretation of the __imul__ contract
  539. # is more plausibly useful than copying the backing objects.
  540. if not isinstance(n, int):
  541. return NotImplemented
  542. if n == 0:
  543. self.clear()
  544. elif n > 1:
  545. self.extend(list(self) * (n - 1))
  546. return self
  547. def copy(self):
  548. return list(self)
  549. def __repr__(self):
  550. return repr(list(self))
  551. def __hash__(self):
  552. raise TypeError("%s objects are unhashable" % type(self).__name__)
  553. for func_name, func in list(locals().items()):
  554. if (util.callable(func) and func.__name__ == func_name and
  555. not func.__doc__ and hasattr(list, func_name)):
  556. func.__doc__ = getattr(list, func_name).__doc__
  557. del func_name, func
  558. _NotProvided = util.symbol('_NotProvided')
  559. class _AssociationDict(_AssociationCollection):
  560. """Generic, converting, dict-to-dict proxy."""
  561. def _create(self, key, value):
  562. return self.creator(key, value)
  563. def _get(self, object):
  564. return self.getter(object)
  565. def _set(self, object, key, value):
  566. return self.setter(object, key, value)
  567. def __getitem__(self, key):
  568. return self._get(self.col[key])
  569. def __setitem__(self, key, value):
  570. if key in self.col:
  571. self._set(self.col[key], key, value)
  572. else:
  573. self.col[key] = self._create(key, value)
  574. def __delitem__(self, key):
  575. del self.col[key]
  576. def __contains__(self, key):
  577. # testlib.pragma exempt:__hash__
  578. return key in self.col
  579. def has_key(self, key):
  580. # testlib.pragma exempt:__hash__
  581. return key in self.col
  582. def __iter__(self):
  583. return iter(self.col.keys())
  584. def clear(self):
  585. self.col.clear()
  586. def __eq__(self, other):
  587. return dict(self) == other
  588. def __ne__(self, other):
  589. return dict(self) != other
  590. def __lt__(self, other):
  591. return dict(self) < other
  592. def __le__(self, other):
  593. return dict(self) <= other
  594. def __gt__(self, other):
  595. return dict(self) > other
  596. def __ge__(self, other):
  597. return dict(self) >= other
  598. def __cmp__(self, other):
  599. return cmp(dict(self), other)
  600. def __repr__(self):
  601. return repr(dict(self.items()))
  602. def get(self, key, default=None):
  603. try:
  604. return self[key]
  605. except KeyError:
  606. return default
  607. def setdefault(self, key, default=None):
  608. if key not in self.col:
  609. self.col[key] = self._create(key, default)
  610. return default
  611. else:
  612. return self[key]
  613. def keys(self):
  614. return self.col.keys()
  615. if util.py2k:
  616. def iteritems(self):
  617. return ((key, self._get(self.col[key])) for key in self.col)
  618. def itervalues(self):
  619. return (self._get(self.col[key]) for key in self.col)
  620. def iterkeys(self):
  621. return self.col.iterkeys()
  622. def values(self):
  623. return [self._get(member) for member in self.col.values()]
  624. def items(self):
  625. return [(k, self._get(self.col[k])) for k in self]
  626. else:
  627. def items(self):
  628. return ((key, self._get(self.col[key])) for key in self.col)
  629. def values(self):
  630. return (self._get(self.col[key]) for key in self.col)
  631. def pop(self, key, default=_NotProvided):
  632. if default is _NotProvided:
  633. member = self.col.pop(key)
  634. else:
  635. member = self.col.pop(key, default)
  636. return self._get(member)
  637. def popitem(self):
  638. item = self.col.popitem()
  639. return (item[0], self._get(item[1]))
  640. def update(self, *a, **kw):
  641. if len(a) > 1:
  642. raise TypeError('update expected at most 1 arguments, got %i' %
  643. len(a))
  644. elif len(a) == 1:
  645. seq_or_map = a[0]
  646. # discern dict from sequence - took the advice from
  647. # http://www.voidspace.org.uk/python/articles/duck_typing.shtml
  648. # still not perfect :(
  649. if hasattr(seq_or_map, 'keys'):
  650. for item in seq_or_map:
  651. self[item] = seq_or_map[item]
  652. else:
  653. try:
  654. for k, v in seq_or_map:
  655. self[k] = v
  656. except ValueError:
  657. raise ValueError(
  658. "dictionary update sequence "
  659. "requires 2-element tuples")
  660. for key, value in kw:
  661. self[key] = value
  662. def copy(self):
  663. return dict(self.items())
  664. def __hash__(self):
  665. raise TypeError("%s objects are unhashable" % type(self).__name__)
  666. for func_name, func in list(locals().items()):
  667. if (util.callable(func) and func.__name__ == func_name and
  668. not func.__doc__ and hasattr(dict, func_name)):
  669. func.__doc__ = getattr(dict, func_name).__doc__
  670. del func_name, func
  671. class _AssociationSet(_AssociationCollection):
  672. """Generic, converting, set-to-set proxy."""
  673. def _create(self, value):
  674. return self.creator(value)
  675. def _get(self, object):
  676. return self.getter(object)
  677. def _set(self, object, value):
  678. return self.setter(object, value)
  679. def __len__(self):
  680. return len(self.col)
  681. def __bool__(self):
  682. if self.col:
  683. return True
  684. else:
  685. return False
  686. __nonzero__ = __bool__
  687. def __contains__(self, value):
  688. for member in self.col:
  689. # testlib.pragma exempt:__eq__
  690. if self._get(member) == value:
  691. return True
  692. return False
  693. def __iter__(self):
  694. """Iterate over proxied values.
  695. For the actual domain objects, iterate over .col instead or just use
  696. the underlying collection directly from its property on the parent.
  697. """
  698. for member in self.col:
  699. yield self._get(member)
  700. return
  701. def add(self, value):
  702. if value not in self:
  703. self.col.add(self._create(value))
  704. # for discard and remove, choosing a more expensive check strategy rather
  705. # than call self.creator()
  706. def discard(self, value):
  707. for member in self.col:
  708. if self._get(member) == value:
  709. self.col.discard(member)
  710. break
  711. def remove(self, value):
  712. for member in self.col:
  713. if self._get(member) == value:
  714. self.col.discard(member)
  715. return
  716. raise KeyError(value)
  717. def pop(self):
  718. if not self.col:
  719. raise KeyError('pop from an empty set')
  720. member = self.col.pop()
  721. return self._get(member)
  722. def update(self, other):
  723. for value in other:
  724. self.add(value)
  725. def __ior__(self, other):
  726. if not collections._set_binops_check_strict(self, other):
  727. return NotImplemented
  728. for value in other:
  729. self.add(value)
  730. return self
  731. def _set(self):
  732. return set(iter(self))
  733. def union(self, other):
  734. return set(self).union(other)
  735. __or__ = union
  736. def difference(self, other):
  737. return set(self).difference(other)
  738. __sub__ = difference
  739. def difference_update(self, other):
  740. for value in other:
  741. self.discard(value)
  742. def __isub__(self, other):
  743. if not collections._set_binops_check_strict(self, other):
  744. return NotImplemented
  745. for value in other:
  746. self.discard(value)
  747. return self
  748. def intersection(self, other):
  749. return set(self).intersection(other)
  750. __and__ = intersection
  751. def intersection_update(self, other):
  752. want, have = self.intersection(other), set(self)
  753. remove, add = have - want, want - have
  754. for value in remove:
  755. self.remove(value)
  756. for value in add:
  757. self.add(value)
  758. def __iand__(self, other):
  759. if not collections._set_binops_check_strict(self, other):
  760. return NotImplemented
  761. want, have = self.intersection(other), set(self)
  762. remove, add = have - want, want - have
  763. for value in remove:
  764. self.remove(value)
  765. for value in add:
  766. self.add(value)
  767. return self
  768. def symmetric_difference(self, other):
  769. return set(self).symmetric_difference(other)
  770. __xor__ = symmetric_difference
  771. def symmetric_difference_update(self, other):
  772. want, have = self.symmetric_difference(other), set(self)
  773. remove, add = have - want, want - have
  774. for value in remove:
  775. self.remove(value)
  776. for value in add:
  777. self.add(value)
  778. def __ixor__(self, other):
  779. if not collections._set_binops_check_strict(self, other):
  780. return NotImplemented
  781. want, have = self.symmetric_difference(other), set(self)
  782. remove, add = have - want, want - have
  783. for value in remove:
  784. self.remove(value)
  785. for value in add:
  786. self.add(value)
  787. return self
  788. def issubset(self, other):
  789. return set(self).issubset(other)
  790. def issuperset(self, other):
  791. return set(self).issuperset(other)
  792. def clear(self):
  793. self.col.clear()
  794. def copy(self):
  795. return set(self)
  796. def __eq__(self, other):
  797. return set(self) == other
  798. def __ne__(self, other):
  799. return set(self) != other
  800. def __lt__(self, other):
  801. return set(self) < other
  802. def __le__(self, other):
  803. return set(self) <= other
  804. def __gt__(self, other):
  805. return set(self) > other
  806. def __ge__(self, other):
  807. return set(self) >= other
  808. def __repr__(self):
  809. return repr(set(self))
  810. def __hash__(self):
  811. raise TypeError("%s objects are unhashable" % type(self).__name__)
  812. for func_name, func in list(locals().items()):
  813. if (util.callable(func) and func.__name__ == func_name and
  814. not func.__doc__ and hasattr(set, func_name)):
  815. func.__doc__ = getattr(set, func_name).__doc__
  816. del func_name, func