47 lines
5.3 KiB
HTML
47 lines
5.3 KiB
HTML
|
<p>For a long time I have been using Clementine music player on my workstation. Recently I reinstalled Gentoo on my desktop and I wanted to avoid installing QT libraries of any sort.
|
|||
|
So I switched to <a href="https://www.musicpd.org/">mpd</a> and I have fallen in love with it. It is very flexible, fast and enriched by a lot of community software.
|
|||
|
For some weeks I used mpc client as my primary client for mpd but I was not satisfied with it. Even though it is pretty minimal but packed with every feature mpd permits, the search feels uncomfortable because is case sensitive and need artist, album, etc. flags before any entry.
|
|||
|
This is why I have written kpd together with Francesco Gallà</p>
|
|||
|
|
|||
|
<h2 id="kyuss-player-client">Kyuss Player Client</h2>
|
|||
|
<p>kpd is an acronym for Kyuss Player Client because we have been listening only to <a href="https://en.wikipedia.org/wiki/Kyuss">Kyuss</a> while programming this client.
|
|||
|
We have reimplemented the search functions to suit our habits. No more case sensitive, optional ‘artist, album, title’ flags.
|
|||
|
kpd accepts only one string as the search argument and implements optional filter arguments to narrow the search in a grep like way.
|
|||
|
I welcome you to read the <a href="http://francescomecca.eu:3000/pesceWanda/kpd">readme</a> in my git to understand how the search works.
|
|||
|
Anyway in this post I want to explain bits of the code.</p>
|
|||
|
|
|||
|
<h3 id="main">Main</h3>
|
|||
|
<p>The main kpd file invoked when the command is run in the console is kpd.py
|
|||
|
The most interesting part in this file IMHO is these lines:</p>
|
|||
|
|
|||
|
<figure class="highlight"><pre><code class="language-python" data-lang="python">
|
|||
|
<span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">argsOrder</span><span class="p">:</span>
|
|||
|
<span class="k">if</span> <span class="n">dictArgs</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">!=</span> <span class="bp">False</span><span class="p">:</span>
|
|||
|
<span class="n">client</span><span class="o">.</span><span class="n">update_status</span> <span class="p">()</span>
|
|||
|
<span class="n">methodToCall</span> <span class="o">=</span> <span class="nb">getattr</span> <span class="p">(</span><span class="n">util</span><span class="p">,</span> <span class="n">el</span><span class="p">)</span>
|
|||
|
<span class="n">retUtil</span> <span class="o">=</span> <span class="n">methodToCall</span> <span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">dictArgs</span><span class="p">[</span><span class="n">el</span><span class="p">],</span> <span class="n">searchRes</span><span class="p">)</span></code></pre></figure>
|
|||
|
|
|||
|
<p>argsOrder is a list of the arguments on the command line in the order the user wrote them.
|
|||
|
kpd uses a dictionary to store for every argument the corrispective string for the function that will be invoked using getattr.
|
|||
|
In this way any argument can be added to the main file without writing any other line of code. WE used this method to avoid using switch alike solutions.</p>
|
|||
|
|
|||
|
<h3 id="util">Util</h3>
|
|||
|
<p>The util.py source file is a pretty easy source file to read. It contains every function that can be invoked by command line arguments. Every function has the same ‘prototypes’ so that they can be called using the method explained above.
|
|||
|
To implement <code class="highlighter-rouge">no-output</code> and <code class="highlighter-rouge">output</code> function I have used a class:
|
|||
|
to suppress the output on the console the program assign to <em>sys.stdout</em> a dummy class that save the original stdout on a variable and replaces write and flush functions so that they are just pass. and no output is written.
|
|||
|
To permit output after suppression the program just reassing the original value to sys.stdout.</p>
|
|||
|
|
|||
|
<h3 id="database-search">Database Search</h3>
|
|||
|
<p>In MPDdatabase.py we have written the search functions.
|
|||
|
Originally we intended to just read and import in a dictionary the whole mpd database that is stored compressed in the home directory.
|
|||
|
This list of dictionaries stores every entry related to the song and if any of them matches the search string or the filter string (considering also flags if any) the related song is printed on the output and saved in a list so it can be added by the add function.
|
|||
|
This approach result very efficent in term of precision but it lacked speed. For a database of about 77 thousand songs (about 550k lines) a search query could last almost 2 seconds.
|
|||
|
To improve the speed of the search we used the pickle module. The pickle module allows kpd to dump the data structure used to store the database in memory on a file that can be read easily by using the <code class="highlighter-rouge">pickle.load</code> function.
|
|||
|
In this way the search lasts about 40 milliseconds on the same database that wastes about 16MiB of memory on disk.</p>
|
|||
|
|
|||
|
<h2 id="conclusion">Conclusion</h2>
|
|||
|
<p>This was really fun. It was our first hand on python project and the first real program we have written since we started learning programming at our university.
|
|||
|
I discovered that programming allows me to relax and that is really cool to have custom software for activities you do every day.
|
|||
|
The source for our program is stored in my git <a href="http://francescomecca.eu:3000/pesceWanda/kpd">here</a> and you are free to modify it.</p>
|
|||
|
|