Skip to content

Instantly share code, notes, and snippets.

View wood-push-melon's full-sized avatar
Working from home

Shu wood-push-melon

Working from home
  • Canonical
  • Canada
View GitHub Profile
@wood-push-melon
wood-push-melon / with_statement.py
Created December 11, 2019 15:07
Object that Supports with Statement
class Connection:
def __init__(self):
...
def __enter__(self):
...
def __exit__(self, type, value, traceback):
...
@wood-push-melon
wood-push-melon / kwargs_only_func.py
Created December 11, 2019 15:03
Functions with only Keyword Arguments
def foo(*, a, b):
pass
foo(a="a", b="b")
@wood-push-melon
wood-push-melon / slice_iterator.py
Created December 11, 2019 14:50
Taking Slice of an Iterator
import itertools
# NOTE: this will consume all items up until the start of slice
# and also all the items in islice object
s = itertools.islice(range(50), 10, 20)
for val in s:
...
@wood-push-melon
wood-push-melon / sanitize_input.py
Last active December 11, 2019 14:45
Sanitizing String Input
user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {
ord('\n') : ' ',
ord('\t') : ' ',
ord('\r') : None
}
user_input.translate(character_map)