Created
June 21, 2018 16:34
-
-
Save nickymarino/ee5c8666b1d557d7e0c99bbd211a40d4 to your computer and use it in GitHub Desktop.
Python helper functions that I don't want to rewrite
This file contains hidden or 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
def replace_str_index(text, index=0, replacement=''): | |
'''Replaces an index in a string with a replacement char/string''' | |
return '{}{}{}'.format(text[:index], replacement, text[index+1:]) | |
def get_substrings(string): | |
'''Returns a list of all substrings in a string''' | |
length = len(string) | |
return [string[i:j+1] for i in range(length) for j in range(i, length)] | |
def copy_to_clipboard(string): | |
'''Copies the string to the clipboard''' | |
import sys | |
import subprocess | |
commands = {'win32': 'clip', | |
'cygwin': 'clip', | |
'darwin': 'pbcopy', | |
} | |
currentPlatform = sys.platform | |
if currentPlatform not in commands: | |
raise KeyError('Platform not supported') | |
copyCommand = commands[currentPlatform] | |
subprocess.Popen([copyCommand], stdin=subprocess.PIPE, encoding='utf8').communicate(string) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment