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
try: | |
import msvcrt | |
def input_char(): # Windows | |
"""Read a single character from user input""" | |
return msvcrt.getwch() | |
except ImportError: | |
import sys, tty, termios | |
def input_char(): | |
"""Read a single character from user input""" | |
fd = sys.stdin.fileno() |
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
ExceptionMatch = typing.Union[ | |
typing.Type[Exception], typing.Tuple[typing.Type[Exception], ...] | |
] | |
def raises( | |
expected_exception: ExceptionMatch, | |
function: typing.Callable, | |
*args, | |
**kwargs, | |
) -> bool: |
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
import typing | |
def griddler_line_hint(line: typing.Sequence[int]) -> typing.Iterator[int]: | |
""" | |
Creates the hint for griddlers for the given line | |
>>> list(griddler_line_hint([1, 1, 1])) | |
[3] | |
>>> list(griddler_line_hint([1, 1, 0, 1, 1])) | |
[2, 2] |
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
from typing import TypeVar, Sequence, Optional, Callable | |
T = TypeVar("T") | |
def find_in_sequence( | |
sequence: Sequence[T], predicate: Callable[[T], bool] | |
) -> Optional[T]: | |
"""Returns the element that satisfies the predicate, if exists""" | |
return next((element for element in sequence if predicate(element)), None) |
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
import os | |
from contextlib import contextmanager | |
from typing import Union, ContextManager | |
PathType = Union[os.PathLike, str] | |
@contextmanager | |
def cd(path: PathType) -> ContextManager[None]: | |
old_path = Path.cwd() |
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
from typing import TypeVar, Hashable, Callable, Set, Iterable, Iterator | |
T = TypeVar("T") | |
K = TypeVar("K", bound=Hashable) | |
def keyed_filter(iterable: Iterable[T], key: Callable[[T], K]) -> Iterator[T]: | |
"""Yields the iterable without key duplicates. | |
>>> list(keyed_filter(["Alice","Adam","Bob","Ben"],key=lambda x:x[0]) ) |
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
from typing import Iterator, TypeVar, Generator | |
T = TypeVar('T') | |
def try_yield(generator: Iterator[T])->Generator[T, None, None]: | |
"""Returns an generator of all the items in `generator` that didn't throw""" | |
while True: | |
try: | |
yield next(generator) | |
except StopIteration: |
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
import os | |
from contextlib import contextmanager | |
@contextmanager | |
def cd(path: os.PathLike): | |
"""Context manager that sets the cwd to be `path`""" | |
old_path = os.getcwd() | |
os.chdir(path) | |
yield |
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
"""Generic and pythonic implementation of the Depth First Search traversal algorithm""" | |
from typing import Callable, List, Iterable,TypeVar,Hashable, MutableSequence | |
from pathlib import Path | |
from collections import deque | |
T = TypeVar('T', bound=Hashable) | |
S = TypeVar('S') | |
def iter_top(stack: MutableSequence[S]) -> Iterator[S]: | |
"""Iterate a stack as long as it is full, yielding the top every time""" |
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
class JsonWebView(private val webView: WebView) { | |
private val BASEURL: String = BackendRemote.SERVER_ADDRESS | |
val TAG = JsonWebView::class.java.name | |
@SuppressLint("SetJavaScriptEnabled") | |
fun loadURL(url:URL,cb: (JSONObject) -> Unit) { | |
webView.settings.apply { | |
javaScriptEnabled = true | |
domStorageEnabled = true | |
webView.loadUrl(url.toString()) |
NewerOlder