Re: [Tutor] Custom Function that Takes argument

2015-10-31 Thread Nym City via Tutor
Hello,
Thank you for your response. I went back and updated encoding to utf-8 and 
ASCII but I still experienced issues with the output.
The part that is interesting is that the output for each of the following 
fields is the (should be) the same    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip

Search by zip comes out just fine but only the partial output is shared for the 
first two options. I don't understand why that would be? I would think if its 
the same output for all three options that I would either work for all three or 
not work for any. Also, why the partial results. 

Any suggestions on how to normalize this?

 Thank you. 


 On Wednesday, October 21, 2015 5:53 AM, Alan Gauld 
 wrote:
   
 

 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


 
  
___
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 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


Re: [Tutor] Custom Function that Takes argument

2015-10-12 Thread Nym City via Tutor
Thank you for your response. I updated the first portion of my code to include 
breaks: 

import urllib2
import json

locu_api = 'redacted'

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 ")
  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

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']

def non_empty_variable(varibles):
    for varible in varibles:
    if varible != "":
    print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]


-
With the above updates, the user prompts are working fine now. Thanks!

The issue that I am trying to solve now is print the final result.  The use 
case is that by not knowing which of the 4 options the user will select, i need 
a way to figure out which selection user made, run it against the appropriate 
query and than print out the result. 
My thought process is to narrow down the user selection by figuring out which 
of the 4 choices is not blank. To do this I created this:
def non_empty_variable(varibles):
    for varible in varibles:
    if varible != "":
    print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]
__I think the above should work but I get traceback 
error:
Traceback (most recent call last):
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 70, in 
    varibles = [locu_search(locality), region_search(region), zip_search(zip)]
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. 


___After I get this new issue resolved, I will go back 
and review urllib query functions. 
 Thank you. 


 On Sunday, October 11, 2015 6:25 PM, Alan Gauld 
 wrote:
   
 

 On 11/10/15 22:22, Nym City wrote:
> import urllib2
> import json
>
> locu_api = 'redacted'
>
> ans=True
> while ans:
>    print ("""
>    1.Search by City
>    2.Search by State
>    3.Search by Zip
>    4.Exit/Quit
>    """)
>    ans=raw_input("What would you like to do? ")
>    if ans=="1":
>      print("\n Enter City")
>    elif ans=="2":
>      print("\n Search by State")
>    elif ans=="3":
>      print("\n Search by Zip")
>    elif ans=="4":
>      print("\n Goodbye")
>    elif ans !="":
>      print("\n Not Valid Choice Try again")
>
>

Note that you never set ans to any false value so it will keep on looping
until you do (by entering an empty string).

You probably need to use break to exit the loop whenb you get valid input.

> # 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"

Note that you should not try to build the query string yourself, there are
functions in the standard library that will do most of the work and they
will be more reliable than anythong you can write yourself.

For example look at the urllib module (for 2.7, its the urlib.parse module
in v3) which contains several utility functions such as quote() and
urlencode()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

Re: [Tutor] Custom Function that Takes argument

2015-10-12 Thread Alan Gauld

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


Re: [Tutor] Custom Function that Takes argument

2015-10-11 Thread Alan Gauld

On 11/10/15 22:22, Nym City wrote:

import urllib2
import json

locu_api = 'redacted'

ans=True
while ans:
print ("""
1.Search by City
2.Search by State
3.Search by Zip
4.Exit/Quit
""")
ans=raw_input("What would you like to do? ")
if ans=="1":
  print("\n Enter City")
elif ans=="2":
  print("\n Search by State")
elif ans=="3":
  print("\n Search by Zip")
elif ans=="4":
  print("\n Goodbye")
elif ans !="":
  print("\n Not Valid Choice Try again")




Note that you never set ans to any false value so it will keep on looping
until you do (by entering an empty string).

You probably need to use break to exit the loop whenb you get valid input.


# 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"


Note that you should not try to build the query string yourself, there are
functions in the standard library that will do most of the work and they
will be more reliable than anythong you can write yourself.

For example look at the urllib module (for 2.7, its the urlib.parse module
in v3) which contains several utility functions such as quote() and
urlencode()


--
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-11 Thread Nym City via Tutor
Thank you for your feedback. I modified the code to reflect your suggestion and 
it worked. Now, I want to take it to the next level when I prompt the user to 
make a selection from a list of options and based on their input certain part 
of the code would execute. Here is the revised code:(site: https://locu.com/)

import urllib2
import json

locu_api = 'redacted'

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by State
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
  print("\n Enter City")
    elif ans=="2":
  print("\n Search by State")
    elif ans=="3":
  print("\n Search by Zip")
    elif ans=="4":
  print("\n Goodbye")
    elif ans !="":
  print("\n Not Valid Choice Try again")


# city = input_city = raw_input('Please enter your city: ')
# region = input_region = raw_input('Please enter your state: ')
# zip = raw_input('What is your zip: ')

# 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']
#
# # 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']
#
# # print zip_search(zip)
-
The part that is not working is the initial prompt for selection. The program 
prompts the user to make a selection, however it is not registering that 
selection rather repeats the prompt.

Hope my question make sense. Please let me know what I am missing. Thank you. 
Thank you. 


 On Monday, September 28, 2015 9:03 PM, Alan Gauld 
 wrote:
   
 

 On 29/09/15 00:45, Nym City via Tutor wrote:

> I am learning how to create custom functions and watched the
 > tutorial online that uses API for locu

Since most folks here probably don't know locu you should
maybe give us a URL. Then we can know what it is you are
talking about.

> I cannot figure out how to prompt a user to input their zip
 > code and use that information against the function.

The normal way is to pass it in as a parameter of the function:

def myfunc(aZipCode):
    print aZipCode

zip = raw_input('What is your zip: ')
myfunc(zip)


> import urllib2
> import json
>
> locu_api = 'not included here for obvious reasons'
> zip = raw_input('What is your zip: ')
>
> 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']
>
> print zip_search(zip_search())

Here you are passing the results of your own function
in as an argument to your function. That should give an
error since the inner call has no argument and the
function is looking for one.
You need to pass in your query string:

print zip_search(zip)

Always post any errors you get it helps us help you.


-- 
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


 
  
___
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-09-28 Thread Alan Gauld

On 29/09/15 00:45, Nym City via Tutor wrote:


I am learning how to create custom functions and watched the

> tutorial online that uses API for locu

Since most folks here probably don't know locu you should
maybe give us a URL. Then we can know what it is you are
talking about.


I cannot figure out how to prompt a user to input their zip

> code and use that information against the function.

The normal way is to pass it in as a parameter of the function:

def myfunc(aZipCode):
print aZipCode

zip = raw_input('What is your zip: ')
myfunc(zip)



import urllib2
import json

locu_api = 'not included here for obvious reasons'
zip = raw_input('What is your zip: ')

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']

print zip_search(zip_search())


Here you are passing the results of your own function
in as an argument to your function. That should give an
error since the inner call has no argument and the
function is looking for one.
You need to pass in your query string:

print zip_search(zip)

Always post any errors you get it helps us help you.


--
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