Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clean and documented. solution in Clear category for Voice TV Control by Celshade
from typing import Union
class VoiceCommand(object):
"""Establish voice commands for a tv remote.
Attributes:
channels: The available tv channels to select from.
Public Methods:
first_channel(): Turns to the first channel.
last_channel(): Turns to the last channel.
turn_channel(): Turns to the the given channel.
next_channel(): Turns to the next channel.
prevous_channel(): Turns to the previous channel.
current_channel(): Gives the name of the current channel.
is_exist(): Confirms the existence of a channel.
"""
def __init__(self, channels: iter) -> None:
self.channels = channels
self._current = self.channels[0]
def first_channel(self) -> str:
"""Turn to the first channel and return the name."""
self._current = self.channels[0]
return self._current
def last_channel(self) -> str:
"""Turn to the last channel and return the name."""
self._current = self.channels[-1]
return self._current
def turn_channel(self, number: int) -> str:
"""Turn to the specified channel and return the name."""
self._current = self.channels[number - 1]
return self._current
def next_channel(self) -> str:
"""Turn to the next channel and return the name."""
if self._current == self.channels[-1]:
return self.first_channel()
else:
return self.turn_channel(self.channels.index(self._current) + 2)
def previous_channel(self) -> str:
"""Turn to the previous channel and return the name."""
if self._current == self.channels[0]:
return self.last_channel()
else:
return self.turn_channel(self.channels.index(self._current))
def current_channel(self) -> str:
"""Return the name of the current channel."""
return self._current
def is_exist(self, channel: Union[int, str]) -> str:
"""Return True if the channel exists, else False."""
if type(channel) is int:
try:
if self.channels[channel - 1]:
return "Yes"
else:
return "No"
except IndexError:
return "No"
elif type(channel) is str and channel in self.channels:
return "Yes"
else:
return "No"
Jan. 30, 2019