python - 导入本地python文件时出现Attritube错误
问题描述
我需要一些帮助来解决问题。下面的代码试图导入一个名为 game_getter.py 的文件来访问它的 all_games 字典变量。
from django.db import models
from catolog import game_getter
# Create your models here.
class Game(models.Model):
url_clue = ["//images.igdb.com"]
for game in game_getter.all_games:
title = game_getter.all_games[game][0]
if url_clue in game_getter.all_games[game][1]:
cover_art = game_getter.all_games[game]
else:
pass
if game_getter.all_games[game][2] == None:
pass
else:
storyline = game_getter.all_games[game][2]
if game_getter.all_games[game][3] == None:
pass
else:
storyline = game_getter.all_games[game][3]
genre_pac = game_getter.all_games[game][4]
def __str__(self):
return self.title
class UserSearch():
user_input = str
在下一个会话的底部,我使用了字典对象 all_games 的返回值。我什至尝试将其设为全局变量,但计算机仍然看不到它。
# Create a search
def search_query(user_input, exp_time):
# Generate token if needed
generate_token(exp_time)
#establish client_id for wrapper
client_id = "not my client_id"
wrapper = IGDBWrapper(client_id, token)
# Query the API based on a search term.
query = wrapper.api_request(
'games', # Requesting the name, storyline, genre name, and cover art url where the user input is the search tearm
f'fields name, storyline, genres.slug, cover.url; offset 0; where name="{user_input}"*; sort first_release_date; limit: 100;',
# Also sorted by release date with a limit of 100 results
)
# Load the binary data into json
message_json = json.loads(query)
# List of all games returned
global all_games
all_games = dict()
key = 0
# Grab each value by key and separate per game
for game in message_json:
name = game.get('name')
cover_url = game.get('cover')
storyline = game.get('storyline')
summary = game.get('summary')
genre_set = game.get('genres')
# Genre posses none to many tags which needs to be sorted.
genre_list = []
if genre_set:
for type in genre_set:
for i in type:
genre_list.append(type[i])
for i in genre_list:
genre_list = [x for x in genre_list if not isinstance(x, int)]
else:
pass
# Group together by game
if game.get('cover') != None:
result = [name,cover_url.get('url'),storyline,summary,genre_list]
# Add the game to the collection of all games found
all_games[key] = result
key += 1
else:
result = [name,storyline,summary,genre_list]
# Add the game to the collection of all games found
all_games[key] = result
key += 1
return all_games
我错过了什么?
解决方案
推荐阅读
- docker - 获取 html 响应而不是 json rasa x 服务器模式(docker)
- .net-core - 大众运输中发送功能的底层
- javascript - 如何使用 Express 服务器返回请求的 Angular GET 格式?
- javascript - 性能:Html5 Canvas / Javascript - 使用 canvas.drawImage 与 canvas.style 移动背景图像
- javascript - 我应该在 Angular 的可重用组件中使用 EventEmitter 还是共享服务?
- ios - 为什么 UITableView 内容偏移量为-20.0?
- bluetooth-lowenergy - 未使用 Tmds.Dbus 库获取 BLE 客户端连接通知
- asp.net - 请求超时。Web 服务器未能在指定时间内响应
- git - Jenkins master 如何与我在 Kubernetes 中的自定义容器通信?
- pivot-table - SSAS 查找日期小于或等于选定日期的数据