- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习使用 python 编写代码,但我遇到了一个错误:
AttributeError: Movie instance has no attribute 'number_of_seasons'
所以问题很清楚,但我不确定如何解决。基本上我创建了一些类,我想显示一些具有不同属性的电影和系列。
这是我创建类和属性的 media.py 文件:
class Video():
"""This class created for both common info movies and series"""
def __init__(self, movie_title, story_line, url_poster, url_trailer, badge):
self.title = movie_title
self.storyline = story_line
self.poster = url_poster
self.trailer = url_trailer
self.badge = badge
def show_trailer(self):
webbrowser.open(self.trailer)
# here i define the class called Film for all movies
class Movie(Video):
"""This class is used to create all movies"""
def __init__(self, movie_title, story_line, url_poster, url_trailer, badge, movie_duration, movie_actors):
Video.__init__(self, movie_title, story_line, url_poster, url_trailer, badge)
self.duration = movie_duration
self.actors = movie_actors
# here i define the class called Series for all series with episodes and seasons
class Series(Video):
def __init__(self, movie_title, story_line, url_poster, url_trailer, badge, number_of_seasons, number_of_episodes):
Video.__init__(self, movie_title, story_line, url_poster, url_trailer, badge)
self.number_of_seasons = number_of_seasons
self.number_of_episodes = number_of_episodes
然后我有 enterteinment_center.py 文件,其中我只添加了 1 部电影和 1 系列:
import media
import fresh_tomatoes
ironman = media.Movie("Ironman",
"Genius, billionaire, and playboy Tony Stark, who has inherited the defense contractor Stark Industries from his father",
"http://cdn.collider.com/wp-content/uploads/2015/04/iron-man-1-poster.jpg",
"https://www.youtube.com/watch?v=8hYlB38asDY",
"http://i.imgur.com/45WNQmL.png",
"126 minutes",
"English")
games_of_thrones = media.Series("Games Of Thrones",
"The series is generally praised for what is perceived as a sort of medieval realism.",
"https://vignette3.wikia.nocookie.net/gameofthrones/images/2/2c/Season_1_Poster.jpg/revision/latest?cb=20110406150536",
"https://www.youtube.com/watch?v=iGp_N3Ir7Do&t",
"http://i.imgur.com/45WNQmL.png",
"7 Seasons",
"12 episodes")
movies = [ironman, games_of_thrones, ironman, games_of_thrones, ironman, games_of_thrones]
fresh_tomatoes.open_movies_page(movies)
创建 html 的最后一个文件是 fresh_tomatoes.py 但我将只在此处粘贴我认为对修复有用的代码片段:
def create_movie_tiles_content(movies):
# The HTML content for this section of the page
content = ''
for movie in movies:
if isinstance(movie, Movie):
# Extract the youtube ID from the url
youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer)
youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer)
trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
# Append the tile for the movie with its content filled in
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster,
trailer_youtube_id=trailer_youtube_id,
film_badge=movie.badge,
film_description=movie.storyline
)
elif isinstance(movie, Series):
# Extract the youtube ID from the url
youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer)
youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer)
trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
# Append the tile for the movie with its content filled in
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster,
trailer_youtube_id=trailer_youtube_id,
film_badge=movie.badge,
film_description=movie.storyline,
serie_season=movie.number_of_seasons
)
return content
def open_movies_page(movies):
# Create or overwrite the output file
output_file = open('fresh_tomatoes.html', 'w')
# Replace the placeholder for the movie tiles with the actual dynamically generated content
rendered_content = main_page_content.format(movie_tiles=create_movie_tiles_content(movies))
# Output the file
output_file.write(main_page_head + rendered_content)
output_file.close()
# open the output file in the browser
url = os.path.abspath(output_file.name)
webbrowser.open('file://' + url, new=2) # open in a new tab, if possible
所以基本上电影和连续剧有一些不同的属性,所以如果我想显示例如《权力的游戏》的季数,我会收到一条错误消息,告诉我电影《钢铁侠》没有任何称为季数的属性。
希望有人能帮上忙!非常感谢!
最佳答案
您不能使用相同的函数来提取具有不同属性的类的值。
您需要为每个类中的每个属性分配一个默认值,或者更改函数以在循环中检查电影
的类型。
例如:
for movie in movies:
if isinstance(movie, Movie):
# add movie attributes to the content
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster,
trailer_youtube_id=trailer_youtube_id,
film_badge=movie.badge,
film_description=movie.storyline,
serie_seasons='no season'
)
elif isinstance(movie, Series):
# add series attributes to the content
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster,
trailer_youtube_id=trailer_youtube_id,
film_badge=movie.badge,
film_description=movie.storyline,
serie_seasons=movie.number_of_seasons
)
此模式允许您根据媒体/视频的类型更改内容。
您可能需要在第二个文件中导入类型:
from media import Movie, Series
关于python - 属性错误 : Movie instance has no attribute 'number_of_seasons' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844553/
我知道我的问题有点含糊,但我不知道如何描述它。我问过很多地方,但似乎没有人理解我为什么要这样做。但请耐心等待,我会解释为什么我想要这样的东西。 我使用 Liquid Templates 允许用户在我的
这个问题在这里已经有了答案: what is the difference between null != object and object!=null [duplicate] (2 个回答) 7年
当我在我的本地主机 Google App Engine 应用程序中将日志记录级别更改为 FINE 时,我开始在我的跟踪堆栈中看到这些: Apr 17, 2013 4:54:20 PM com.goog
Python 有内置函数 type : class type(object) With one argument, return the type of an object. The return v
我正在使用深度学习进行语义分割,我遇到了以下术语:语义分割、实例检测、对象检测 和对象分割. 它们有什么区别? 最佳答案 这些术语的某些用法对用户而言是主观的或依赖于上下文,但据我所知对这些术语的合理
我面临 -[NSConcreteMutableData release] 的问题:消息发送到已释放的实例,我也附上了我的示例代码。 - (IBAction)uploadImage { NSString
我试图显示模型中的单个实例(数据库行),其中多个实例共享多行的相同字段(列)值。为了澄清这一说法,我有以下情况: ID/Title/Slug/Modified 1 Car A 1s ag
我正在尝试使用mockito来模拟服务。然而,我没有找到一种方法来告诉mockito,给定一个类的实例返回给我相同的实例: 类似于: given(service.add(any(Individua
我知道如何从父类(super class)原型(prototype)创建子类原型(prototype)。但是,如果我已经有了父类(super class)对象的实例来创建子类对象怎么办? 在 JS 中
鉴于 Kotlin 1.1。对于某个类的 instance,instance::class.java 和 instance.javaClass 似乎几乎是等价的: val i = 0 println(
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Find out the instance id from within an ec2 machine 我正在寻找从
为什么我的 Instantiate 函数没有创建 That 的“空白”实例? 我有以下最小类: classdef That < handle properties This = '' end
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTra
考虑以下几点: public class A { public String name = "i am a A instance"; } public class B extends A {
我正在使用 Scalr 来扩展网站服务器。 在 Apache 服务器上,我安装了 Sakai,并为 Linux 机器创建了一个启动脚本。 问题是,如何确保MySQL实例在Apache服务器启动之前启动
Android Realm DB 允许使用 Realm.getInstance() 获取多个实例。这些中的最佳实践是什么? :1.创建单个实例(应用程序范围)并在任何地方使用它2. 在需要时获取一个新
我很难理解为什么修改实例 a 中的属性会修改实例 b 中的相同属性。 var A = function (){ }; A.prototype.data = { value : 0 }; var
我将 Weka 用作更长管道的一部分,因此,我无法承受将所有数据写入文件或数据库只是为了创建一个 Instances。目的。我可以即时做的是创建 Instance 的列表对象。 来自 this pag
class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func = c.func # c.func i
Angular Routing 文档提到了组件实例创建、组件实例激活和路由激活。 文档没有解释这些概念的区别,以及每次创建/激活发生的时间。 问题 实例创建和实例激活有什么区别? 实例激活和路由激活有
我是一名优秀的程序员,十分优秀!