-
-
Notifications
You must be signed in to change notification settings - Fork 15
Adding random_permutation function to math.py #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hi, @mohitbhasin ! Thank you for your interest in working on this. For this implementation, I'd rather not call The signature of the def shuffle(self, objects: Sequence[Any]) -> Sequence[Any]: And I just noticed that Like This method should work for generators, lists, and numpy arrays, returning the same data type except when the user passes a generator. Finally, note the docstring. That docstring is a key that links to a reference file. Here is the file for You'll need to create a file for that also, in the same format, with example code. |
Got it. I'll work on it and let you know if I have any questions. |
Thank you! |
Also, let's call it |
|
What are your thoughts on this:
|
I have a question here... Sequence allows for immutable sequences, but you mentioned "in place" shuffling... So I went to have a look at the standard library's And nothing at the official Python type shed either. |
Right. I think I've changed my mind here. None of the other methods in math.py change user inputs in-place, and in general, I don't like it when there inconsistencies or confusion about if something does or does not change something in place. This is one of the things I don't like about Processing's PVector class. How about naming this |
@mohitbhasin , we've talked it over, both here and on Mastodon, and we'd changed our mind about two things:
|
How about using Numpy's types? import numpy as np
from numpy.typing import ArrayLike, NDArray
from collections.abc import Generator
def random_permutation(seq: ArrayLike | Generator) -> NDArray:
if isinstance(seq, Generator): seq = tuple(seq)
return np.random.permutation(seq) |
def random_permutation(self, seq: ArrayLike) -> NDArray:
if isinstance(seq, Generator): seq = tuple(seq)
return np.random.permutation(seq) |
Signature type hints are for IDE linters, not for the execution itself. |
Got it. Thanks. |
@GoToLoop Is there a reason for not using np.random.default_rng() which is used across Math.py? |
For the input param, you can use If the input type is a list, the output type should also be a list, not a numpy array. You can follow what |
You can use |
That was my 1st try! However, method permutation() complained about that parameter type inside VSCode. 😞 |
@hx2A Thanks for the feedback. Does this look good? def random_permutation(self, seq: Sequence[Any]) -> Sequence[Any]:
"""$class_Sketch_random_permutation"""
if isinstance(seq, types.GeneratorType):
seq = list(seq)
indices = self._rng.permutation(range(len(seq)))
if not isinstance(seq, list):
try:
return seq[indices]
except:
pass
return [seq[idx] for idx in indices] Do you think |
…string for the same. random_permutation() utilizes permutation() of numpy and return a sequence.
@GoToLoop , Really? Interesting. Why would it see a conflict between the @mohitbhasin , I think that code looks good to me. Did you test it with a list, numpy array, and generator to make sure they all work? The next step is to write that documentation file.
I'm not sure what you are saying. What do you mean? |
Maybe b/c updating after a long time, VSCodium was glitching somehow. import numpy as np
from numpy.typing import NDArray
from collections.abc import Generator, Sequence
def random_permutation(seq: Sequence | Generator) -> NDArray:
if isinstance(seq, Generator): seq = tuple(seq)
return np.random.permutation(seq) Just for reference, this is permutation()'s signature according to addon "BasedPyright": |
Yes, I tested list, empty list, generator, tuple, numpy array. All are working as expected.
I have pushed the code and documentation in the pull request, please review.
I was not sure for which data type |
subcategory = random | ||
|
||
@@ signatures | ||
random_permutation(seq: Sequence[Any] -> Sequence[Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file looks good. Only thing is this signature is missing the final )
and :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out. I added the missing )
. I checked other docstrings files, none of them ends with :
, so I have not added it.
However, If it should be there, I can add it. Just let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hx2A What do you think about the above change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was good, thank you! I just merged the PR.
You were correct about the :
, thank you for paying attention to details.
Almost there, @mohitbhasin ! Can you also add a line to this csv file: https://github.com/py5coding/py5generator/blob/main/py5_resources/data/sketch.csv It will be just like That's a data file that py5generator uses to analyze the code. |
I have added the entry in sketch.csv for random_permutation() |
minor adjustments
No description provided.