Last active
August 22, 2018 07:33
-
-
Save rrajasek95/3c43fafdd85d02e55dc2d5fad8798ac2 to your computer and use it in GitHub Desktop.
Python Snippets
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
""" | |
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