patternpythonMinor
Python Stock Checker
Viewed 0 times
stockcheckerpython
Problem
```
# Prints Updates of whats currently happening
print('Importing Libraries')
# imports stuff
from tkinter import *
import sys, string, os, subprocess, openpyxl, time
from selenium import webdriver
from splinter import Browser
from twilio.rest import TwilioRestClient
import tkMessageBox
# Prints Updates of what's happening
print("Finished Importing")
# Prints Updates of what's happening
print("Pulling Settings")
# Opens settings excel document
settingsWorkbook = openpyxl.load_workbook('settings.xlsx')
# Gets the settings sheet
settingsSheet = settingsWorkbook.get_sheet_by_name('Sheet1')
# Finds the value for oder codes workbook name
ordersWorkbookName = settingsSheet['B6'].value
# If it is empty;
if str(ordersWorkbookName)=='None':
# Sets to default
ordersWorkbookName = sheet['C6'].value
# Gets the value of order codes sheet name
ordersSheetName = settingsSheet['B7'].value
# If its empty;
if str(ordersSheetName) == 'None':
# Sets to default
ordersSheetName = settingsSheet['C7'].value
# Gets folder directory
folderDirectory = settingsSheet['B2'].value
# If its empty;
if str(folderDirectory) == 'None':
# Prionts error message
print('Please Finish Settings by filling in: Folder Directory')
sys.exit()
chromeDriverDirectory = settingsSheet['B3'].value
if str(chromeDriverDirectory) == 'None':
print('Please Finish Settings by Filling in: Chrome Driver directory')
sys.exit()
ordersDirectory = settingsSheet['B5'].value
if str(ordersDirectory) == 'None':
print('Please Finish Settings by Filling in: Excel Orders File Directory')
sys.exit()
saveValue = settingsSheet['B8'].value
if str(saveValue) == 'None':
saveValue = settingsSheet['C8'].value
print('Using Default Save Value')
settingsDirectory = settingsSheet['B4'].value
if str(settingsDirectory) == 'None':
print('Please Finish Settings by Filling in: Excel Settings File Directory')
sys.exit
# Prints Updates of what's happening
print('Finished Pulling Settings')
#
# Prints Updates of whats currently happening
print('Importing Libraries')
# imports stuff
from tkinter import *
import sys, string, os, subprocess, openpyxl, time
from selenium import webdriver
from splinter import Browser
from twilio.rest import TwilioRestClient
import tkMessageBox
# Prints Updates of what's happening
print("Finished Importing")
# Prints Updates of what's happening
print("Pulling Settings")
# Opens settings excel document
settingsWorkbook = openpyxl.load_workbook('settings.xlsx')
# Gets the settings sheet
settingsSheet = settingsWorkbook.get_sheet_by_name('Sheet1')
# Finds the value for oder codes workbook name
ordersWorkbookName = settingsSheet['B6'].value
# If it is empty;
if str(ordersWorkbookName)=='None':
# Sets to default
ordersWorkbookName = sheet['C6'].value
# Gets the value of order codes sheet name
ordersSheetName = settingsSheet['B7'].value
# If its empty;
if str(ordersSheetName) == 'None':
# Sets to default
ordersSheetName = settingsSheet['C7'].value
# Gets folder directory
folderDirectory = settingsSheet['B2'].value
# If its empty;
if str(folderDirectory) == 'None':
# Prionts error message
print('Please Finish Settings by filling in: Folder Directory')
sys.exit()
chromeDriverDirectory = settingsSheet['B3'].value
if str(chromeDriverDirectory) == 'None':
print('Please Finish Settings by Filling in: Chrome Driver directory')
sys.exit()
ordersDirectory = settingsSheet['B5'].value
if str(ordersDirectory) == 'None':
print('Please Finish Settings by Filling in: Excel Orders File Directory')
sys.exit()
saveValue = settingsSheet['B8'].value
if str(saveValue) == 'None':
saveValue = settingsSheet['C8'].value
print('Using Default Save Value')
settingsDirectory = settingsSheet['B4'].value
if str(settingsDirectory) == 'None':
print('Please Finish Settings by Filling in: Excel Settings File Directory')
sys.exit
# Prints Updates of what's happening
print('Finished Pulling Settings')
#
Solution
Your code looks hard to read and not like Python code.
The main reason for this is that every second line is a comment. Most of these comments just repeat what the surrounding code does. These comments don't have any positive value and should therefore be removed.
The other reason is that you didn't structure the code into paragraphs. After each chunk of lines that do something useful, there should be a blank line to give the reader a time to breathe.
The third reason is that in Python code, all lines that begin in column 1 are definitions (for classes or functions), but not executable code. To make your code more pythonic, move all code that is currently in column 1 into functions.
Also, don't write any code above the
A typical Python file looks like this:
So far for the style.
A large portion of the code is for formatting the ETA. This code should be in its own function since the details of formatting it are not interesting enough for a reader of the
I didn't read the whole code for calculating the ETA, since it seems far too large. Did you mean this?
The main reason for this is that every second line is a comment. Most of these comments just repeat what the surrounding code does. These comments don't have any positive value and should therefore be removed.
The other reason is that you didn't structure the code into paragraphs. After each chunk of lines that do something useful, there should be a blank line to give the reader a time to breathe.
The third reason is that in Python code, all lines that begin in column 1 are definitions (for classes or functions), but not executable code. To make your code more pythonic, move all code that is currently in column 1 into functions.
Also, don't write any code above the
import statements.A typical Python file looks like this:
from selenium import browser
import sys
def first_function():
code()
def next_function():
code()
more_code()
def main():
# setup UI elements
root.mainloop()
if __name__ == '__main__':
main()So far for the style.
A large portion of the code is for formatting the ETA. This code should be in its own function since the details of formatting it are not interesting enough for a reader of the
findInfo function. The only thing that is interesting at this level is print("ETA: %s" % format_eta(seconds)).I didn't read the whole code for calculating the ETA, since it seems far too large. Did you mean this?
def format_eta(seconds):
hh = seconds / 3600
mm = seconds / 60 % 60
ss = seconds % 60
if hh != 0:
return "%d hours %d minutes" % (hh, mm)
return "%d minutes %d seconds" % (mm, ss)Code Snippets
from selenium import browser
import sys
def first_function():
code()
def next_function():
code()
more_code()
def main():
# setup UI elements
root.mainloop()
if __name__ == '__main__':
main()def format_eta(seconds):
hh = seconds / 3600
mm = seconds / 60 % 60
ss = seconds % 60
if hh != 0:
return "%d hours %d minutes" % (hh, mm)
return "%d minutes %d seconds" % (mm, ss)Context
StackExchange Code Review Q#147548, answer score: 5
Revisions (0)
No revisions yet.