You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

  1. # Copyright 2015 Adafruit Industries.
  2. # Author: Tony DiCola
  3. # License: GNU GPLv2, see LICENSE.txt
  4. import random
  5. class Playlist(object):
  6. """Representation of a playlist of movies."""
  7. def __init__(self, movies, is_random):
  8. """Create a playlist from the provided list of movies."""
  9. self._movies = movies
  10. self._index = None
  11. self._is_random = is_random
  12. def get_next(self):
  13. """Get the next movie in the playlist. Will loop to start of playlist
  14. after reaching end.
  15. """
  16. # Check if no movies are in the playlist and return nothing.
  17. if len(self._movies) == 0:
  18. return None
  19. # Start Random movie
  20. if self._is_random:
  21. self._index = random.randrange(0, len(self._movies))
  22. else:
  23. # Start at the first movie and increment through them in order.
  24. if self._index is None:
  25. self._index = 0
  26. else:
  27. self._index += 1
  28. # Wrap around to the start after finishing.
  29. if self._index >= len(self._movies):
  30. self._index = 0
  31. return self._movies[self._index]
  32. def length(self):
  33. """Return the number of movies in the playlist."""
  34. return len(self._movies)