Computer design

From 22112
Jump to navigation Jump to search
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

Motto: Less fear - more fun

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 when inserting a row. 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. You are to make a python program that runs in the terminal and interacts with you (your commands). If confused, start with making a program that simply pulls all messages from the database and show them. Build up with selecting a channel to show, then the ability to post a message.
Something that I found to be a challenge was to figure out when to pull new messages from the database and which messages. If you are overwhelmed, then just auto pull the last 5-10 messages every time - but you get a lot redundant information that way. You could rely on the /last command - you command the pull yourself. I made a pull for only new messages when I pressed <enter> without any command/message.

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: This is minimum useful functionality/requirement.

/quit                   Quits the program, possibly leaving a message.
/channel <channelname>  Selects/creates the channel for your messages.
/showchannels           Shows possible channels.
/nick <nickname>        Sets your name to <nickname>.
/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", /trueid, which sets your name to your login name, /activity, which shows when the channels had the latest messages.
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.

Hint: Something I found useful is that you can get the id, i.e. the auto incremented primary key of you last inserted message like this:

cursor.execute("""INSERT INTO messages SET mesg=%s, name=%s, channel=%s""", (chat, nick, channel))
msgID = cursor.lastrowid

Extra optional fun: ChatBots
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.

  • behave like a dog; search for a random or specific user, and bark at them or pee on them.
  • find the latest message and add 'That is what she said yesterday'.
  • be more clever and consult external AI and generate a one-line answer/comment.
  • find the quote of the day and post on the common channel.
  • camp on the 'Dreams' channel and say to every new poster: "Tell me about your dreams!"
  • copy other posters - simply repeating their message.
  • advertise an event across all channels.
  • 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?