Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/coreschema/utils.py: 7%

25 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# This `uniq` implementation is taken from the Python `jsonschema` package. 

2# 

3# https://github.com/Julian/jsonschema 

4# 

5# Copyright (c) 2013 Julian Berman 

6# 

7# Permission is hereby granted, free of charge, to any person obtaining a copy 

8# of this software and associated documentation files (the "Software"), to deal 

9# in the Software without restriction, including without limitation the rights 

10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

11# copies of the Software, and to permit persons to whom the Software is 

12# furnished to do so, subject to the following conditions: 

13# 

14# The above copyright notice and this permission notice shall be included in 

15# all copies or substantial portions of the Software. 

16# 

17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

23# THE SOFTWARE. 

24import itertools 

25 

26 

27def unbool(element, true=object(), false=object()): 

28 """ 

29 A hack to make True and 1 and False and 0 unique for ``uniq``. 

30 """ 

31 

32 if element is True: 

33 return true 

34 elif element is False: 

35 return false 

36 return element 

37 

38 

39def uniq(container): 

40 """ 

41 Check if all of a container's elements are unique. 

42 Successively tries first to rely that the elements are hashable, then 

43 falls back on them being sortable, and finally falls back on brute 

44 force. 

45 """ 

46 

47 try: 

48 return len(set(unbool(i) for i in container)) == len(container) 

49 except TypeError: 

50 try: 

51 sort = sorted(unbool(i) for i in container) 

52 sliced = itertools.islice(sort, 1, None) 

53 for i, j in zip(sort, sliced): 

54 if i == j: 

55 return False 

56 except (NotImplementedError, TypeError): 

57 seen = [] 

58 for e in container: 

59 e = unbool(e) 

60 if e in seen: 

61 return False 

62 seen.append(e) 

63 return True