In my program, I prompt the user to enter a range of cities (in a spreadsheet), and then a range of review scores, and then make some decisions based on what the user has entered. Since the handling of the both user inputs (both ranges) is common to each, I have combined both into the single function getRanges()
below. It works well, but I get the sense that can be improved (shortened, and with fewer if/else's). In the absence of a switch
statement in Python, what are some neater approaches here?
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import WebDriverException
import unittest, time, re
from functions import *
from collections import OrderedDict
import csv
from openpyxl import load_workbook
from easygui import enterbox, msgbox
import traceback
class bot(unittest.TestCase):
def setUp(self):
self.wb = load_workbook("Spreadsheet.xlsx", data_only=True)
self.cities = []
self.getRanges('cities')
self.getRanges('reviews')
print 'Running...'
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com/ncr"
self.verificationErrors = []
self.accept_next_alert = True
self.reviewPattern = re.compile(ur'\d+')
self.niche = "plumbers+"
def getRanges(self, citiesOrReviews):
self.LowerLimit = enterbox("Enter a lower limit for %s, or 'all'" % citiesOrReviews, 'Range')
if self.LowerLimit.lower().strip("'") == "all":
self.reviewRange = False
if citiesOrReviews.lower() == 'cities':
for row in self.wb.active.rows:
self.cities.append(row[0].value)
elif self.LowerLimit.isdigit():
if citiesOrReviews.lower() == 'cities':
if int(self.LowerLimit) > len(self.wb.active.rows):
msgbox("The lower limit you've entered is higher than the number of cities in the spreadsheet. Let's retry.", 'Error')
self.getRanges(citiesOrReviews)
self.UpperLimit = enterbox("Enter an upper limit for %s:" % citiesOrReviews, "Range")
if self.UpperLimit.isdigit():
if citiesOrReviews.lower() == 'cities':
if int(self.UpperLimit) > len(self.wb.active.rows):
msgbox("The upper limit you've entered is higher than the number of cities in the spreadsheet. Let's retry.", "Error")
self.getRanges(citiesOrReviews)
if self.UpperLimit <= self.LowerLimit:
msgbox("The upper and lower limits are identical, or the upper limit is less than the lower. Let's retry.", "Error")
self.getRanges(citiesOrReviews)
else:
if citiesOrReviews.lower() == 'cities':
for row in self.wb.active.rows[int(self.LowerLimit) - 1:int(self.UpperLimit)]:
self.cities.append(row[0].value)
elif citiesOrReviews.lower() == 'reviews':
self.reviewRange = [int(self.LowerLimit) - 1, int(self.UpperLimit)]
else:
msgbox("The value entered must either be 'all', or a number. Let's retry.", "Error")
self.getRanges(citiesOrReviews)
switch
? For example: stackoverflow.com/questions/60208/… \$\endgroup\$if/else's
, and I don't think (because of how I have less-than-optimally sequenced the flow of the function) that utilizingelif
is really going to help me out here. \$\endgroup\$