C4DMMar2013Exercise » History » Version 2

Chris Cannam, 2013-03-16 10:41 AM

1 1 Chris Cannam
h1. Python exercise, C4DM Software Carpentry Mar 2013
2 1 Chris Cannam
3 2 Chris Cannam
h2. Task                           
4 1 Chris Cannam
                                             
5 2 Chris Cannam
Write a program that allows a user to input a source playlist (in the same format as the example playlist attached below), a destination file, a minimum and maximum song length in the format minutes:seconds (i.e. 3:30) and a genre.
6 1 Chris Cannam
7 2 Chris Cannam
The program must write to the destination file a new playlist (in the same format as the original) of all songs in the provided playlist that fit the song length criteria and are in the required genre.
8 1 Chris Cannam
9 2 Chris Cannam
For example, the python program would be run as:
10 1 Chris Cannam
11 2 Chris Cannam
@getSongs.py fullPlaylist.txt newPlaylist.txt 1:00 2:00 Rock@
12 1 Chris Cannam
13 2 Chris Cannam
This would write a file called @newPlaylist.txt@ containing a list of all rock songs from @fullPlaylist.txt@ that are between 1 and 2 minutes long.
14 2 Chris Cannam
15 2 Chris Cannam
h3. Example file and implementation details
16 2 Chris Cannam
17 2 Chris Cannam
A large playlist containing randomly generated band and song names is attached below, which should be used to test the program.
18 2 Chris Cannam
19 1 Chris Cannam
You should use functions to achieve the task, and ensure that the program is fail-safe and handles user input errors gracefully.
20 1 Chris Cannam
21 2 Chris Cannam
This should be achievable with the concepts covered in the Introduction To Python workshop, but pay extra attention to how you parse each line and separate/split it by the various characters. You may need to separate entries more than once.
22 1 Chris Cannam
23 2 Chris Cannam
Recall also that strings can be concatenated simply by adding them:
24 2 Chris Cannam
25 2 Chris Cannam
<pre>
26 1 Chris Cannam
>>>print 'text1' + 'text2'
27 1 Chris Cannam
>>>'text1text2'
28 2 Chris Cannam
</pre>
29 1 Chris Cannam
30 1 Chris Cannam
One extra thing that is worth mentioning here is how to write to files rather than read from them. Recall the procedure for opening and reading a file:
31 1 Chris Cannam
32 2 Chris Cannam
<pre>
33 1 Chris Cannam
source = open('file.txt', 'r')
34 2 Chris Cannam
</pre>
35 1 Chris Cannam
36 1 Chris Cannam
We simply need to replace the 'r' flag (which stands for 'read') with 'w'
37 1 Chris Cannam
38 2 Chris Cannam
<pre>
39 1 Chris Cannam
writer = open('newfile.txt', 'w')
40 2 Chris Cannam
</pre>
41 1 Chris Cannam
42 1 Chris Cannam
To write something to the file we just call:
43 1 Chris Cannam
44 2 Chris Cannam
<pre>
45 1 Chris Cannam
writer.write('text to be written')
46 2 Chris Cannam
</pre>
47 1 Chris Cannam
48 1 Chris Cannam
If we want to write something and then go onto a new line, we must add \n to the end of the string.
49 1 Chris Cannam
50 2 Chris Cannam
<pre>
51 1 Chris Cannam
writer.write('text to be written\n') 
52 2 Chris Cannam
</pre>
53 1 Chris Cannam
54 1 Chris Cannam
Have fun!