francescomecca.eu/output/index-13.html

140 lines
9.6 KiB
HTML
Raw Normal View History

2018-11-10 18:19:00 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Caught in the Net (old posts, page 13) | Caught in the Net</title>
2018-11-10 18:32:04 +01:00
<link rel="stylesheet" href="assets/blog/fonts/opensans.css">
<link href="assets/blog/css/normalize.css" rel="stylesheet" type="text/css">
<link href="assets/blog/css/cayman.css" rel="stylesheet" type="text/css">
2018-11-10 18:19:00 +01:00
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="en" href="rss.xml">
<link rel="canonical" href="francescomecca.eu/index-13.html">
<link rel="prev" href="index-14.html" type="text/html">
<link rel="next" href="index-12.html" type="text/html">
</head>
<body>
<div id="container">
<section class="page-header"><h1 class="project-name">
Caught in the Net
</h1>
<h2 class="project-tagline">La rete ti cattura ma libera il pensiero</h2>
<a class="btn" href=".">Home</a>
<a class="btn" href="pages/about/">About me</a>
<a class="btn" href="pages/contattami/">Contact me</a>
<a class="btn" href="archiveall.html">Archive</a>
<a class="btn" href="rss.xml">RSS</a>
2024-03-06 09:18:36 +01:00
<a class="btn" href="https://bugs.lezzo.org">Personal Git</a>
2024-03-06 09:16:06 +01:00
<a class="btn" href="https://bugs.lezzo.org/bparodi/Curriculum_vitae/raw/branch/master/latex.dir/francesco_mecca_cv_eng.pdf">Curriculum</a>
2018-11-10 18:19:00 +01:00
</section><section class="main-content"><div class="posts">
<article class="post"><header><h1 class="post-title"><a href="blog/2016/4/17/kpd-player/" class="u-url">Kyuss Music Player</a></h1>
</header><div>
<span class="post-date">17 April 2016</span>
</div>
<br><div class="e-content entry-content">
<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>
<h3>Kyuss Player Client</h3>
<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>
<h4>Main</h4>
<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>
<p>.. code:: python</p>
2024-02-28 14:38:55 +01:00
<div class="code"><pre class="code literal-block"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">el</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nl">argsOrder</span><span class="p">:</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">dictArgs</span><span class="o">[</span><span class="n">el</span><span class="o">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="k">False</span><span class="err">:</span>
<span class="w"> </span><span class="n">client</span><span class="p">.</span><span class="n">update_status</span><span class="w"> </span><span class="p">()</span>
<span class="w"> </span><span class="n">methodToCall</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">getattr</span><span class="w"> </span><span class="p">(</span><span class="n">util</span><span class="p">,</span><span class="w"> </span><span class="n">el</span><span class="p">)</span>
<span class="w"> </span><span class="n">retUtil</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">methodToCall</span><span class="w"> </span><span class="p">(</span><span class="n">client</span><span class="p">,</span><span class="w"> </span><span class="n">dictArgs</span><span class="o">[</span><span class="n">el</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">searchRes</span><span class="p">)</span>
</pre></div>
2018-11-10 18:19:00 +01:00
<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>
<h4>Util</h4>
<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>no-output</code> and <code>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>
<h4>Database Search</h4>
<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>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>
<h3>Conclusion</h3>
<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>
</div>
</article><br><hr>
<br><article class="post"><header><h1 class="post-title"><a href="blog/2016/4/10/short-lesson-from-reddit/" class="u-url">Bright Father</a></h1>
</header><div>
<span class="post-date">10 April 2016</span>
</div>
<br><div class="e-content entry-content">
<blockquote>
<p>My father used to tell us ridiculous false information all the time. The catch was if we could catch one out and prove him wrong he'd give us a dollar. As we got older it would got a little less outrageous, but we'd still get that dollar if we could prove it. Looking back it was a good way to get us to think for ourselves.</p>
</blockquote>
<p><a href="https://www.reddit.com/user/zedoriah">zedoria</a> on <a href="https://www.reddit.com/r/AskReddit/comments/u1ili/what_did_school_teach_you_that_was_blatantly/?ref=search_posts">reddit</a></p>
</div>
</article><br><hr>
<br><article class="post"><header><h1 class="post-title"><a href="blog/2016/4/10/lifehacks/" class="u-url">Lifehacks</a></h1>
</header><div>
<span class="post-date">10 April 2016</span>
</div>
<br><div class="e-content entry-content">
<ul>
<li>
<p>Even though you may be nervous about talking to random people, the worst you can get is "Go away".</p>
</li>
<li>
<p>Do not buy your girlfriend or wife flowers in an attempt to make nice after you pissed her off. Every
time she looks at the flowers, she will just be reminded that you pissed her off, unless she has the
memory span of a goldfish.</p>
</li>
<li>
<p>To keep lettuce fresh for days longer, wrap it in paper towels instead of inside a plastic bag, it
works very well.</p>
</li>
<li>
<p>Cubes of sugar in biscuit barrels help the biscuits stay crisp.</p>
</li>
<li>
<p>To clear your sinuses, eat a lot of wasabi. It will hurt tons, but your sinuses clear almost
instantaneously.</p>
</li>
</ul>
</div>
</article><br><hr>
<br>
</div>
<div class="pagination">
<a href="index-14.html" rel="prev"></a>
<a class="pagination-item newer" href="index-14.html">Newer
</a><a class="pagination-item older" href="index-12.html">Older</a>
</div>
2023-08-30 18:10:14 +02:00
2018-11-10 18:19:00 +01:00
<footer class="site-footer" id="footer"><span> CC BY-SA 4.0 International.<br></span>
2018-11-10 18:19:11 +01:00
<span class="site-footer-credits"><a href="https://getnikola.com">Nikola</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
2018-11-10 18:19:00 +01:00
</footer></section>
</div>
</body>
</html>