Last active
September 23, 2019 03:40
-
-
Save ncooke3/8edb7bdb7d9867b6f6ad20c212f4a6ef to your computer and use it in GitHub Desktop.
Simple python script that formats "Hello World" to "hello_world" and copies formatted string to clipboard!
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
# Hey! This is a script that formats a string to the lowercased, underscore-joined strings frequently used in python. | |
# On mac, copy this file into your /usr/local/bin directory! | |
# You can then run it anywhere in terminal with: | |
# >>> python pythonalize.py "The string you want formatted!" | |
# The string you pass in will be automatically copied to your Mac's clipboard! | |
import subprocess | |
import sys | |
def convert_to_file_name_syntax(name): | |
words = name.split(" ") | |
for index, word in enumerate(words): | |
words[index] = word.lower() | |
return "_".join(words) | |
def write_to_clipboard(output): | |
process = subprocess.Popen( | |
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) | |
process.communicate(output.encode('utf-8')) | |
formatted_file_name = convert_to_file_name_syntax(sys.argv[1]) | |
write_to_clipboard(formatted_file_name) | |
print(formatted_file_name + " copied to clipboard!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment