Skip to content

Instantly share code, notes, and snippets.

@rrajasek95
Last active August 22, 2018 07:33
Show Gist options
  • Save rrajasek95/3c43fafdd85d02e55dc2d5fad8798ac2 to your computer and use it in GitHub Desktop.
Save rrajasek95/3c43fafdd85d02e55dc2d5fad8798ac2 to your computer and use it in GitHub Desktop.
Python Snippets
"""
A set of basic util functions.
They're pretty simple recipes which can be stored as snippets.
Available at:
https://gist.github.com/rrajasek95/3c43fafdd85d02e55dc2d5fad8798ac2
"""
from itertools import repeat
import sys
import os
def prefix(prefix_str, iterable):
"""Apply a string prefix to the iterable."""
return map(lambda p, x: p + str(x), repeat(prefix_str), iterable)
def error(*args, **kwargs):
"""Print to STDERR."""
print(*args, file=sys.stderr, **kwargs)
def safemkdir(directory):
"""Create a directory if one does not exist already."""
if not os.path.exists(directory):
os.makedirs(directory)
def write_contents_to_path(data, path):
"""Write data to a file in the path."""
with open(path, 'wb') as file:
file.write(data)
def yes_no_prompt(text):
"""Create a yes no prompt which takes yes by default."""
yes_values = ["Y", "y", ""]
input_text = input(text + " [Y/n]")
return input_text in yes_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment