Method: rich._ratio.ratio_resolve
Calls: 13, Exceptions: 0, Paths: 4Back
Path 1: 9 calls (0.69)
100 (5) 10 (1) 60 (1) 20 (1) 110 (1)
list (9)
[5, 5] (1) [30, 30] (1) [10, 10] (1) [100] (1) [50, 50] (1) [20, 40, 40] (1) [40, 40, 20] (1) [33, 33, 34] (1) [36, 37, 37] (1)
1def ratio_resolve(total: int, edges: Sequence[Edge]) -> List[int]:
2 """Divide total space to satisfy size, ratio, and minimum_size, constraints.
3
4 The returned list of integers should add up to total in most cases, unless it is
5 impossible to satisfy all the constraints. For instance, if there are two edges
6 with a minimum size of 20 each and `total` is 30 then the returned list will be
7 greater than total. In practice, this would mean that a Layout object would
8 clip the rows that would overflow the screen height.
9
10 Args:
11 total (int): Total number of characters.
12 edges (List[Edge]): Edges within total space.
13
14 Returns:
15 List[int]: Number of characters for each edge.
16 """
17 # Size of edge or None for yet to be determined
18 sizes = [(edge.size or None) for edge in edges]
19
20 _Fraction = Fraction
21
22 # While any edges haven't been calculated
23 while None in sizes:
24 # Get flexible edges and index to map these back on to sizes list
25 flexible_edges = [
26 (index, edge)
27 for index, (size, edge) in enumerate(zip(sizes, edges))
28 if size is None
29 ]
30 # Remaining space in total
31 remaining = total - sum(size or 0 for size in sizes)
32 if remaining <= 0:
33 # No room for flexible edges
34 return [
35 ((edge.minimum_size or 1) if size is None else size)
36 for size, edge in zip(sizes, edges)
37 ]
38 # Calculate number of characters in a ratio portion
39 portion = _Fraction(
40 remaining, sum((edge.ratio or 1) for _, edge in flexible_edges)
41 )
42
43 # If any edges will be less than their minimum, replace size with the minimum
44 for index, edge in flexible_edges:
45 if portion * edge.ratio <= edge.minimum_size:
46 sizes[index] = edge.minimum_size
47 # New fixed size will invalidate calculations, so we need to repeat the process
48 break
49 else:
50 # Distribute flexible space and compensate for rounding error
51 # Since edge sizes can only be integers we need to add the remainder
52 # to the following line
53 remainder = _Fraction(0)
54 for index, edge in flexible_edges:
55 size, remainder = divmod(portion * edge.ratio + remainder, 1)
56 sizes[index] = size
57 break
58 # Sizes now contains integers only
59 return cast(List[int], sizes)
Path 2: 2 calls (0.15)
100 (1) 50 (1)
list (2)
[100, 1] (1) [30, 10, 30] (1)
1def ratio_resolve(total: int, edges: Sequence[Edge]) -> List[int]:
2 """Divide total space to satisfy size, ratio, and minimum_size, constraints.
3
4 The returned list of integers should add up to total in most cases, unless it is
5 impossible to satisfy all the constraints. For instance, if there are two edges
6 with a minimum size of 20 each and `total` is 30 then the returned list will be
7 greater than total. In practice, this would mean that a Layout object would
8 clip the rows that would overflow the screen height.
9
10 Args:
11 total (int): Total number of characters.
12 edges (List[Edge]): Edges within total space.
13
14 Returns:
15 List[int]: Number of characters for each edge.
16 """
17 # Size of edge or None for yet to be determined
18 sizes = [(edge.size or None) for edge in edges]
19
20 _Fraction = Fraction
21
22 # While any edges haven't been calculated
23 while None in sizes:
24 # Get flexible edges and index to map these back on to sizes list
25 flexible_edges = [
26 (index, edge)
27 for index, (size, edge) in enumerate(zip(sizes, edges))
28 if size is None
29 ]
30 # Remaining space in total
31 remaining = total - sum(size or 0 for size in sizes)
32 if remaining <= 0:
33 # No room for flexible edges
34 return [
35 ((edge.minimum_size or 1) if size is None else size)
36 for size, edge in zip(sizes, edges)
37 ]
38 # Calculate number of characters in a ratio portion
39 portion = _Fraction(
40 remaining, sum((edge.ratio or 1) for _, edge in flexible_edges)
41 )
42
43 # If any edges will be less than their minimum, replace size with the minimum
44 for index, edge in flexible_edges:
45 if portion * edge.ratio <= edge.minimum_size:
46 sizes[index] = edge.minimum_size
47 # New fixed size will invalidate calculations, so we need to repeat the process
48 break
49 else:
50 # Distribute flexible space and compensate for rounding error
51 # Since edge sizes can only be integers we need to add the remainder
52 # to the following line
53 remainder = _Fraction(0)
54 for index, edge in flexible_edges:
55 size, remainder = divmod(portion * edge.ratio + remainder, 1)
56 sizes[index] = size
57 break
58 # Sizes now contains integers only
59 return cast(List[int], sizes)
Path 3: 1 calls (0.08)
100 (1)
[] (1)
[] (1)
1def ratio_resolve(total: int, edges: Sequence[Edge]) -> List[int]:
2 """Divide total space to satisfy size, ratio, and minimum_size, constraints.
3
4 The returned list of integers should add up to total in most cases, unless it is
5 impossible to satisfy all the constraints. For instance, if there are two edges
6 with a minimum size of 20 each and `total` is 30 then the returned list will be
7 greater than total. In practice, this would mean that a Layout object would
8 clip the rows that would overflow the screen height.
9
10 Args:
11 total (int): Total number of characters.
12 edges (List[Edge]): Edges within total space.
13
14 Returns:
15 List[int]: Number of characters for each edge.
16 """
17 # Size of edge or None for yet to be determined
18 sizes = [(edge.size or None) for edge in edges]
19
20 _Fraction = Fraction
21
22 # While any edges haven't been calculated
23 while None in sizes:
24 # Get flexible edges and index to map these back on to sizes list
25 flexible_edges = [
26 (index, edge)
27 for index, (size, edge) in enumerate(zip(sizes, edges))
28 if size is None
29 ]
30 # Remaining space in total
31 remaining = total - sum(size or 0 for size in sizes)
32 if remaining <= 0:
33 # No room for flexible edges
34 return [
35 ((edge.minimum_size or 1) if size is None else size)
36 for size, edge in zip(sizes, edges)
37 ]
38 # Calculate number of characters in a ratio portion
39 portion = _Fraction(
40 remaining, sum((edge.ratio or 1) for _, edge in flexible_edges)
41 )
42
43 # If any edges will be less than their minimum, replace size with the minimum
44 for index, edge in flexible_edges:
45 if portion * edge.ratio <= edge.minimum_size:
46 sizes[index] = edge.minimum_size
47 # New fixed size will invalidate calculations, so we need to repeat the process
48 break
49 else:
50 # Distribute flexible space and compensate for rounding error
51 # Since edge sizes can only be integers we need to add the remainder
52 # to the following line
53 remainder = _Fraction(0)
54 for index, edge in flexible_edges:
55 size, remainder = divmod(portion * edge.ratio + remainder, 1)
56 sizes[index] = size
57 break
58 # Sizes now contains integers only
59 return cast(List[int], sizes)
Path 4: 1 calls (0.08)
100 (1)
list (1)
[40, 35, 25] (1)
1def ratio_resolve(total: int, edges: Sequence[Edge]) -> List[int]:
2 """Divide total space to satisfy size, ratio, and minimum_size, constraints.
3
4 The returned list of integers should add up to total in most cases, unless it is
5 impossible to satisfy all the constraints. For instance, if there are two edges
6 with a minimum size of 20 each and `total` is 30 then the returned list will be
7 greater than total. In practice, this would mean that a Layout object would
8 clip the rows that would overflow the screen height.
9
10 Args:
11 total (int): Total number of characters.
12 edges (List[Edge]): Edges within total space.
13
14 Returns:
15 List[int]: Number of characters for each edge.
16 """
17 # Size of edge or None for yet to be determined
18 sizes = [(edge.size or None) for edge in edges]
19
20 _Fraction = Fraction
21
22 # While any edges haven't been calculated
23 while None in sizes:
24 # Get flexible edges and index to map these back on to sizes list
25 flexible_edges = [
26 (index, edge)
27 for index, (size, edge) in enumerate(zip(sizes, edges))
28 if size is None
29 ]
30 # Remaining space in total
31 remaining = total - sum(size or 0 for size in sizes)
32 if remaining <= 0:
33 # No room for flexible edges
34 return [
35 ((edge.minimum_size or 1) if size is None else size)
36 for size, edge in zip(sizes, edges)
37 ]
38 # Calculate number of characters in a ratio portion
39 portion = _Fraction(
40 remaining, sum((edge.ratio or 1) for _, edge in flexible_edges)
41 )
42
43 # If any edges will be less than their minimum, replace size with the minimum
44 for index, edge in flexible_edges:
45 if portion * edge.ratio <= edge.minimum_size:
46 sizes[index] = edge.minimum_size
47 # New fixed size will invalidate calculations, so we need to repeat the process
48 break
49 else:
50 # Distribute flexible space and compensate for rounding error
51 # Since edge sizes can only be integers we need to add the remainder
52 # to the following line
53 remainder = _Fraction(0)
54 for index, edge in flexible_edges:
55 size, remainder = divmod(portion * edge.ratio + remainder, 1)
56 sizes[index] = size
57 break
58 # Sizes now contains integers only
59 return cast(List[int], sizes)