Re: [Tutor] Custom Function that Takes argument

2015-10-21 Thread Alan Gauld

On 21/10/15 01:18, Nym City via Tutor wrote:


def zip_search(query):
 api_key = locu_api
 url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
 zip = query.replace(' ', '%20')
 final_url = url + '=' + zip + "=restaurant"
 jason_obj = urllib2.urlopen(final_url)
 data = json.load(jason_obj)
 for item in data['objects']:
 print item['name'], item['phone'], item['street_address'], 
item['categories'], item['website_url']

ans=True
while ans:
 print ("""
 1.Search by City
 2.Search by Region (State Abbreviation)
 3.Search by Zip
 4.Exit/Quit
 """)
 ans=raw_input("What would you like to do? ")
 if ans=="1":
   locality = raw_input("\nEnter City ")
   print locu_search(locality)
   break
 elif ans=="2":
 region = raw_input("\n Search by State ")
 print region_search(region)
 break
 elif ans=="3":
 zip = raw_input("\n Search by Zip ")
 print zip_search(zip)
 break
 elif ans=="4":
   print("\n Goodbye")
   break


Because you now process the results in the if clauses you no
longer need the breaks. In fact if you want the menu to be
repeated you need to take them all out except for the last one.



-
I am not sure if above changes exactly reflect your suggestions but I tested 
the logic and it seems to work.

The only issue is that on the first two queries (locu_search(locality) and
region_search(region)) I receive the following traceback error:
Traceback (most recent call last):
   File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 
47, in 
 print locu_search(locality)
   File 
"C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 
14, in locu_search
 print item['name'], item['phone'], item['street_address']
   File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
 return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u200e' in position 29: 
character maps to 


Looks like you are using cpl252 and your data is coming back as 
something else (UTF-8 maybe?) so you probably need to specify

the encoding.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Custom Function that Takes argument

2015-10-21 Thread Nym City via Tutor
Hello,
Thank you for your feedback. Sorry it took me a while to revise my code but 
here is my code with the changes:
import urllib2
import json

locu_api = 'redacted'

def locu_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    locality = query.replace(' ', '%20')
    final_url = url + '=' + locality + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address']

def region_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    region = query.replace(' ', '%20')
    final_url = url + '=' + region + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['locality'], item['website_url']

def zip_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    zip = query.replace(' ', '%20')
    final_url = url + '=' + zip + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['categories'], item['website_url']

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
  locality = raw_input("\nEnter City ")
  print locu_search(locality)
  break
    elif ans=="2":
    region = raw_input("\n Search by State ")
    print region_search(region)
    break
    elif ans=="3":
    zip = raw_input("\n Search by Zip ")
    print zip_search(zip)
    break
    elif ans=="4":
  print("\n Goodbye")
  break 
-
I am not sure if above changes exactly reflect your suggestions but I tested 
the logic and it seems to work. 

The only issue is that on the first two queries (locu_search(locality) and 
region_search(region)) I receive the following traceback error:
Traceback (most recent call last):
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 47, in 
    print locu_search(locality)
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 14, in locu_search
    print item['name'], item['phone'], item['street_address']
  File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u200e' in 
position 29: character maps to 

The last query about zip definition works perfectly. Please share thoughts on 
the revised logic and recommendation for handling the error above. Thanks!

Please
Thank you. 


 On Monday, October 12, 2015 8:30 PM, Alan Gauld 
 wrote:
   
 

 On 13/10/15 00:32, Nym City wrote:
>
> ans=raw_input("What would you like to do? ")
>    if ans=="1":
>      locality = raw_input("\nEnter City ")
>      break
>    elif ans=="2":
>        region = raw_input("\n Search by State ")
>        break
>    elif ans=="3":
>        zip = raw_input("\n Search by Zip ")
>        break
>    elif ans=="4":
>      print("\n Goodbye")
>      break
> ...  The use case is that by not knowing which of the 4 options the 
> user will select,


But you know what they select after they have done it so you need to wait
till that point to run the appropriate  query. And ans tells you which 
choice
the user made.

> NameError: name 'locality' is not defined
>
> 
> The error points back to where this is:
>
> varibles = [locu_search(locality), region_search(region), zip_search(zip)]
>
> I don't understand why Python thinks that locality (and others) are 
> not defined when I defined them at the very beginning.
>

You didn't. You only define them once the user makes a choice.
eg. If the ans is 3 then locality and region are not defined.

But even if they were (with default values) you don;t want to
run all the queries each time. Only run the query selected by the user.
So call the query functions in the if/else structure where you set the 
variables.


HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor