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.

41 lines
1.3 KiB

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