patternpythonMinor
Escalation approval tool, copy to clipboard via Python
Viewed 0 times
escalationviapythonapprovalclipboardtoolcopy
Problem
I created a simple little script for the people I work with, what it does is copy information to the users clipboard for escalation approvals. It's pretty simple and not very exciting but I want to know if there are ways to do this better:
```
from Tkinter import Tk
import getpass
import os
import time
class ClipBoard(object):
full_string = '' # Full string that will contain the entire escalation approval
def __init__(self, string, addition):
self.string = string
self.addition = addition
def add_addition_to_string(self): # Add the string together
return self.string + self.addition
def copy_string_to_clipboard(self): # Copy to the windows clipboard
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)
def create_full_string(self): # Create the string and return it
self.full_string = self.add_addition_to_string()
return self.full_string
class ConfigureProgram(object):
def __init__(self, directory, escalator):
self.directory = directory
self.escalator = escalator
def check_directory(self): # Check if the directory exists
if os.path.exists(self.directory) is False:
os.makedirs(self.directory) # If it doesn't create it
else:
return True
def check_esc_name(self): # Check if there's already a file made
if self.check_directory() is True:
with open(self.escalator, 'a+') as esc: # If not create it
esc.write(raw_input('Enter what you want appear on escalations(be specific): '))
def get_user(): # Get the username that's running at the moment
return getpass.getuser()
def create_addition_for_string(): # Where's it going to?
return raw_input('Escalate to where? ')
def check_if_run_yet(path_to_file): # Check if you need to configure the program
return os.path.exists(path_to_file)
def read_username(): # Read the escalators
```
from Tkinter import Tk
import getpass
import os
import time
class ClipBoard(object):
full_string = '' # Full string that will contain the entire escalation approval
def __init__(self, string, addition):
self.string = string
self.addition = addition
def add_addition_to_string(self): # Add the string together
return self.string + self.addition
def copy_string_to_clipboard(self): # Copy to the windows clipboard
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)
def create_full_string(self): # Create the string and return it
self.full_string = self.add_addition_to_string()
return self.full_string
class ConfigureProgram(object):
def __init__(self, directory, escalator):
self.directory = directory
self.escalator = escalator
def check_directory(self): # Check if the directory exists
if os.path.exists(self.directory) is False:
os.makedirs(self.directory) # If it doesn't create it
else:
return True
def check_esc_name(self): # Check if there's already a file made
if self.check_directory() is True:
with open(self.escalator, 'a+') as esc: # If not create it
esc.write(raw_input('Enter what you want appear on escalations(be specific): '))
def get_user(): # Get the username that's running at the moment
return getpass.getuser()
def create_addition_for_string(): # Where's it going to?
return raw_input('Escalate to where? ')
def check_if_run_yet(path_to_file): # Check if you need to configure the program
return os.path.exists(path_to_file)
def read_username(): # Read the escalators
Solution
The main part of your program is only
the rest being used only as a formatting tool. You should thus focus into making such formatting as clear as possible.
But first of, you may want to read about docstrings as your comment should be one:
So now, onto cleaning a bit everything around this function.
First off, you don't need a class with 3 other functions just to concatenate two strings. Based on your
where
The last thing to do is to simplify a bit the reading of the user config file. It's a bit of a shame to be required to run the program twice in order to create a file if it doesn't exist. Instead, you could
But this function assumes we are in the right directory, which may not exist yet. Let's tackle that as well:
as you can see, I used
Also, if I recall correctly,
Putting it all together, you can end up with:
def copy_string_to_clipboard(self): # Copy to the windows clipboard
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)the rest being used only as a formatting tool. You should thus focus into making such formatting as clear as possible.
But first of, you may want to read about docstrings as your comment should be one:
def copy_string_to_clipboard(self):
"""Copy to the windows clipboard"""
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)So now, onto cleaning a bit everything around this function.
First off, you don't need a class with 3 other functions just to concatenate two strings. Based on your
opening variable, what you need to do is simply:user = read_username()
assignation = raw_input('Escalate to where? ')
copy_to_clipboard(
'Ticket has been reviewed and approved by {}. '
'Ticket assigned to {}.'.format(user, assignation))where
copy_to_clipboard has been slightly modified to accept the whole string as argument:def copy_to_clipboard(text):
"""Copy the content of a string to the windows clipboard"""
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(text)The last thing to do is to simplify a bit the reading of the user config file. It's a bit of a shame to be required to run the program twice in order to create a file if it doesn't exist. Instead, you could
try to read the file and, if it fails, ask the user for its content:def read_username(filename='esc_name.txt'):
"""Get the user's name from config file or command line"""
try:
with open(filename) as f:
return f.readline()
except OSError:
pass
# If we are here, we couldn't read the configuration
# file so ask the user an try to save its choice
username = raw_input('Enter what you want appear on escalations (be specific): ')
try:
with open(filename, 'w') as f:
f.write(username)
except OSError:
pass
return usernameBut this function assumes we are in the right directory, which may not exist yet. Let's tackle that as well:
from getpass import getuser
def setup(program_dir='esc'):
"""Create our own directory structure and move to the right place"""
appdata = r'C:\Users\{}\AppData\Roaming'.format(getuser())
os.chdir(appdata)
if not os.path.exists(program_dir):
os.mkdir(program_dir)
os.chdir(program_dir)as you can see, I used
getuser from getpass directly without having to create a wrapper that just remove 1 level of nesting.Also, if I recall correctly,
os.environ['APPDATA'] should contain the value we are building into appdata. So you could simplify it to:def setup(program_dir='esc'):
"""Create our own directory structure and move to the right place"""
os.chdir(os.environ['APPDATA'])
if not os.path.exists(program_dir):
os.mkdir(program_dir)
os.chdir(program_dir)Putting it all together, you can end up with:
import os
from Tkinter import Tk
def copy_to_clipboard(text):
"""Copy the content of a string to the windows clipboard"""
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(text)
def setup(program_dir='esc'):
"""Create our own directory structure and move to the right place"""
os.chdir(os.environ['APPDATA'])
if not os.path.exists(program_dir):
os.mkdir(program_dir)
os.chdir(program_dir)
def read_username(filename='esc_name.txt'):
"""Get the user's name from config file or command line"""
try:
with open(filename) as f:
return f.readline()
except OSError:
pass
# If we are here, we couldn't read the configuration
# file so ask the user an try to save its choice
username = raw_input('Enter what you want appear on escalations (be specific): ')
try:
with open(filename, 'w') as f:
f.write(username)
except OSError:
pass
return username
if __name__ == '__main__':
setup()
user = read_username()
assignation = raw_input('Escalate to where? ')
copy_to_clipboard(
'Ticket has been reviewed and approved by {}. '
'Ticket assigned to {}.'.format(user, assignation))
print 'Press CTRL-V to paste the approval.'Code Snippets
def copy_string_to_clipboard(self): # Copy to the windows clipboard
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)def copy_string_to_clipboard(self):
"""Copy to the windows clipboard"""
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(self.full_string)user = read_username()
assignation = raw_input('Escalate to where? ')
copy_to_clipboard(
'Ticket has been reviewed and approved by {}. '
'Ticket assigned to {}.'.format(user, assignation))def copy_to_clipboard(text):
"""Copy the content of a string to the windows clipboard"""
cb = Tk()
cb.withdraw()
cb.clipboard_clear()
cb.clipboard_append(text)def read_username(filename='esc_name.txt'):
"""Get the user's name from config file or command line"""
try:
with open(filename) as f:
return f.readline()
except OSError:
pass
# If we are here, we couldn't read the configuration
# file so ask the user an try to save its choice
username = raw_input('Enter what you want appear on escalations (be specific): ')
try:
with open(filename, 'w') as f:
f.write(username)
except OSError:
pass
return usernameContext
StackExchange Code Review Q#140322, answer score: 4
Revisions (0)
No revisions yet.