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
Idea: Use case for a VersionIterator? #223
Comments
I kinda like the general idea and had also thought of something similar. Two comments for the specific API:
Based on these I came up with a simple coroutine approach, where you can def iter_versions(version, part, prerelease_token="rc"):
current_part = part
while True:
version = next_version(version, current_part, prerelease_token)
current_part = (yield version) or part Usage: import itertools as it
def test_iter_versions():
iv = iter_versions("1.2.3-rc.3", "prerelease")
assert list(it.islice(iv, 3)) == ["1.2.3-rc.4", "1.2.3-rc.5", "1.2.3-rc.6"]
assert next(iv) == "1.2.3-rc.7"
assert next(iv) == "1.2.3-rc.8"
assert iv.send("patch") == "1.2.3"
assert next(iv) == "1.2.4-rc.1"
assert iv.send("minor") == "1.3.0"
assert next(iv) == "1.3.1-rc.1"
assert iv.send("major") == "2.0.0"
assert next(iv) == "2.0.1-rc.1" |
Haha, cool! 😎 👍
Yes, I thought about that too. But if we create an infinite iterator, we always have to use
Ahh, I see. Good idea! I like your approach, it's much better. |
An alternative api is to accept a (finite or infinite) iterable of parts: def iter_versions(version, parts, prerelease_token="rc"):
for part in parts:
version = next_version(version, part, prerelease_token)
yield version
def test_iter_versions():
parts = it.chain(
it.repeat("prerelease", 5),
["patch", "prerelease", "minor", "prerelease", "major", "prerelease"],
)
iv = iter_versions("1.2.3-rc.3", parts)
assert list(it.islice(iv, 3)) == ["1.2.3-rc.4", "1.2.3-rc.5", "1.2.3-rc.6"]
assert next(iv) == "1.2.3-rc.7"
assert list(iv) == [
"1.2.3-rc.8",
"1.2.3",
"1.2.4-rc.1",
"1.3.0",
"1.3.1-rc.1",
"2.0.0",
"2.0.1-rc.1",
] |
I like this idea. class VersionInfo:
# ...
def iter_versions(self, parts, prerelease_token="rc"):
current_version = self
for part in parts:
current_version = current_version.next_version(part, prerelease_token)
yield current_version |
Hmn, I'm wondering about the |
Not if we want to support arbitrary iterables without expanding them into tuples first. |
@gsakkis If you don't mind, I would suggest to add this to the semver3 release (as this is something new and I would like to get rid of semver2 as soon as possible 😉 ). |
@tomschr Sure no problem |
Situation
For example, if we want to start from a certain version and get the next 3 versions we could do that:
This would work, but maybe we could use the iterator protocol for that.
Proposed Solution
The solution is very rough, but that's on purpose. It's a minimal example to demonstrate the idea and not get distracted by other methods. So I know, this would need fine-tuning (add properties, protect from invalid datatypes etc.).
An implementation could look like this:
Questions
__init__
?Actually, I'm not sure myself if this is something good or completely unnecessary. I just had this idea when working on issue #222. 😉 Also I thought, it would be helpful to document it, regardless if this will be accepted or not.
@gsakkis, @scls19fr
What do you think? I would like to hear your opinion. 😉
The text was updated successfully, but these errors were encountered: