_toolz.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. Functionality copied from the toolz package to avoid having
  3. to add toolz as a dependency.
  4. See https://github.com/pytoolz/toolz/.
  5. toolz is relased under BSD licence. Below is the licence text
  6. from toolz as it appeared when copying the code.
  7. --------------------------------------------------------------
  8. Copyright (c) 2013 Matthew Rocklin
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. a. Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. b. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. c. Neither the name of toolz nor the names of its contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  24. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  30. DAMAGE.
  31. """
  32. import operator
  33. from six.moves import reduce
  34. def get_in(keys, coll, default=None, no_default=False):
  35. """
  36. NB: This is a straight copy of the get_in implementation found in
  37. the toolz library (https://github.com/pytoolz/toolz/). It works
  38. with persistent data structures as well as the corresponding
  39. datastructures from the stdlib.
  40. Returns coll[i0][i1]...[iX] where [i0, i1, ..., iX]==keys.
  41. If coll[i0][i1]...[iX] cannot be found, returns ``default``, unless
  42. ``no_default`` is specified, then it raises KeyError or IndexError.
  43. ``get_in`` is a generalization of ``operator.getitem`` for nested data
  44. structures such as dictionaries and lists.
  45. >>> from pyrsistent import freeze
  46. >>> transaction = freeze({'name': 'Alice',
  47. ... 'purchase': {'items': ['Apple', 'Orange'],
  48. ... 'costs': [0.50, 1.25]},
  49. ... 'credit card': '5555-1234-1234-1234'})
  50. >>> get_in(['purchase', 'items', 0], transaction)
  51. 'Apple'
  52. >>> get_in(['name'], transaction)
  53. 'Alice'
  54. >>> get_in(['purchase', 'total'], transaction)
  55. >>> get_in(['purchase', 'items', 'apple'], transaction)
  56. >>> get_in(['purchase', 'items', 10], transaction)
  57. >>> get_in(['purchase', 'total'], transaction, 0)
  58. 0
  59. >>> get_in(['y'], {}, no_default=True)
  60. Traceback (most recent call last):
  61. ...
  62. KeyError: 'y'
  63. """
  64. try:
  65. return reduce(operator.getitem, keys, coll)
  66. except (KeyError, IndexError, TypeError):
  67. if no_default:
  68. raise
  69. return default