Giter VIP home page Giter VIP logo

Comments (3)

tmr232 avatar tmr232 commented on July 30, 2024

Honestly, I don't longer remember the reasoning, and will have to go over it.
As for aligning it - I am concerned about backwards compatibility. what do you think?

from sark.

yannayl avatar yannayl commented on July 30, 2024

Is it possible (though it's horrible) to support both simultaneously somehow (and mark one way as deprecated)?
Any other solution would obv. break compatibility. This is life. Libraries evolve.

P.S. as you know, I have my fair share of scripts to update accordingly, I am not taking it lightly.

from sark.

arizvisa avatar arizvisa commented on July 30, 2024

In case you guys want this, I needed something pretty similar at one point and I wrote some pretty hacky-looking code that combines an iterator with a list. It adds a .reset() method which will reset the iterator, but it forwards all list methods to the backend list, and implements all the methods implemented by the real listiterator. This way it should act just like a regular python2 iterator despite it being stored internally as a list.

class listiterator(object):
    def __init__(self, iterator=()):
        self.__state__ = list(iterator)
        self.reset()

    def reset(self):
        self.__sequence__ = iter(self.__state__)
        return self
        
    def next(self):
        return next(self.__sequence__)

    def __getattr__(self, name):
        return getattr(self.__state__, name)

    def __iter__(self):
        return self.__sequence__

    def __length_hint__(self):
        return len(self.__state__)

# sneak out a method constructor
imcons = type(listiterator.__init__)

# call a method from an object using a closure to fetch it
methodcaller = lambda attribute: lambda self, *args, **kwargs: (lambda method=getattr(list, attribute): method(self.__state__, *args, **kwargs))()

# filter out all the descriptors that we need to copy from the `list` type
descriptors = (name for name in dir(list) if name not in listiterator.__dict__ and name not in object.__dict__)
descriptors = ((name, getattr(list, name)) for name in descriptors if name.startswith('__'))
descriptors = (name for name, value in descriptors if callable(value) and getattr(value, '__objclass__', None) == list)

# use methodcaller to construct an unbound method for the method descriptors we snagged 
for name in descriptors:
    setattr(listiterator, name, imcons(methodcaller(name), None, listiterator))

(edited to add comments)

from sark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.