0

This code

def get_recent_tracks(username, fromuts):
    print(fromuts)
    recent_tracks = lastfm_network.get_user(username).get_recent_tracks(**{'from':fromuts})
    for i, track in enumerate(recent_tracks):
        printable = track_only(track)
        print(str(i + 1) + " " + printable)
    return recent_tracks

results in

get_recent_tracks() got an unexpected keyword argument 'from'

The print() function returns the correct result. I learned several programming languages in my youth. Unfortunately, my youth being some 30 years ago, Python was not one of them. What I need is a working equivalent of

recent_tracks = lastfm_network.get_user(username).get_recent_tracks(from=fromuts)

For other params like limit=20 the code works just fine.

Dierk
  • 3
  • 1
  • 1
    You haven't said what API/library you're using; the first relevant result on Google suggests it might be Elixirfm, in which case `get_recent_tracks` simply does not have an argument named `from`, hence the error message that this argument is unexpected: https://hexdocs.pm/elixirfm/0.1.2/Elixirfm.User.html#get_recent_tracks/2 – kaya3 Mar 28 '21 at 16:36
  • Sorry for that. It's supposed to be the Last.fm API: https://www.last.fm/api/show/user.getRecentTracks And of course it's Python. – Dierk Mar 28 '21 at 16:56

1 Answers1

0

There's nothing strictly wrong with the format of your code. That code will get around the problem of from being a Python keyword, and it is doing so. So you've gotten past that problem. The problem now is more fundamental and has nothing to do with the fact that from is a keyword...

The problem is simply that the get_recent_tracks method does not accept a named parameter with the name from. You'd expect this, because it would be hard for it to define such a parameter, and it wouldn't make sense for it to do so knowing that calling it with such a named parameter would be problematic. Read the docs for the method and supply it with the correct parameters, and you won't have an issue.

UPDATE: Collecting information from comments, the issue seems to be that the OP was assuming a one-to-one mapping between the names and existence of each of the parameters to the HTTP API call that underlies the get_recent_tracks Python method. Here are the docs for each of these APIs:

Python: https://hexdocs.pm/elixirfm/0.1.2/Elixirfm.User.html#get_recent_tracks/2

HTTP: https://last.fm/api/show/user.getRecentTracks

According to @Dierk, the OP, the equivalent parameter exists in the Python API and has the name time_from. This isn't reflected in the documentation referenced above. I notice that in that same documentation, there is the comment "start and end times not implemented yet" for another method in the API. So maybe this documentation is outdated, and support for the "time" parameters has since been added to the API.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • According to https://www.last.fm/api/show/user.getRecentTracks it should. Or did I understand "named parameter" wrong? – Dierk Mar 28 '21 at 16:55
  • That document doesn't describe the Python binding, but rather just the HTTP API. What leads you to believe that the Python equivalent should take a `from`? Could it be that other parameters map the way you're expecting, but `from` has been renamed since it is a reserved word in Python? Is there documentation for the Python API? – CryptoFool Mar 28 '21 at 17:00
  • It appears that the docs that @kaya3 references might be describing the Python binding to this same API. Other parameter names match up pretty well. But the Python binding doesn't take a `from` parameter. Why that is, I can't tell you. I would have expected it to be there with a different name than `from`. Other parameters have different names between the HTTP and Python bindings. – CryptoFool Mar 28 '21 at 17:04
  • Ah, now I understand. API "limit" is Python "limit" (as I wrote, limit=20 does work), but that's not necessarily the case for "from". Sneaky. It imports pylast, and the documentation for that is not really extensive. But you've given me a pointer where to look. So, thanks! – Dierk Mar 28 '21 at 17:06
  • Yeah...super sneaky. There is no `to` parameter either, which I guess is expected if there's no `from`. I notice that `extended` was renamed to `extended_info`. This is why it's always good to refer to the docs for the particular language binding, at least as soon as your assumptions about mappings from one API to another start to break down. - Best of luck getting this worked out! – CryptoFool Mar 28 '21 at 17:09
  • Found it. It's "time_from". Thanks for making me learn new stuff :-) – Dierk Mar 28 '21 at 17:16
  • Coolio! Glad you could figure it out. - I'll update my answer to include this, as I have done so to include the other stuff we've learned. Feel free to accept it as the answer to your question if you feel it merits that :) – CryptoFool Mar 28 '21 at 17:17