-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
12 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from collections import deque as _deque | ||
from collections import Iterable | ||
from itertools import islice, zip_longest as izip | ||
from collections.abc import Iterable | ||
from itertools import zip_longest as izip | ||
|
||
|
||
def as_list(val, wrap=True): | ||
#strings are iterables all the way down, so an exception needs to be made | ||
# strings are iterables all the way down, so an exception needs to be made | ||
# else we get infinite recursion, which is bad | ||
# this only took me 2 hours to debug, new record! | ||
if not isinstance(val, Iterable) or isinstance(val, str): | ||
return [val] if wrap else val | ||
else: | ||
return [as_list(x, wrap=False) for x in val] | ||
|
||
|
||
|
||
class deque(_deque): | ||
def copy(self): | ||
if hasattr(_deque, 'copy'): | ||
if hasattr(_deque, "copy"): | ||
return _deque.copy(self) | ||
else: | ||
return deque(x for x in self) | ||
|
||
def __getitem__(self, key): | ||
if isinstance(key, slice): | ||
return [x for x in self][key] | ||
else: | ||
return _deque.__getitem__(self, key) | ||
|
||
def reversed(self): | ||
tmp = self.copy() | ||
tmp.reverse() | ||
return tmp | ||
|
||
|
||
def zip_longest(*iterables): | ||
for vals in izip(*iterables): | ||
yield filter(lambda x:x is not None, vals) | ||
yield filter(lambda x: x is not None, vals) |