[英]Python - passing parameter via command line to module function
我在名為tags.py
的module
定義了此功能:
def lastfm_artist_to_tags(artist):
tag_weight = {}
result = last.get_artist(artist).get_top_tags()
for tag in result:
tag_weight[str(tag.item.get_name())] = str(tag.weight)
tag_weight = {k: int(v) for k, v in tag_weight.items()}
return sorted(tag_weight.items(), key=lambda x: x[1], reverse=True)
作為main的tags.py
被成功地調用with:
print lastfm_artist_to_tags('radiohead') //note string
但我正在嘗試將上述函數導入到我的playlist.py
,如下所示:
#playlist.py
from api.lastfm.seeds.tags import *
class hardrock(rock):
def __init__(self,name,user):
rock.__init__(self, name, user)
# get tags for this artist
tags = lastfm_artist_to_tags(str(artist))
並通過命令行調用它:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
artist = get_artist(artist_name) //get_artist() returns json response from Spotify API
作者: $ python playlist.py radiohead
但我收到以下錯誤:
Error: The artist you supplied could not be found
我也試圖通過lastfm_artist_to_tags(artist)
,但無濟於事。
文件夾結構:
playlist.py
api/
__init__.py
lastfm/
__init__.py
seeds/
__init__.py
tags.py
我在這里想念什么?
編輯:
print (artist_name)
和print (str(artist_name))
從SPotify API
返回相同的結果:
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
如果這個可行:
print lastfm_artist_to_tags('radiohead') //note string
這意味着函數lastfm_artist_to_tags
正在運行,您應該在其他地方查找問題。
從另一個模塊調用時,您可能應該檢查是否確實將'radiohead'
傳遞給相同的函數。 最簡單的方法是打印參數:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
print(artist_name) # Add this line
artist = get_artist(artist_name)
print(artist) # Add this line
print(str(artist)) # Add this line
tags = lastfm_artist_to_tags(str(artist))
這應該為您提供有關代碼錯誤的一些有用信息。 如果您發現所有打印的變量都符合預期,那么我很高興回頭。
基於@Cyker的答案,並且由於get_artist()
返回json
響應,因此我必須像這樣修復它:
tags = lastfm_artist_to_tags(str(artist['name']))
然后tags
將打印:
[('britpop', 100), ('rock', 89), ('british', 61), ('alternative', 53), ('indie', 46), ('seen live', 21), ('alternative rock', 19), ('indie rock', 12), ('90s', 10), ('oasis', 9), ('pop', 7), ('Manchester', 4), ('UK', 4), ('classic rock', 3), ('Britrock', 3), ('male vocalists', 2), ('00s', 2), ('hard rock', 2), ('brit pop', 2), ('pop rock', 2), ('british rock', 2), ('english', 2), ('brit rock', 2), ('england', 1), ('favorites', 1), ('punk', 1)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.