Coverage for partitionsets/ordered_set.py: 88.89%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 22:17 +0100

1#! /usr/bin/env python 

2""" Minimal implementation of an ordered set for hashables 

3and feedback from [PyLint, Flake8]. 

4 

5References: 

6 

7[Flake8]: https://pypi.python.org/pypi/flake8 

8 

9[OrdSetImplPy]: http://code.activestate.com/recipes/576694/ (mixed with 

10 the simplified code from Don Sawatzky's comment, which is sufficient 

11 for this task) 

12 

13[PyLint]: https://pypi.python.org/pypi/pylint 

14 

15All python third party sources used are licensed under the MIT License. """ 

16 

17import sys 

18 

19if sys.version_info[0:2] > (3, 2): 19 ↛ 22line 19 didn't jump to line 22, because the condition on line 19 was never false

20 from collections.abc import Sequence 

21else: 

22 from collections import Sequence 

23 

24 

25class OrderedSet(Sequence): 

26 """Creates an ordered set from a list of tuples or 

27 other hashable items. cf. [OrdSetImplPy]""" 

28 

29 def __init__(self, hashable_items): 

30 """Save unique items of L in input order.""" 

31 self.__map = {} 

32 self.__oset = [] 

33 for item in hashable_items: 

34 if item not in self.__map: 34 ↛ 33line 34 didn't jump to line 33, because the condition on line 34 was never false

35 self.__map[item] = 1 

36 self.__oset.append(item) 

37 

38 def __getitem__(self, index): 

39 return self.__oset[index] 

40 

41 def __delitem__(self, index): 

42 pass 

43 

44 def __setitem__(self, index, value): 

45 pass 

46 

47 def __len__(self): 

48 return len(self.__oset)