Python_Day02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

C2 - Python

C-COD-240

Day 02
First project

1.4
Day 02
delivery method: pythond02 on Github

• The totality of your source files, except all useless files (binary, temp files, obj
files,...), must be included in your delivery.

• Error messages have to be written on the error output, and the program should
then exit with the 84 error code (0 if there is no error).

1
EXERCISE 01 (2PTS)
File to hand in: python_d02/time_01/display_time.py

Create a function “display_time”. It takes as argument a date in epoch format (in seconds since 01/01/1970
00:00).

It displays the paramenter as GMT following the example:


∇ Terminal - + x
∼/C-COD-240> python3
»> from display_time import display_time
# The 27/04/2017 at 5:10 pm
»> display_time(1493313015)
17:10:15, Thursday, 4* day of the 17* week of 2017, April
# The 24/02/1993 at 8 am
»> display_time(730540800)
08:00:00, Wednesday, 3* day of the 08* week of 1993, February

Convert dates online with this link

2
EXERCISE 02 (3PTS)
File to hand in: python_d02/classes_modules_01/email.py

Create a class “Email”.


It has 7 attributes following the rules below:

• _body: Constructor parameter


• _subject: Constructor parameter
• _from: Constructor parameter
• _to: Constructor parameter
• _created_at: set during initialize at the current Time
• _updated_at: set during initialize at the current Time
• _sent_at: set during initialize at None

See also Keyword arguments, and Positionnal parameters.

3
EXERCISE 03 (3PTS)
File to hand in: python_d02/classes_modules_02/email.py,
python_d02/classes_modules_02/sendable.py,
python_d02/classes_modules_02/sms.py

Copy the previous code of Email, but rename the class to “Sendable” and the file to “sendable.py”.
Now, you have to create two files with two classes “Email” and “Sms”.

They inherit from Sendable.


Email and Sms use the Parent constructor.
Email takes the same parameters as before, and Sms only takes “body”, “from”, and “to” parameters.

Furthermore, in Sendable, add a method “send”. It sets the attribute “sent_at” to the current Time, but only
one time.

Calling this method twice must raise an Exception “DataAlreadySent” with no message.

Note: Be sure to import the modules required before using the classes

4
EXERCISE 04 ( 3PTS )
File to hand in: python_d02/classes_modules_03/email.py,
python_d02/classes_modules_03/private.py,
python_d02/classes_modules_03/sendable.py,
python_d02/classes_modules_03/sms.py

Copy the previous code.


From now, each call to “sent” will save the metadata of the Sendable object to a history, global for every
Sendable objects.

These metadata are “from”, “to” and “sent_at”.


To perform this action, you will create a new class attribute in “Sendable”.
It should be an empty dictionary.
When “sent” is successfully called, it has to register the information.

The expected structure is:


>{from => { to => [ sent_at, sent_at, sent_at, . . . ], to => [ sent_at, sent_at, . . . ] . . . }}
Then, add a class method “history” to read it.

Then, create a new class “Private” like “Email”. But when the method “sent” is called, you should not trigger
any spying action, not even change sent_at.

But it still has to raise an error if the message is sent twice. Be smart.

5
EXERCISE 05 (3PTS)
File to hand in: python_d02/class_modules_04/email.py,
python_d02/class_modules_04/sendable.py,
python_d02/class_modules_04/sms.py,
python_d02/class_modules_04/private.py

Copy the previous code.

Create a new method encrypt that takes an optional parameter rotate.


If rotate is not specified, you must use the default value 13.
When this method is called, you must apply a ROT-13 operation on the body (or ROT-N if rotate is specified).

The method returns the encrypted text.


Add a method decrypt to do the reverse operation (same parameter, returns the decrypted text).

6
EXERCISE 06 (3PTS)
File to hand in: python_d02/classes_modules_05/email.py,
python_d02/classes_modules_05/private.py,
python_d02/classes_modules_05/sendable.py,
python_d02/classes_modules_05/sms.py,
python_d02/classes_modules_05/sendable_box.py

Copy the previous code.

Add a class SendableBox.


It has a class attribute “all” to store every SendableBox created.
Each time you create a new SendableBox (with one argument, it’s address), you have to create a new infinite
loop in a thread.
Every seconds, it checks if it receive a new message.
If true, then display “New message from {m.from}” on the first line, and then on a new line the subject or
the body if the first one is None.
It displays one message maximum by second.

SendableBox has an class function “recv”, taking as argument a Sendable and has to distribute it.

Link Sendable with SendableBox when send is called, trigger recv

Then add a 2nd parameter “max_box” to the SendableBox constructor.


When the number of message received by the SendableBox is equal to max_box, the loop in the thread
has to break
At the end, add a 3rd parameter “sleep_duration”. Instead of waiting 1 second between each check, wait
only “sleep_duration” seconds.

7
EXERCISE 07 (3PTS)
File to hand in: python_d02/classes_modules_06/contact.py

In this exercise you will discover the multiple-inheritance capacities of python


Create 3 classes Addressable, Nameable, and HasFriend.

• Addressable has to define an attribute “address”


• Nameable has to define an attribute “name”
• HasFriend must define an attribute “friends” that returns the array of friends of the class that includes
them.

If the attributes friends doesn’t exist, it has to create it.


Create a class Contact which will inherit from the 3 aforementioned classes.

You might also like