Wanted: Criticism of code for a Python module, plus a Mac tester
Ian Kelly
ian.g.kelly at gmail.com
Thu Feb 16 00:03:44 EST 2012
On Wed, Feb 15, 2012 at 6:11 PM, HoneyMonster <someone at someplace.invalid> wrote:
> As to your first suggestion though, I am having some difficulty. Note
> that the vulnerability rotates; i.e. CONDITIONS[4] is not the same as
> CONDITIONS[0].
> Is there a better way of doing it than a simple list.append()?
Ah, it's more complicated than I realized. I believe this generates
the correct result, although adding None to the dealers list is
somewhat unsatisfactory:
DEALERS = ["Dealer North", "Dealer East", "Dealer South", "Dealer West", None]
VULNERABILITIES = [
"Neither vulnerable", "North-South vulnerable",
"East-West vulnerable", "Both vulnerable",
]
CONDITIONS = [(d, v) for (d, v) in zip(DEALERS * 4, VULNERABILITIES * 5) if d]
I might be tempted to write a custom iterator for it, something like this:
def rotating(iterable):
cache = collections.deque()
for value in iterable:
yield value
cache.append(value)
while True:
cache.rotate(-1)
for value in cache:
yield value
# Using the original 4-length DEALERS list
CONDITIONS = list(itertools.islice(itertools.izip(itertools.cycle(DEALERS),
rotating(VULNERABILITIES)), 16))
But I don't know that it's really worth the added complexity.
Cheers,
Ian
More information about the Python-list
mailing list