A live-action movie version of "All You Need Is Kill" has been released released throughout Japan on July 4, 2014 .co.jp/edgeoftomorrow/) To commemorate that, I wrote the ʻAllYouNeedIsKill` class in Python.
aynik.py
#!/usr/bin/env python3
# vim:fileencoding=utf-8
# Copyright (c) 2014 Masami HIRATA <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
__all__ = ['AllYouNeedIsKill']
class AllYouNeedIsKill:
def __init__(self, iterable):
self._iter_source = iter(iterable)
self._buffer = []
self._iter_buffer = None
def __iter__(self):
return self
def __next__(self):
if self._iter_buffer is not None:
try:
return next(self._iter_buffer)
except StopIteration:
self._iter_buffer = None
value = next(self._iter_source)
self._buffer.append(value)
return value
def rewind(self):
if self._buffer:
self._iter_buffer = iter(self._buffer)
else:
self._iter_buffer = None
def reset(self):
if self._iter_buffer is not None:
self._buffer = list(self._iter_buffer)
self.rewind()
else:
self._buffer = []
ʻThe instance of the AllYouNeedIsKillclass behaves as an iterator with therewind ()method for rewinding and thereset ()` method for starting from the state at the time of execution added to the iterator of the argument object. ..
The sample is below.
>>> from itertools import count # count()Is an iterator that just keeps counting up
>>> from aynik import AllYouNeedIsKill
>>> counter = AllYouNeedIsKill(count(1))
>>> next(counter)
1
>>> next(counter)
2
>>> counter.rewind() #Perform rewind
>>> next(counter) #Rewound so it becomes 1
1
>>> next(counter)
2
>>> counter.reset() #Starting from the current state
>>> next(counter)
3
>>> counter.rewind() #Perform rewind
>>> next(counter) #The return value will be 3 instead of 1
3
>>>
The ʻAllYouNeedIsKill class allows me to handle infinite iterators as well, but it's implemented in a class rather than a function, and it's typed like some tools in the standard library ʻitertools. Please note that the performance is poor due to not deploying iterators to tuples and the code is not Python-like.
ʻAynik.py` will be released under a two-clause BSD license, so feel free to use it.
Recommended Posts