|
16 | 16 |
|
17 | 17 | log = get_logger(__name__)
|
18 | 18 |
|
19 |
| -ZEN_OF_PYTHON = """\ |
| 19 | +ZEN_OF_PYTHON = """ |
20 | 20 | Beautiful is better than ugly.
|
21 | 21 | Explicit is better than implicit.
|
22 | 22 | Simple is better than complex.
|
|
36 | 36 | If the implementation is hard to explain, it's a bad idea.
|
37 | 37 | If the implementation is easy to explain, it may be a good idea.
|
38 | 38 | Namespaces are one honking great idea -- let's do more of those!
|
39 |
| -""" |
| 39 | +""".strip() |
40 | 40 | LEADS_AND_COMMUNITY = (Roles.project_leads, Roles.domain_leads, Roles.partners, Roles.python_community)
|
41 | 41 |
|
42 | 42 |
|
@@ -111,31 +111,54 @@ async def zen(
|
111 | 111 | zen_lines = ZEN_OF_PYTHON.splitlines()
|
112 | 112 |
|
113 | 113 | # Prioritize checking for an index or slice
|
114 |
| - match = re.match(r"(-?\d+)(:(-?\d+)?)?", search_value.split(" ")[0]) |
| 114 | + match = re.match( |
| 115 | + r"(?P<index>-?\d++(?!:))|(?P<start>(?:-\d+)|\d*):(?:(?P<end>(?:-\d+)|\d*)(?::(?P<step>(?:-\d+)|\d*))?)?", |
| 116 | + search_value.split(" ")[0], |
| 117 | + ) |
115 | 118 | if match:
|
116 |
| - upper_bound = len(zen_lines) - 1 |
117 |
| - lower_bound = -1 * len(zen_lines) |
118 |
| - |
119 |
| - start_index = int(match.group(1)) |
120 |
| - |
121 |
| - if not match.group(2): |
122 |
| - if not (lower_bound <= start_index <= upper_bound): |
123 |
| - raise BadArgument(f"Please provide an index between {lower_bound} and {upper_bound}.") |
124 |
| - embed.title += f" (line {start_index % len(zen_lines)}):" |
125 |
| - embed.description = zen_lines[start_index] |
| 119 | + if match.group("index"): |
| 120 | + index = int(match.group("index")) |
| 121 | + if not (-19 <= index <= 18): |
| 122 | + raise BadArgument("Please provide an index between -19 and 18.") |
| 123 | + embed.title += f" (line {index % 19}):" |
| 124 | + embed.description = zen_lines[index] |
126 | 125 | await ctx.send(embed=embed)
|
127 | 126 | return
|
128 | 127 |
|
129 |
| - end_index= int(match.group(3)) if match.group(3) else len(zen_lines) |
| 128 | + start_index = int(match.group("start")) if match.group("start") else None |
| 129 | + end_index = int(match.group("end")) if match.group("end") else None |
| 130 | + step_size = int(match.group("step")) if match.group("step") else 1 |
130 | 131 |
|
131 |
| - if not ((lower_bound <= start_index <= upper_bound) and (lower_bound <= end_index <= len(zen_lines))): |
132 |
| - raise BadArgument(f"Please provide valid indices between {lower_bound} and {upper_bound}.") |
133 |
| - if not (start_index % len(zen_lines) < end_index % (len(zen_lines) + 1)): |
134 |
| - raise BadArgument("The start index for the slice must be smaller than the end index.") |
| 132 | + if step_size == 0: |
| 133 | + raise BadArgument("Step size must not be 0.") |
135 | 134 |
|
136 |
| - embed.title += f" (lines {start_index%len(zen_lines)}-{(end_index-1)%len(zen_lines)}):" |
137 |
| - embed.description = "\n".join(zen_lines[start_index:end_index]) |
138 |
| - await ctx.send(embed=embed) |
| 135 | + lines = zen_lines[start_index:end_index:step_size] |
| 136 | + if not lines: |
| 137 | + raise BadArgument("Slice returned 0 lines.") |
| 138 | + |
| 139 | + if len(lines) == 1: |
| 140 | + embed.title += f" (line {zen_lines.index(lines[0])}):" |
| 141 | + embed.description = lines[0] |
| 142 | + await ctx.send(embed=embed) |
| 143 | + elif lines == zen_lines: |
| 144 | + embed.title += ", by Tim Peters" |
| 145 | + await ctx.send(embed=embed) |
| 146 | + elif len(lines) == 19: |
| 147 | + embed.title += f" (step size {step_size}):" |
| 148 | + embed.description = "\n".join(lines) |
| 149 | + await ctx.send(embed=embed) |
| 150 | + else: |
| 151 | + if step_size != 1: |
| 152 | + step_message = f", step size {step_size}" |
| 153 | + else: |
| 154 | + step_message = "" |
| 155 | + first_position = zen_lines.index(lines[0]) |
| 156 | + second_position = zen_lines.index(lines[-1]) |
| 157 | + if first_position > second_position: |
| 158 | + (first_position, second_position) = (second_position, first_position) |
| 159 | + embed.title += f" (lines {first_position}-{second_position}{step_message}):" |
| 160 | + embed.description = "\n".join(lines) |
| 161 | + await ctx.send(embed=embed) |
139 | 162 | return
|
140 | 163 |
|
141 | 164 | # Try to handle first exact word due difflib.SequenceMatched may use some other similar word instead
|
|
0 commit comments