0

In my code below, I am trying to rerun a new list which is created by removing the first three list items that were already matched to a track in the giveHref() function. I attempt to run the new list through the decrementList() function, but I receive this error: generator object decrementList at 0x104cb8050. Why am I unable to run this new list through the function again?

I'm also having issues on having this done automatically until there are no more list items, so any advice/suggestions on this would be helpful as well. I would like this program to run once and return all HREFs for matching decremented list items that match to API tracks.

import requests
import json

# initial message
message = "if i can't let it go out of my mind"

# split into list
split_message = message.split()

# get lenth of original list
size = len(split_message)

def decrementList(words):
    #decrement list by 1 for each try
    for w in [ words ] + [ words[:-x] for x in range(1, len(words)) ]:
        url = 'http://ws.spotify.com/search/1/track.json?q='
        request = requests.get(url + "%20".join(w))

        json_dict = json.loads(request.content)
        num_results = json_dict['info']['num_results']

        if num_results > 0:
            num_removed = len(words) - len(w)

            track_title = ' '.join(w)

            # yield number of list items removed, possible track name that
            # is comprised of list items, actual track names from API and new decremented list
            # all through generator
            for track in json_dict["tracks"]:
                if track["name"].lower().startswith(track_title.lower()):
                    yield num_removed, track, track["name"], w


# finds and returns HREF of first match of track name made from decremented list and API track name
def giveHref():
    for num_words_removed, track, track_name, w in decrementList(split_message):
        if ' '.join(w) == track_name.lower():
            return track['href'], num_words_removed


track['href'], num_words_removed = giveHref()

# creates new list with items from the previously matched track removed
# in this case, first three list items are removed, as they previously matched to a track
def newMessage():
    new_message_length =  size - num_words_removed
    new_message = split_message[new_message_length:size]
    return new_message


new_message = newMessage()

decrementList(new_message)
metersk
  • 11,803
  • 21
  • 63
  • 100

1 Answers1

1

By using yield, you are getting a generator object from your function, not a list. You have to iterate through it, or coerce it to a list if you want to use it.

[x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

(x for x in range(10))
<generator object <genexpr> at 0x137a870>

list((x for x in range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Read some docs or see this answer and make sure you understand the difference.

In your case, you can just use a list comprenhension to get what you seem to want.

def dec(l):
    for i, w in enumerate(l):
        yield l[i:]

stuff = ["one", "two", "three", "four"]
print [x for x in dec(stuff)]
Community
  • 1
  • 1
bennomadic
  • 71
  • 1
  • 4
  • i am not looking to decrement the list by one each time. i am looking to decrement the list to the next track match, which requires me sending the new_message returned value through the functions again. not sure if this helps me – metersk Jan 11 '14 at 06:29