Skip to content

Zen slicing#3297

Merged
ChrisLovering merged 8 commits intopython-discord:mainfrom
LeandroVandari:zen_slicing
Mar 29, 2025
Merged

Zen slicing#3297
ChrisLovering merged 8 commits intopython-discord:mainfrom
LeandroVandari:zen_slicing

Conversation

@LeandroVandari
Copy link
Copy Markdown
Contributor

Implemented the feature in issue #3289.

Now, the !zen command accepts arguments in the form start:stop, and will return the corresponding zen lines.
Additionally, since the indices can now no longer simply be passed as integers, the zen_rule_index argument to the zen command was removed, and now indices are parsed from a regex expression.

Copy link
Copy Markdown
Member

@vivekashok1221 vivekashok1221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening this PR 🐍

Comment thread bot/exts/utils/utils.py Outdated
@LeandroVandari
Copy link
Copy Markdown
Contributor Author

LeandroVandari commented Mar 18, 2025

This latest commit fixes the problems you mentioned, but i noticed it's impossible to include the last line when slicing (i.e. !zen 1:18 and !zen 1:-1 will only include up to the semi-last line, and !zen 1:19 will error out). I was considering some options to fix that, and I wanted to get your opinions:

  1. Make slicing end inclusive -- i don't like this because it feels unpythonic and just incoherent with the language
  2. Allowing for end_index == len(zen_lines) -- again, weird and unpythonic, but less bad than the previous option.
  3. Add the possibility for an open range (i.e. 1:) which will include up to the last line. -- this feels like the best option to me.

Additionally, I was wondering: Do we ever want to support steps, now that we're going the slice way? That feature would probably never be used, but it's just consistent with slicing I feel like.

@LeandroVandari
Copy link
Copy Markdown
Contributor Author

LeandroVandari commented Mar 18, 2025

Maybe it's possible to write a test for this? It seems like a mostly-pure function

I'm not really used to writing tests, especially for discord bots, but I could take a look at the code base and draft a PR for it later on (should I open an issue about it?)

@vivekashok1221
Copy link
Copy Markdown
Member

vivekashok1221 commented Mar 18, 2025

  1. Allowing for end_index == len(zen_lines) -- again, weird and unpythonic, but less bad than the previous option.
  2. Add the possibility for an open range (i.e. 1:) which will include up to the last line. -- this feels like the best option to me.

I like option 2 since it's consistent with how slicing in Python works. If we deviate from it, people might think it's a bug.
Maybe we can do option 2 AND option 3 (your call).

re: writing tests

draft a PR for it later on (should I open an issue about it?)

Sure (you don't need to open an issue IMO but you can if you want to)
Writing tests is optional btw, but highly appreciated.

…_index % zen_lines would be 0, even though it's the last line
@LeandroVandari
Copy link
Copy Markdown
Contributor Author

i implemented both 2 and 3, and from my quick tests all seems functional now. I'll take a look into the repo tests to see how I'd implement that

Copy link
Copy Markdown
Member

@vivekashok1221 vivekashok1221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed one more thing while testing. Everything else looks good to me.

Comment thread bot/exts/utils/utils.py Outdated
Co-authored-by: Vivek Ashokkumar <vivekashok1221@gmail.com>
Copy link
Copy Markdown
Member

@vivekashok1221 vivekashok1221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@LeandroVandari
Copy link
Copy Markdown
Contributor Author

I drafted some tests for the zen command. I tried to keep a balance between hard-coding / developing answers myself. I don't think it's ready yet, but I'd love some opinions on it :)

Copy link
Copy Markdown
Member

@vivekashok1221 vivekashok1221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ChrisLovering ChrisLovering enabled auto-merge March 29, 2025 20:13
@ChrisLovering ChrisLovering merged commit 6c90ab7 into python-discord:main Mar 29, 2025
4 checks passed
@wookie184 wookie184 linked an issue Apr 8, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

!zen command does not allow slicing.

4 participants