Computer design
Previous: Coding against databases | Next: Queueing System |
Material for the lesson
Video: Computer architecture and design
Video: Python IO performance & file buffers
Powerpoint: Computer design
Video: Exercises
Note that the videos are no longer completely aligned with the powerpoints due to a shift in focus.
Exercises
1) Measuring IO speed
Create four python scripts which copies the file human.fsa to your own home, using the four different methods mentioned in the powerpoint. Time the programs using ’time’, e.g.
time python mycopy1.py
Also time the simple Unix cp doing the same task. Delete your copy afterwards.
Rank the programs according to performance and explain the difference in execution time.
This is actually a bit hard to do correctly. Considering what you learned about file buffering, then reading the same file twice (or more) in a row will use the file buffers instead of real IO the second time. This means that the first time is always slowest, no matter how efficient the different programs are. You can't even be sure if another student is reading the same file at the same time and hence "disturbs" your measurements.
A way around this problem is simply to use a computer with so little memory, that the file buffer system does not come into play. That is your laptop.
Try to run the scripts on different machines. As minimum you have your laptop and pupil1. DTU's HPC system is also an option.
2) The Chat Board
This is a fun cooperative exercise with databases. You should all make your own Chat client, where you can communicate with the other course participants. The exercise utilizes the concurrent features of databases - the easy update without worry if somebody else is accessing the database at the same time.
You use a database called chatboard that is common to you all. In that database there will be just one table, messages with the fields;
id primary key auto_increment mesg varchar(500) name varchar(15) default NULL channel varchar(12) default 'common' stamp timestamp default CURRENT_TIMESTAMP
The last field, stamp, is automatically updated to the current time when the message is inserted. You only have to insert mesg, name and channel. You can only read the table and insert new rows in the table - that is - create new messages. Only the moderator can delete messages.
mesg is the message on the chat.
name is name of the message sender. If NULL, then it is anonymous.
channel is the channel for this message. Ask google if you don't know what a chat channel is. If NULL, it is the common channel.
stamp is when the message is created. This is used for selecting the newest messages and deleting the old.
The Chat client you make should have the following structure.
connect to database chatboard pull last messages and show get a one-line input while input is not quit: if input is command1: do commmand1 code elif input is command2: do command2 code elif input is something: input was a message, so post it to the chat board pull chat board for new messages and display them if any get next online input close database connection
As you hopefully can see, this structure makes it easy to start small with simple things and build up with new expanded functionality.
A command is an input where the first word starts with slash possibly followed by a parameter. Anything else is just a message to be posted on the selected channel.
List of commands:
/quit Quits the program, possibly leaving a message. /channel <channelname> Selects/creates the channel for your messages. /showchannels Shows possible channels. /trueid Sets your name to your login name. /nick <nickname> Sets your name to <nickname>. /activity Which channels had the latest messages, and when. /last <X> Pull last X messages from the chosen channel and display.
You can implement your own commands, like /search <name>, which would search for messages by the person. /who, which would show who has messages. /dance, which displays a message "<yourname> is dancing for you". You dream it up - it is your chat client and the more powerful and funny it is, the better chances of you being the next Mark Zuckerberg.
When you have done your ChatClient you have accumulated enough experience to create a ChatBot. What should the bot do?
Your dream, pal, your vision.
It could behave like a dog; search for a random or specific user, and bark at them or pee on them.
It could find the latest message and add 'That is what she said yesterday'.
It could be more clever and consult external AI and generate a one-line answer/comment.
It could find the quote of the day and post on the common channel.
It could camp on the 'Dreams' channel and say to every new poster: "Tell me about your dreams!"
It could copy other posters - simply repeating their message.
It could advertise an event across all channels.
It could have a list of people's birthdays, and when the day came about, post "Today is X's birthday" on the common channel.
If anybody posted a calculation, it could compute the result and post it - just like google.
I think that was enough ideas for bots.
The fun part of this exercise is you can use the Chat Board for the rest of the course, once you made the client. You can even develop your client and/or bot more in the progress of the course. Once you get the the SSH tunneling set up, you just run your client from home/your laptop. How can it get any better?