Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pyrsistent/_toolz.py: 33%

9 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2Functionality copied from the toolz package to avoid having 

3to add toolz as a dependency. 

4 

5See https://github.com/pytoolz/toolz/. 

6 

7toolz is released under BSD licence. Below is the licence text 

8from toolz as it appeared when copying the code. 

9 

10-------------------------------------------------------------- 

11 

12Copyright (c) 2013 Matthew Rocklin 

13 

14All rights reserved. 

15 

16Redistribution and use in source and binary forms, with or without 

17modification, are permitted provided that the following conditions are met: 

18 

19 a. Redistributions of source code must retain the above copyright notice, 

20 this list of conditions and the following disclaimer. 

21 b. Redistributions in binary form must reproduce the above copyright 

22 notice, this list of conditions and the following disclaimer in the 

23 documentation and/or other materials provided with the distribution. 

24 c. Neither the name of toolz nor the names of its contributors 

25 may be used to endorse or promote products derived from this software 

26 without specific prior written permission. 

27 

28 

29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 

30AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 

31IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 

32ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR 

33ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 

34DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 

35SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 

36CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 

37LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 

38OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 

39DAMAGE. 

40""" 

41import operator 

42from functools import reduce 

43 

44 

45def get_in(keys, coll, default=None, no_default=False): 

46 """ 

47 NB: This is a straight copy of the get_in implementation found in 

48 the toolz library (https://github.com/pytoolz/toolz/). It works 

49 with persistent data structures as well as the corresponding 

50 datastructures from the stdlib. 

51 

52 Returns coll[i0][i1]...[iX] where [i0, i1, ..., iX]==keys. 

53 

54 If coll[i0][i1]...[iX] cannot be found, returns ``default``, unless 

55 ``no_default`` is specified, then it raises KeyError or IndexError. 

56 

57 ``get_in`` is a generalization of ``operator.getitem`` for nested data 

58 structures such as dictionaries and lists. 

59 >>> from pyrsistent import freeze 

60 >>> transaction = freeze({'name': 'Alice', 

61 ... 'purchase': {'items': ['Apple', 'Orange'], 

62 ... 'costs': [0.50, 1.25]}, 

63 ... 'credit card': '5555-1234-1234-1234'}) 

64 >>> get_in(['purchase', 'items', 0], transaction) 

65 'Apple' 

66 >>> get_in(['name'], transaction) 

67 'Alice' 

68 >>> get_in(['purchase', 'total'], transaction) 

69 >>> get_in(['purchase', 'items', 'apple'], transaction) 

70 >>> get_in(['purchase', 'items', 10], transaction) 

71 >>> get_in(['purchase', 'total'], transaction, 0) 

72 0 

73 >>> get_in(['y'], {}, no_default=True) 

74 Traceback (most recent call last): 

75 ... 

76 KeyError: 'y' 

77 """ 

78 try: 

79 return reduce(operator.getitem, keys, coll) 

80 except (KeyError, IndexError, TypeError): 

81 if no_default: 

82 raise 

83 return default