def numTrialChecker(num_trials): """ Make sure the list is 1 or greater """ try: num_trials = int(num_trials) if num_trials <1: error = errorHandlerClass("noZero") error.errorHandlerFunction("noZero") return num_trials except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction(thisError)
def inputReceiver(thisInput): ''' Make sure the range of input is between 1800 and 2012. Detacts other input errors as well ''' try: thisInput = int(thisInput) if thisInput > 2012 or thisInput < 1800: error = errorHandlerClass("outRange") error.errorHandlerFunction() else: return thisInput except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()
def infoFinder(thisAddress): ''' Parse the web for its information ''' page = requests.get(thisAddress) tree = html.fromstring(page.text) #get the html name_finder = tree.xpath('// h1[@itemprop="name"]/text()') try: #name needs cleaning name_finder = str(name_finder[0]).strip() name_finder = str(name_finder).encode('ascii', 'ignore') #Due to encoding, "'" might throw an error e.g. Wendy's except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction() street_finder = tree.xpath('//span[@itemprop="streetAddress"]/text()') #parse city_finder = tree.xpath('//span[@itemprop="addressLocality"]/text()') price_finder = tree.xpath('//span[@itemprop="priceRange"]/text()') phone_finder = tree.xpath('//span[@itemprop="telephone"]/text()') phone_finder = str(phone_finder[0]).strip() web_finder = tree.xpath('//div[@class="biz-website"]/a[@href]/text()') review_finder = tree.xpath('//li[@class="tab inline-block js-language-link selected"]/span[@class="count"]/text()') street_finder = "".join(street_finder) #make it as string price_finder = "".join(price_finder) #make it as string city_finder = "".join(city_finder) #make it as string web_finder = "".join(web_finder) #make it as string review_finder= "".join(review_finder) #make it as string return name_finder, street_finder, city_finder, price_finder, phone_finder, web_finder, review_finder
def askInput(): ''' Print out options and receives an input. ''' try: print "" print "*"*30 print "Please select from following:" print "Type in 1 to See violations of popular restaurants in NYC" print "Type in 2 to View the Heatmap of NYC restaurants" print "Type in 3 to Explore the restaurant and the number of violations in your RestaurantKeeper" print "Type in 4 to Explore restaurants in your RestaurantKeeper grouped by types and the number of violations" print "Type in 5 to Explore violations of popular restaurants in NYC grouped by types" print "Type in 6 to Add a restaurant to your Restaurant Keeper" print "Type in 7 to Quick View of my Restaurant Keeper" print "Type in 8 to Full View of my Restaurant Keeper" print "Type in 9 to Reset my Restaurant Keeper" print "Type in 0 to Quit" print "*"*30 print "" thisResponse = int(raw_input("What is your choice? ")) optionPicker(thisResponse) #optionPicker will delegate tasks except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()
def askInput(): ''' Print out options and receives an input. ''' try: print "" print "*" * 30 print "Please select from following:" print "Type in 1 to See violations of popular restaurants in NYC" print "Type in 2 to View the Heatmap of NYC restaurants" print "Type in 3 to Explore the restaurant and the number of violations in your RestaurantKeeper" print "Type in 4 to Explore restaurants in your RestaurantKeeper grouped by types and the number of violations" print "Type in 5 to Explore violations of popular restaurants in NYC grouped by types" print "Type in 6 to Add a restaurant to your Restaurant Keeper" print "Type in 7 to Quick View of my Restaurant Keeper" print "Type in 8 to Full View of my Restaurant Keeper" print "Type in 9 to Reset my Restaurant Keeper" print "Type in 0 to Quit" print "*" * 30 print "" thisResponse = int(raw_input("What is your choice? ")) optionPicker(thisResponse) #optionPicker will delegate tasks except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()
def positionChecker(positions): """ Checks incorrect inputs and returns input as a list """ positions = str(positions).strip("[").strip("]").replace(" ", "") positions = positions.split(",") if positions==['']: error = errorHandlerClass("shortString") error.errorHandlerFunction("shortString") else: for item in positions: if int(item) >1000: error = errorHandlerClass("overLimit") error.errorHandlerFunction("overLimit") elif int(item) < 0: error = errorHandlerClass("negative") error.errorHandlerFunction("negative") return positions
def listBuilder(): ''' Compare the information from Yelp and join it with violation information from the city data and save it on json file These are some of links from Yelp (Top 10 restaurants in NYC according to Zagat) http://www.yelp.com/biz/le-bernardin-new-york http://www.yelp.com/biz/eleven-madison-park-new-york http://www.yelp.com/biz/daniel-new-york http://www.yelp.com/biz/sushi-yasuda-new-york http://www.yelp.com/biz/gramercy-tavern-new-york http://www.yelp.com/biz/peter-luger-steak-house-brooklyn http://www.yelp.com/biz/la-grenouille-new-york ''' print "Please copy and paste the Yelp's link of the restaurant that you would like to add" try: print ("e.g. http://www.yelp.com/biz/bouley-new-york-2") thisLink = str(raw_input("----> ")).replace(" ", "") name_finder, street_finder, city_finder, price_finder, phone_finder, web_finder, review_finder = infoFinder(thisLink) thisPhoneNum = re.sub("[()-]", '', phone_finder).replace(' ','') if os.path.exists("restaurant_list.csv")==True : #in case if the user has used the program before to save a restaurant myRestaurantFromFile = pd.io.parsers.read_csv('restaurant_list.csv',sep="\t") #read the file if myRestaurantFromFile['PHONE'].isin([int(thisPhoneNum)]).sum()>=1: #previously stored print "%s is already stored" %name_finder askInput() #data is already stored else: #new entry thisRating=ratingList[ratingList['PHONE'].isin([thisPhoneNum])] if thisRating.empty: #in case the phone number on Yelp can not be found in NYC Inspection data thisRating=pd.DataFrame([{"DBA":"To be Updated", "BORO":"To be Updated", "ZIPCODE":"To be Updated", "PHONE":thisPhoneNum, "CUISINE DESCRIPTION":"To be Updated", "INSPECTION DATE":"To be Updated", "VIOLATION DESCRIPTION":"To be Updated", "CRITICAL FLAG":"To be Updated", "SCORE":"To be Updated", "GRADE":"To be Updated", "GRADE DATE":"To be Updated"}]) myRestaurantPD = pd.DataFrame([{"DBA_fromYelp":name_finder, "ADDRESS":street_finder, "CITY":city_finder, "PRICE":price_finder, "PHONE":thisPhoneNum, "WEB":web_finder, "REVIEW":review_finder}]) myRestaurantPD = pd.merge(myRestaurantPD, thisRating, on=["PHONE"], how='inner') new = myRestaurantPD.append(myRestaurantFromFile, ignore_index=True, verify_integrity=False) new.to_csv('restaurant_list.csv', sep='\t', encoding='utf-8', index=False) print "%s is successfully added" %name_finder else: #the user is new thisRating=ratingList[ratingList['PHONE'].isin([thisPhoneNum])] if thisRating.empty: #in case the phone number on Yelp can not be found in NYC Inspection data thisRating=pd.DataFrame([{"DBA":"To be Updated", "BORO":"To be Updated", "ZIPCODE":"To be Updated", "PHONE":thisPhoneNum, "CUISINE DESCRIPTION":"To be Updated", "INSPECTION DATE":"To be Updated", "VIOLATION DESCRIPTION":"To be Updated", "CRITICAL FLAG":"To be Updated", "SCORE":"To be Updated", "GRADE":"To be Updated", "GRADE DATE":"To be Updated"}]) myRestaurantPD = pd.DataFrame([{"DBA_fromYelp":name_finder, "ADDRESS":street_finder, "CITY":city_finder, "PRICE":price_finder, "PHONE":thisPhoneNum, "WEB":web_finder, "REVIEW":review_finder}]) myRestaurantPD = pd.merge(myRestaurantPD, thisRating, on=["PHONE"], how='inner') #the user is new myRestaurantPD.to_csv('restaurant_list.csv', sep='\t', encoding='utf-8', index=False) print "%s is successfully added" %name_finder myRestaurantPD.drop(myRestaurantPD.index[:]) #clear the dataframe askInput() except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()
def inputReceiver(position_value, num_trials): """ Receives the number of shares """ print "Please type in a list of the number of shares to buy in parallel:" print "e.g. [1, 10, 100, 1000]: " try: positions = raw_input("---->") positions = positionChecker(positions) #check input num_trials = int(input("Please type in number of trials you want to run: ")) num_trials = numTrialChecker(num_trials) #check number for item in positions: eachPosition = int(item) position_value[eachPosition] = 1000/eachPosition #10 will have 100 position return position_value, num_trials except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction(thisError)
def infoFinder(thisAddress): ''' Parse the web for its information ''' page = requests.get(thisAddress) tree = html.fromstring(page.text) #get the html name_finder = tree.xpath('// h1[@itemprop="name"]/text()') try: #name needs cleaning name_finder = str(name_finder[0]).strip() name_finder = str(name_finder).encode( 'ascii', 'ignore') #Due to encoding, "'" might throw an error e.g. Wendy's except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction() street_finder = tree.xpath( '//span[@itemprop="streetAddress"]/text()') #parse city_finder = tree.xpath('//span[@itemprop="addressLocality"]/text()') price_finder = tree.xpath('//span[@itemprop="priceRange"]/text()') phone_finder = tree.xpath('//span[@itemprop="telephone"]/text()') phone_finder = str(phone_finder[0]).strip() web_finder = tree.xpath('//div[@class="biz-website"]/a[@href]/text()') review_finder = tree.xpath( '//li[@class="tab inline-block js-language-link selected"]/span[@class="count"]/text()' ) street_finder = "".join(street_finder) #make it as string price_finder = "".join(price_finder) #make it as string city_finder = "".join(city_finder) #make it as string web_finder = "".join(web_finder) #make it as string review_finder = "".join(review_finder) #make it as string return name_finder, street_finder, city_finder, price_finder, phone_finder, web_finder, review_finder
sys.exit(1) if __name__ == "__main__": thisfile1 = "countries.csv" countryDF = countryCsvReader(thisfile1) thisfile2 = "indicator gapminder gdp_per_capita_ppp.xlsx" incomeDF = incomeCsvReader(thisfile2) while True: try: thisInput = raw_input( "Please type in the year between 1800 to 2012: ") if (thisInput == "finish") or (thisInput == "Finish") or ( thisInput == "FINISH") or (thisInput == "f"): #calls finishing() finishing() elif (thisInput == "quit") or (thisInput == "Quit") or ( thisInput == "QUIT") or (thisInput == "q"): #quit print "Bye" sys.exit(1) else: #otherwise, inputReceiver will check for its value thisYear = inputReceiver(thisInput) yearGraph(incomeDF, thisYear) except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()
def listBuilder(): ''' Compare the information from Yelp and join it with violation information from the city data and save it on json file These are some of links from Yelp (Top 10 restaurants in NYC according to Zagat) http://www.yelp.com/biz/le-bernardin-new-york http://www.yelp.com/biz/eleven-madison-park-new-york http://www.yelp.com/biz/daniel-new-york http://www.yelp.com/biz/sushi-yasuda-new-york http://www.yelp.com/biz/gramercy-tavern-new-york http://www.yelp.com/biz/peter-luger-steak-house-brooklyn http://www.yelp.com/biz/la-grenouille-new-york ''' print "Please copy and paste the Yelp's link of the restaurant that you would like to add" try: print("e.g. http://www.yelp.com/biz/bouley-new-york-2") thisLink = str(raw_input("----> ")).replace(" ", "") name_finder, street_finder, city_finder, price_finder, phone_finder, web_finder, review_finder = infoFinder( thisLink) thisPhoneNum = re.sub("[()-]", '', phone_finder).replace(' ', '') if os.path.exists( "restaurant_list.csv" ) == True: #in case if the user has used the program before to save a restaurant myRestaurantFromFile = pd.io.parsers.read_csv( 'restaurant_list.csv', sep="\t") #read the file if myRestaurantFromFile['PHONE'].isin( [int(thisPhoneNum)]).sum() >= 1: #previously stored print "%s is already stored" % name_finder askInput() #data is already stored else: #new entry thisRating = ratingList[ratingList['PHONE'].isin( [thisPhoneNum])] if thisRating.empty: #in case the phone number on Yelp can not be found in NYC Inspection data thisRating = pd.DataFrame([{ "DBA": "To be Updated", "BORO": "To be Updated", "ZIPCODE": "To be Updated", "PHONE": thisPhoneNum, "CUISINE DESCRIPTION": "To be Updated", "INSPECTION DATE": "To be Updated", "VIOLATION DESCRIPTION": "To be Updated", "CRITICAL FLAG": "To be Updated", "SCORE": "To be Updated", "GRADE": "To be Updated", "GRADE DATE": "To be Updated" }]) myRestaurantPD = pd.DataFrame([{ "DBA_fromYelp": name_finder, "ADDRESS": street_finder, "CITY": city_finder, "PRICE": price_finder, "PHONE": thisPhoneNum, "WEB": web_finder, "REVIEW": review_finder }]) myRestaurantPD = pd.merge(myRestaurantPD, thisRating, on=["PHONE"], how='inner') new = myRestaurantPD.append(myRestaurantFromFile, ignore_index=True, verify_integrity=False) new.to_csv('restaurant_list.csv', sep='\t', encoding='utf-8', index=False) print "%s is successfully added" % name_finder else: #the user is new thisRating = ratingList[ratingList['PHONE'].isin([thisPhoneNum])] if thisRating.empty: #in case the phone number on Yelp can not be found in NYC Inspection data thisRating = pd.DataFrame([{ "DBA": "To be Updated", "BORO": "To be Updated", "ZIPCODE": "To be Updated", "PHONE": thisPhoneNum, "CUISINE DESCRIPTION": "To be Updated", "INSPECTION DATE": "To be Updated", "VIOLATION DESCRIPTION": "To be Updated", "CRITICAL FLAG": "To be Updated", "SCORE": "To be Updated", "GRADE": "To be Updated", "GRADE DATE": "To be Updated" }]) myRestaurantPD = pd.DataFrame([{ "DBA_fromYelp": name_finder, "ADDRESS": street_finder, "CITY": city_finder, "PRICE": price_finder, "PHONE": thisPhoneNum, "WEB": web_finder, "REVIEW": review_finder }]) myRestaurantPD = pd.merge(myRestaurantPD, thisRating, on=["PHONE"], how='inner') #the user is new myRestaurantPD.to_csv('restaurant_list.csv', sep='\t', encoding='utf-8', index=False) print "%s is successfully added" % name_finder myRestaurantPD.drop(myRestaurantPD.index[:]) #clear the dataframe askInput() except: thisError = sys.exc_info()[0] error = errorHandlerClass(thisError) error.errorHandlerFunction()