Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/bitstring/helpers.py: 13%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

30 statements  

1from typing import Union, Tuple 

2 

3 

4def _indices(s: slice, length: int) -> Tuple[int, Union[int, None], int]: 

5 """A better implementation of slice.indices such that a 

6 slice made from [start:stop:step] will actually equal the original slice.""" 

7 if s.step is None or s.step > 0: 

8 return s.indices(length) 

9 assert s.step < 0 

10 start, stop, step = s.indices(length) 

11 if stop < 0: 

12 stop = None 

13 return start, stop, step 

14 

15def offset_slice_indices_lsb0(key: slice, length: int) -> slice: 

16 start, stop, step = _indices(key, length) 

17 if step is not None and step < 0: 

18 if stop is None: 

19 new_start = start + 1 

20 new_stop = None 

21 else: 

22 first_element = start 

23 last_element = start + ((stop + 1 - start) // step) * step 

24 new_start = length - last_element 

25 new_stop = length - first_element - 1 

26 else: 

27 first_element = start 

28 # The last element will usually be stop - 1, but needs to be adjusted if step != 1. 

29 last_element = start + ((stop - 1 - start) // step) * step 

30 new_start = length - last_element - 1 

31 new_stop = length - first_element 

32 return slice(new_start, new_stop, key.step) 

33 

34def tidy_input_string(s: str) -> str: 

35 """Return string made lowercase and with all whitespace and underscores removed.""" 

36 try: 

37 t = s.split() 

38 except (AttributeError, TypeError): 

39 raise ValueError(f"Expected str object but received a {type(s)} with value {s}.") 

40 return ''.join(t).lower().replace('_', '')