Skip to content

Instantly share code, notes, and snippets.

View Tadaboody's full-sized avatar
🐣
Breaking open

Tomer Tadaboody

🐣
Breaking open
View GitHub Profile
@Tadaboody
Tadaboody / ignore_all.sh
Created September 5, 2018 11:20
GitIgnore all dirty files
git status --porcelain|awk '{print $2}' >>.gitignore
@Tadaboody
Tadaboody / git_feature_branch.sh
Last active October 3, 2018 10:33
Creates a new branch from the copied issue name, replacing whitespace with underscores
git_feature_branch() {
# Adds a new git branch named [name_with_underscores_$1]. name is either in clipboard or given as a second argument
(( [ "$2" ] && echo $2 ) || clippaste )|python3 -c "print('_'.join(input().split()) + '_$1')"|xargs git checkout -b
}
@Tadaboody
Tadaboody / git_ignored.sh
Created October 4, 2018 14:49
List all top files ignored by git in the current repo
git status --ignored --porcelain=v2|grep '!'|awk '{print $2}'
@Tadaboody
Tadaboody / JsonWebview.kt
Created October 9, 2018 11:09
Wrapper for a webview that handles JSONs, invoking a callback when encountering them
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())
@Tadaboody
Tadaboody / dfs.py
Last active January 9, 2019 14:29
Generic and pythonic implementation of the Depth First Search traversal algorithm
"""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"""
@Tadaboody
Tadaboody / cd.py
Created October 29, 2018 12:59
Context manager to cd into a dir for a while
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
@Tadaboody
Tadaboody / try_yield.py
Last active November 13, 2018 16:29
"""Returns an generator of all the items in `generator` that didn't throw"""
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:
@Tadaboody
Tadaboody / keyed_filter.py
Created January 17, 2019 14:47
Yields the iterable without key duplicates.
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]) )
@Tadaboody
Tadaboody / cd.py
Last active March 14, 2019 10:29
Cd as a context manager
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()
@Tadaboody
Tadaboody / find_in_sequence.py
Created April 21, 2019 11:01
Returns the element that satisfies the predicate, if exists
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)