2

So I am new to coding and this is my first question so I apologise if the formatting is incorrect.

I am trying to figure out how to combine the (get_x_start) and the (get_x_end) functions {where x is the unique name given to the pair of functions} such that there is one function for each x that returns (field) and (end). I tried just using a list ie:

return (field,end) 

But then when I tried I didn't know how to use those as indivual perameters within (main()) such that the function (fetch_data) could use both of the returned parameters from a single x function.

My code follows. If anything in it is unclear please let me know and I will try and clarify. Thanks.

def get_data (url):  
    response = url  
    return str(response.read()) 

def fetch_data (field,data,end):  
    sen = data[data.find(field)+len(field)+3:data.find(end)-3]  
    return sen

def get_name_start (data):  
    field = "Name"  
    return field  

def get_name_end (data):  
    end = "ClosingTime"  
    return end

def get_open_start (data):  
    field = "OpeningTime"  
    return field

def get_open_end (data):  
    end = "Name"  
    return end

def get_close_start (data):  
    field = "ClosingTime"  
    return field

def get_close_end (data):  
    end = "CoLocated"  
    return end

def main ():  
    import urllib.request  
    data = get_data (urllib.request.urlopen('http://www.findfreewifi.co.za/publicjson/locationsnear?lat=-33.9568396&lng=18.45887&topX=1'))  
    print (fetch_data(get_name_start(data),data,get_name_end(data)))  
    print (fetch_data(get_open_start(data),data,get_open_end(data)))  
    print (fetch_data(get_close_start(data),data,get_close_end(data)))  

main ()
gboffi
  • 22,939
  • 8
  • 54
  • 85
Carrion_7
  • 23
  • 1
  • 3

2 Answers2

4

Here it is a function that returns a tuple

def return_ab():
    return 5, 6

Here it is a function that accepts three arguments

def test(a, b, c):
    print(a, b, c)

And here it is how you can call the test() function passing it the two parameters returned by return_ab() but unpacked

test(*return_ab(), 7) # => 5 6 7

The key point is the asterisk in front of return_ab(). It's a relatively new syntax that is very useful indeed...

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Did I close the question properly by accepting your answer? – Carrion_7 Apr 24 '17 at 17:41
  • @Carrion_7 Yes and no... Accepting the answer you state that my answer solved your problem but you are free to change your mind and choose a different answer that _better_ answers your needs. But I'd be happier if you won't change your mind... – gboffi Apr 24 '17 at 18:15
  • How do I close the question? – Carrion_7 Apr 25 '17 at 13:33
  • @Carrion_7 You don't! you possibly accept an answer, maybe upvote the accepted answer and/or other useful answers, and that's all... On Stack Overflow answers are not closed. Ask other questions, answer other people's questions, in two words _move on_ – gboffi Apr 25 '17 at 18:06
0

From this post here multiple variables can be returned like this

def f(in_str):
    out_str = in_str.upper()
    return True, out_str # Creates tuple automatically

succeeded, b = f("a") # Automatic tuple unpacking

From here you can then pass your variables into the new function like:

new_f(succeeded, b)

Hope this helps

Community
  • 1
  • 1
kopo222
  • 119
  • 11