Skip to content

Idea: Use case for a VersionIterator? #223

@tomschr

Description

@tomschr

Situation

For example, if we want to start from a certain version and get the next 3 versions we could do that:

v = VersionInfo.parse("1.2.3-rc.3")
for _ in range(3):
   print(v.next_version(part="prerelease"))

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.).

class VersionInfoIterator:
    """Iterator over VersionInfo objects"""
    def __init__(self, version, part, steps=10):
        self.version = version
        self.part = part
        self._steps = steps

    def __iter__(self):
        return self

    def __repr__(self):
        cls = type(self)
        return "{}(version={!r}, part={!r}, steps={})".format(cls.__name__,
                self.version, self.part, self._steps)

    def __next__(self):
        if not self.steps:
            raise StopIteration
        self._steps -= 1
        self.version = self.version.next_version(self.part)
        return self.version

An implementation could look like this:

from semver import VersionInfo, VersionInfoIterator
>>> v = VersionInfo.parse("1.2.3-rc.3")
>>> vit = VersionInfoIterator(v, "prerelease", steps=3)
>>> next(vit)                                               
VersionInfo(major=1, minor=2, patch=3, prerelease='rc.4', build=None)
>>> next(vit)                                                                                                                              
VersionInfo(major=1, minor=2, patch=3, prerelease='rc.5', build=None)
>>> vit.part = "minor"
>>> next(vit)    
VersionInfo(major=1, minor=3, patch=0, prerelease=None, build=None)
>>> next(vit)
Traceback (most recent call last)
...
StopIteration

# or

>>> vit = VersionInfoIterator(v, "prerelease", steps=3)
>>> list(vit)
[VersionInfo(major=1, minor=2, patch=3, prerelease='rc.4', build=None),
 VersionInfo(major=1, minor=2, patch=3, prerelease='rc.5', build=None),
 VersionInfo(major=1, minor=2, patch=3, prerelease='rc.6', build=None)]

Questions

  • Would such an iterator be useful?
  • Is it necessary or is first example above enough when iterating?
  • If you think it could be useful, would you change something in __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. 😉

Metadata

Metadata

Assignees

Labels

DesignIdeas, suggestions, musings about design questionsDocDocumentation related issueEnhancementNot a bug, but increases or improves in value, quality, desirability, or attractivenessQuestionUnclear or open issue subject for debateRelease_3.x.yOnly for the major release 3

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy