Python - 通过命令行将参数传递给模块函数

时间:2016-11-22 03:00:33

标签: python parameter-passing

我在名为tags.py的{​​{1}}中定义了此功能:

 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)

tags.py,作为主要成功,使用:

成功调用
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

by:$ 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'}

2 个答案:

答案 0 :(得分:2)

如果这个有效:

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

这可以为您提供一些有关代码错误的有用信息。如果您发现所有打印变量都符合预期,那么我很高兴回顾一下。

答案 1 :(得分:0)

根据@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)]