2013-12-12 36 views
2

嗨我想为'The Movie Database'API使用simpletmdb python包装,不会通过这个问题。TypeError:必须使用'class name'实例作为第一个参数调用unbound方法'method name'(代替str实例)

当我尝试创建对象并调用获取电影信息的方法时,我不断收到此错误。

in info 
response = TMDB._request('GET', path, params) 
TypeError: unbound method _request() must be called with TMDB instance as first argument  (got str instance instead) 

我调用它的代码是:

from tmdbsimple import TMDB 

tmdb = TMDB('API_KEY') 
movie = tmdb.Movies(603) 
response = movie.info() 
print movie.title 

而且simpletmdb包装的neccessary部分是,在电影类是TMDB的子类:

class TMDB: 
    def __init__(self, api_key, version=3): 
     TMDB.api_key = str(api_key) 
     TMDB.url = 'https://api.themoviedb.org' + '/' + str(version) 

    def _request(method, path, params={}, json_body={}): 
     url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key 
     if method == 'GET': 
      headers = {'Accept': 'application/json'} 
      content = requests.get(url, params=params, headers=headers).content 
     elif method == 'POST': 
      for key in params.keys(): 
       url += '&' + key + '=' + params[key] 
      headers = {'Content-Type': 'application/json', \ 
         'Accept': 'application/json'} 
      content = requests.post(url, data=json.dumps(json_body), \ 
            headers=headers).content 
     elif method == 'DELETE': 
      for key in params.keys(): 
       url += '&' + key + '=' + params[key] 
      headers = {'Content-Type': 'application/json', \ 
         'Accept': 'application/json'} 
      content = requests.delete(url, data=json.dumps(json_body), \ 
            headers=headers).content 
     else: 
      raise Exception('method: ' + method + ' not supported.') 
     response = json.loads(content.decode('utf-8')) 
     return response 

    # 
    # Set attributes to dictionary values. 
    # - e.g. 
    # >>> tmdb = TMDB() 
    # >>> movie = tmdb.Movie(103332) 
    # >>> response = movie.info() 
    # >>> movie.title # instead of response['title'] 

    class Movies: 
     """ """ 
     def __init__(self, id=0): 
      self.id = id 

     # optional parameters: language 
     def info(self, params={}): 
      path = 'movie' + '/' + str(self.id) 
      response = TMDB._request('GET', path, params) 
      TMDB._set_attrs_to_values(self, response) 
      return response 

包装罐在这里找到https://github.com/celiao/tmdbsimple 我只是想要按照在那里找到的例子。

任何帮助将是伟大的!

回答

1

由于在Github上建议由@qazwsxpawel,@staticmethod装饰已被添加到TMDB类方法_request和_set_attrs_to_values。如果你升级你的tmdbsimple版本,这个例子现在应该在Python 2.7中工作。 https://pypi.python.org/pypi/tmdbsimple

+0

完美,谢谢! –

0

它可能与您的方法_request的缩进有关。试试这个代码:

class TMDB: 
    def __init__(self, api_key, version=3): 
     TMDB.api_key = str(api_key) 
     TMDB.url = 'https://api.themoviedb.org' + '/' + str(version) 

    def _request(method, path, params={}, json_body={}): 
     url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key 

     if method == 'GET': 
      headers = {'Accept': 'application/json'} 
      content = requests.get(url, params=params, headers=headers).content 
     elif method == 'POST': 
      for key in params.keys(): 
       url += '&' + key + '=' + params[key] 
      headers = {'Content-Type': 'application/json', \ 
       'Accept': 'application/json'} 
      content = requests.post(url, data=json.dumps(json_body), \ 
       headers=headers).content 
     elif method == 'DELETE': 
      for key in params.keys(): 
       url += '&' + key + '=' + params[key] 
      headers = {'Content-Type': 'application/json', \ 
       'Accept': 'application/json'} 
      content = requests.delete(url, data=json.dumps(json_body), \ 
       headers=headers).content 
     else: 
      raise Exception('method: ' + method + ' not supported.') 
     response = json.loads(content.decode('utf-8')) 
     return response 

    # 
    # Set attributes to dictionary values. 
    # - e.g. 
    # >>> tmdb = TMDB() 
    # >>> movie = tmdb.Movie(103332) 
    # >>> response = movie.info() 
    # >>> movie.title # instead of response['title'] 

    class Movies: 
     """ """ 
     def __init__(self, id=0): 
      self.id = id 

     # optional parameters: language 
     def info(self, params={}): 
      path = 'movie' + '/' + str(self.id) 
      response = TMDB._request('GET', path, params) 
      TMDB._set_attrs_to_values(self, response) 
       return response 

this post这可以解释为什么单下划线开头当您使用from foo import *语法

+0

不幸的是,这并没有改变错误。 –

+0

我认为这可能是它,但它没有区别。我用make_request改变了_request的所有出现,并且仍然有同样的错误。 –

0

这看起来像一个简单的拼写错误的方法是不被导入。变化:

def _request(method, path, params={}, json_body={}): 

要这样:

def _request(self, method, path, params={}, json_body={}): 
+0

谢谢,但这不会改变错误。 –

相关问题