コード例 #1
0
def test_image_link():
    movie = Movie("Sharknado", 2020)
    movie.image_link = "test"
    assert movie.image_link == "test"

    movie.image_link = 2
    assert movie.image_link == None
コード例 #2
0
def load_movie_file(repository: MemoryRepository, filename: str):
    with open(filename, mode="r", encoding="utf-8-sig") as csvfile:
        reader = csv.DictReader(csvfile)

        directors = dict()
        genres = dict()
        actors = dict()

        for row in reader:
            # Create and populate Movie object
            movie = Movie(
                title=row["Title"].strip(),
                release_year=int(row["Year"])
            )
            movie.description = row["Description"].strip()
            movie.runtime_minutes = int(row["Runtime (Minutes)"])
            movie.rating = float(row["Rating"])
            movie.votes = int(row["Votes"])
            movie.revenue_in_millions = row["Revenue (Millions)"]
            movie.metascore = row["Metascore"]
            if "Image URL" in row:
                movie.image_link = row["Image URL"]

            # Add bidirectional link between director and movie
            director_name=row["Director"].strip()
            try:
                director = directors[director_name]
            except KeyError:
                director = Director(director_full_name=director_name)
                directors[director_name] = director
            make_movie_director_association(movie, director)

            # Add bidirectional link between movie and genres
            for name in row["Genre"].split(","):
                genre_name = name.strip()
                try:
                    genre = genres[genre_name]
                except KeyError:
                    genre = Genre(genre_name=genre_name)
                    genres[genre_name] = genre
                make_movie_genre_association(movie, genre)

            # Add bidirectional link between actors and movies
            for name in row["Actors"].split(","):
                actor_name = name.strip()
                try:
                    actor = actors[actor_name]
                except KeyError:
                    actor = Actor(actor_full_name=actor_name)
                    actors[actor_name] = actor
                make_movie_actor_association(movie, actor)

            # Add movie object to repository
            repository.add_movie(movie)
        
        # Add directors to repository
        for director in directors.values():
            repository.add_director(director)
        
        # Add genres to repository
        for genre in genres.values():
            repository.add_genre(genre)

        # Add actors to repository
        for actor in actors.values():
            repository.add_actor(actor)