Simulate internet TV – Python tutorial

Simulate internet TV – Python tutorial


Hi everyone, welcome to the Python tutorial. In this
tutorial, I am going to simulate an internet TV. So I have to create two class
first. One is called the network … NetworkDevice, and the second one is called the Television
class. And I will create some methods for each of the classes. And then I will derive the InternetTV class from the above classes And I will also add some new methods pertaining
to InternetTV only. First we need to create a class for the NetworkDevice. I will create
an initializer first. mac_number and the cache_size are 2 variables required to initialize a NetworkDevice object I simply create two instance variables for
the NetworkDevice object mac_number here and cache size are the two instance variables
of an … of a NetworkDevice object. So these two variables would be having a value equal to
the variables passed to the initializer Then I can create a method for a network device
I can tell the network device to do data transmission I would request a download
if I want to tell the network device to transmit data. And that network device is also able
to receive data We have variable to pass to the receive
method If the data required to receive
this larger than cache size of the network device, we have to do … do something We stopped the receipt of data because the packet
size is too large We will return False as well. That means that this receive method will have a return value, which is a boolean variable type Else, we do something. We will return
True as the value of this receive method Ok, this completes the NetworkDevice class.
We can also create the Television class similarly You have to set up the initializer first A television has a screen as well as a volume tuner.
Here, self refers to the Television object. When we create a object later, this object
will be passed automatically for us to the initializer. So it is not a variable that
we need to handle when we really call the class to generate an object. We will see the object
instantiation process later. OK, I have two instance variables for a Television object
called screen and volume tuner. And these two instance variables have the value obtained
from the initializer. A television can display video. How can I do that? And when I want
to display a video with a television, I would print this statement out. Display… displaying video
on a certain screen provided by the initializer of the Television object. A television
can also change its volume. That means I can make it a sound produced by the television
louder or quieter If the change_volume parameter is less than
0, I would do something like this let me say I want to reduce the sound by some amount OK. If the amount is negative, I would print a
statement to tell the user that the sound will be reduced by a certain unit. Since I want to make the sentence a bit more natural, I will turn the negative amount
received from the change_volume method to positive by adding a negative a sign before
the amount variable. So if it is … if the amount is negative, this part will become positive If the amount is larger than 0, I would print a
similar statement But this time I don’t need a negative
sign here because the amount received from the user … change_volume method is positive
already. OK. From these two classes NetworkDevice and Television, I can create an internet
TV which is a combination of a network device and a television. So how can I do that? Here is the class declaration. I need to put the
Television class and NetworkDevice class in the parameter list of the InternetTv class.
And then I can set up the initializer for InternetTv An InternetTv would have all the components
from the Television and from the NetworkDevice In addition, an InternetTv has a central
processing unit. So I have to add a new parameter to the initializer of the InternetTv class.
Here, cpu is a new instance variable for the InternetTv object OK. InternetTv has some components similar to the NetworkDevice. So I can simply call the NetworkDevice initializer to help me instantiate an InternetTv
object. So the syntax is like this NetworkDevice.__init__ And then I will pass the mac_number and cache_size to the initializer
of the NetworkDevice. These two variables are actually the initializer of the NetworkDevice
shown on the above code. So when I pass these two values to the NetworkDevice initializer,
I will simply do these two lines for me, OK? Same idea for the Television component
of an InternetTv. If I want to add some television components to an InternetTv, I use the
Television initializer. And you can check that a Television class has two parameters for the initializer. When I use the Television initializer, I simply put these
two parameters to the initializer part So altogether an InternetTv has 5 instance variables. And for a Television, I can have some particular method to it. We can watch
some TV programs from the network directly I simply call the NetworkDevice.transmit method to do the computer connection OK. When the transmission completes, the network
TV is going to receive the data from the remote site. So we can use the NetworkDevice.receive
method to do the receipt of data. Of course we know that this method will return a boolean
value True or False. So we can use a variable is_received here to store the result coming
from the NetworkDevice.receives method If is_received is True, we do something like
this We display the video by calling the Television.display_video method from the Television class OK. Let us instantiate an InternetTv here. a_tv is an InternetTv. It has a number of components. So we have five arguments
of an InternetTv. So when we pass these five arguments to the initializer of an InternetTv,
all these three roles will be executed OK. I can tell the a_tv to watch Network TV
by passing the argument value of one. So the data size is one unit. And then I can tell the a_tv to
change the volume. The argument is minus 1 So I want to reduce the volume.
And then I will ask the a_tv object to watch the network TV again. For this time, the data
size is 3 units. So let us try to run the code OK. For the first time, when we call the watch_network_tv method, the a_tv will request a download. And it is going to receive some data packets. And it can tell is that the screen is LED screen and it is going to reduce the
sound by one unit. And the second call of the watch_network_tv method is doing
some similar thing. It will request a download first. But the receipt of data will be aborted because the packets size is too large. So we can’t put 3 units of data to the internet TV because the cache is only 2 units of memory so the “receipt aborted” prompt will be
shown when we put 3 as the argument, but not for the first call which is only having one as
the argument My implementation is intuitive and straightforward.
However,it may not be the most efficient solution to the problem. If you can think about any
idea to improve my code, feel free to post your code on the comment section below the
video. If you like this video, please give it a like and please subscribe to my channel. Thank you
for watching

Leave a Reply

Your email address will not be published. Required fields are marked *