francescomecca.eu/_site/index.html

338 lines
17 KiB
HTML
Raw Normal View History

2016-05-01 11:13:57 +02:00
<!DOCTYPE html>
<html lang="en-us">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>
Caught in the Net &middot; La rete ti cattura ma libera il pensiero
</title>
<!-- CSS -->
<link rel="stylesheet" href="/public/css/poole.css">
<link rel="stylesheet" href="/public/css/syntax.css">
<link rel="stylesheet" href="/public/css/hyde.css">
<!-- Icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="/public/favicon.ico">
<!-- RSS -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
</head>
<body class="theme-base-09">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<h1>
<a href="/">
Caught in the Net
</a>
</h1>
<p class="lead"></p>
</div>
<nav class="sidebar-nav">
<a class="sidebar-nav-item active" href="/">Home</a>
<a class="sidebar-nav-item" href="/about/">About</a>
<a class="sidebar-nav-item" href="/archive/">Archive</a>
<a class="sidebar-nav-item" href="/contattami/">Contattami</a>
<a class="sidebar-nav-item" href="/atom.xml">RSS</a>
<a class="sidebar-nav-item" href="http://francescomecca.eu:3000">Personal Git</a>
<a cleass="sidebar-nav-item" href="https://github.com/s211897-studentipolito">github</a>
<span class="sidebar-nav-item" href="" >Powered by Jekyll and Hyde</span>
</nav>
<p>&copy; 2016. CC BY-SA 4.0 International </p>
</div>
</div>
<h3 class="masthead-title">
<a href="/" title="Home">Caught in the Net</a>
</h3>
<div class="content container">
<div class="posts">
<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>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>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>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>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>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>
<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>
<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>
<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>
<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>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>
</div>
<div class="pagination">
<a class="pagination-item older" href="/page2">Older</a>
<span class="pagination-item newer">Newer</span>
</div>
</div>
</body>
</html>