francescomecca.eu/_site/page4/index.html

276 lines
16 KiB
HTML
Raw Normal View History

2016-05-01 11:13:57 +02:00
<!DOCTYPE html>
<html lang="en-us">
2017-05-09 11:29:54 +02:00
2016-05-01 11:13:57 +02:00
<head>
2017-05-09 11:29:54 +02:00
<meta charset="UTF-8">
<title>Caught in the Net</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#157878">
<link rel="stylesheet" href="/css/normalize.css">
<!--<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>-->
<link rel="stylesheet" href="/fonts/opensans.css">
<link rel="stylesheet" href="/css/cayman.css">
2016-05-01 11:13:57 +02:00
</head>
2017-05-09 11:29:54 +02:00
<body>
<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>
2017-05-09 12:17:48 +02:00
<a class="btn" href="/about/">About me</a>
2017-05-09 11:29:54 +02:00
<a class="btn" href="/contattami/">Contact me</a>
2017-05-10 20:26:52 +02:00
<a class="btn" href="/archive/">Archive</a>
<a class="btn" href="/feed.xml">RSS</a>
2017-09-22 14:07:26 +02:00
<a class="btn" href="http://francescomecca.eu/git/explore/repos">Personal Git</a>
2017-05-09 11:29:54 +02:00
<a class="btn" href="https://github.com/FraMecca">Github</a>
2017-05-09 11:38:51 +02:00
<a class="btn" href="/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
2017-05-09 11:29:54 +02:00
</section>
2016-05-01 11:13:57 +02:00
2017-05-09 11:29:54 +02:00
<section class="main-content">
2016-05-01 11:13:57 +02:00
<div class="posts">
2017-10-05 06:35:49 +02:00
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/04/17/kpd-player/">
Kyuss Music Player
</a>
</h1>
<span class="post-date">17 Apr 2016</span>
<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 &#39;artist, album, title&#39; 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 &#39;prototypes&#39; 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>
<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>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>
</div>
</br>
<hr>
</br>
<span> </span>
2017-05-10 20:26:52 +02:00
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/04/10/short-lesson-from-reddit/">
Bright Father
</a>
</h1>
<span class="post-date">10 Apr 2016</span>
<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&#39;d give us a dollar. As we got older it would got a little less outrageous, but we&#39;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>
</br>
<hr>
</br>
<span> </span>
2017-05-07 12:21:36 +02:00
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/04/10/lifehacks/">
Lifehacks
</a>
</h1>
<span class="post-date">10 Apr 2016</span>
<ul>
<li><p>Even though you may be nervous about talking to random people, the worst you can get is &quot;Go away&quot;.</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>
2017-05-09 11:29:54 +02:00
</br>
<hr>
</br>
<span> </span>
2017-05-07 12:21:36 +02:00
2017-03-22 13:07:09 +01:00
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/04/02/buridan_donkey/">
The Buridan's donkey in python
</a>
</h1>
<span class="post-date">02 Apr 2016</span>
<p>During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: <a href="http://www.diveintopython.net/">Dive into Python</a>.
When I noted that python uses the <a href="https://en.wikipedia.org/wiki/Mersenne_Twister">Mersenne Twister PRNG</a> as well I decided to write another version of my <a href="http://francescomecca.eu/index.php/archives/207">Buridan&#39;s donkey program</a>.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"> <span class="kn">import</span> <span class="nn">random</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">args</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">isatty</span><span class="p">():</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">argRange</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">argRange</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s">'.'</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">))))</span></code></pre></figure>
<p>This script works in a different way than the one in c++.
Rather than shuffling a list made by the entries in the arguments, it pops randomly one entry from the list till the list is empty.</p>
<p>Not satisfied enough, I wrote also a telegram bot using the <a href="https://github.com/eternnoir/pyTelegramBotAPI">telebot library</a> that works as the script above but inside the telegram app.
The bot can be added to your contact list by simply searching for <a href="http://telegram.me/duridan_donkey_bot">@duridan_donkey_bot</a> (yes, a typo!)</p>
<p>All the code is opensource and can be found on my github page.</p>
<p>Francesco Mecca</p>
</div>
2017-05-09 11:29:54 +02:00
</br>
<hr>
</br>
<span> </span>
2017-03-22 13:07:09 +01:00
2017-03-20 00:46:14 +01:00
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/04/02/blog-migrated/">
How I migrated to a static blog
</a>
</h1>
<span class="post-date">02 Apr 2016</span>
<p>Until one week ago my blog was hosted at my house, on a raspberrypi with debian + wordpress. I was not satisfied by my setup because given the minimal size of my blog and the really scarce content I post every now and then, a full LLMP stack was overblown.
I decided to change distribution (my server now runs <a href="http://www.voidlinux.eu/">Void</a>) and to migrate to a static blog without CMS nor PHP.</p>
<h1 id="welcome-to-jekyll-and-hyde">Welcome to Jekyll and Hyde</h1>
<p>The process of migration was rather painless.
First of all I installed ruby on my desktop computer, then via ruby gems I installed jekyll:</p>
<p><code>gem install jekyll</code></p>
<p><code>gem install jekyll-gist</code></p>
<p><code>gem install jekyll-paginate</code></p>
<p>I used a <a href="https://wordpress.org/plugins/jekyll-exporter/">wordpress plugin</a> to copy all my old posts.
Then I cloned from <a href="https://github.com/poole/hyde">git</a> the Hyde theme which you can see a demo <a href="http://hyde.getpoole.com/">here</a> and corrected a pair of warning that jekyll printed on my terminal. Actually the <a href="http://jekyllrb.com/docs">jekyll docs</a> are quite complete and covered all the errors that I encountered.</p>
<p>Jekyll structure is quite simple to understand: in the folder _post/ there are your post in markdown format (remember to delete the examples in that folder);
in the root the are some files that should be modified: the about.md file, the 404 page and index.html that is the frontpage of the blog;
finally _config.yml contains the general configuration for the website and should be adjusted to your own likings.
When Jekyll builds a website it parses all the markdown files and stores them in _site folder. Jekyll uses the html files in _layouts and _includes to render the markdown files.A</p>
<p>I added a simple <a href="http://francescomecca.eu/archive/">archive page</a> following the little piece of code in <a href="http://joshualande.com/jekyll-github-pages-poole/">this page</a></p>
<div class="highlight"><pre><code class="language-" data-lang="">---
layout: page
title: Archive
---
## Blog Posts
{% for post in site.posts %}
* {{ post.date | date_to_string }} &amp;raquo; [ {{ post.title }} ]({{ post.url }})
{% endfor %}:wq
</code></pre></div>
<p>I noticed that in _includes/head.html there is this line:</p>
<div class="highlight"><pre><code class="language-" data-lang="">&lt;link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800,600' rel='stylesheet' type='text/css'
</code></pre></div>
<p>so I proceed to remove it because is not needed for my blog.
Finally I put a link to the archive, my github page and the atom feed on the sidebar by simple adding a href on _includes/sidebar.html.</p>
<p>I did not proceed with further modifications but there are tons of possibilities with jekyll. I think that the main advantages are the fact that you don&#39;t have to manage html code when writing a new post and that everything can be done via cli.</p>
<p>Francesco Mecca</p>
</div>
2017-05-09 11:29:54 +02:00
</br>
<hr>
</br>
<span> </span>
2017-03-20 00:46:14 +01:00
2016-05-01 11:13:57 +02:00
</div>
<div class="pagination">
<a class="pagination-item older" href="/page5">Older</a>
<a class="pagination-item newer" href="/page3">Newer</a>
</div>
2017-05-09 11:29:54 +02:00
2017-05-10 20:26:52 +02:00
2017-05-09 11:29:54 +02:00
<footer class="site-footer">
2017-05-10 20:26:52 +02:00
<!--<span class="site-footer-owner"><a href="http://francescomecca.eu">Caught in the Net</a> is maintained by <a href="contattami">Francesco Mecca</a>.</span>-->
2017-05-09 11:29:54 +02:00
<span> CC BY-SA 4.0 International.</br> </span>
<span class="site-footer-credits"><a href="https://jekyllrb.com">Jekyll</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
</footer>
</section>
2016-05-01 11:13:57 +02:00
</body>
</html>