francescomecca.eu/_site/page5/index.html
Francesco Mecca ee4fba2d86 coventry 1
2018-10-25 12:23:48 +02:00

340 lines
22 KiB
HTML

<!DOCTYPE html>
<html lang="en-us">
<head>
<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">
</head>
<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>
<a class="btn" href="/about/">About me</a>
<a class="btn" href="/contattami/">Contact me</a>
<a class="btn" href="/archive/">Archive</a>
<a class="btn" href="/feed.xml">RSS</a>
<a class="btn" href="http://francescomecca.eu/git/pesceWanda">Personal Git</a>
<a class="btn" href="https://github.com/FraMecca">Github</a>
<a class="btn" href="/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
</section>
<section class="main-content">
<div class="posts">
<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>
</br>
<hr>
</br>
<span> </span>
<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>
</br>
<hr>
</br>
<span> </span>
<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>
</br>
<hr>
</br>
<span> </span>
<div class="post">
<h1 class="post-title">
<a href="/index.php/archives/207">
The Buridan&#8217;s donkey paradox
</a>
</h1>
<span class="post-date">20 Sep 2015</span>
<p style="text-align: left;">
The Buridan&#8217;s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.
</p>
<p style="text-align: left;">
The paradox shows an hypothetical situation in which a donkey searching for food is caught in the middle of two equally appealing stacks of hay located at the same distance from the donkey. Because the donkey has no real reason to choose one over the other he dies of starvation.
</p>
<p><a href="https://en.wikipedia.org/wiki/Buridan&#x27;s_ass"><img class="aligncenter size-full wp-image-209" src="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg" alt="Deliberations_of_Congress" width="431" height="425" srcset="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress-300x296.jpg 300w, http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg 431w" sizes="(max-width: 431px) 100vw, 431px" /></a>I have decided to write a cli program that chooses for me when I can&#8217;t make up my mind.</p>
<p>The program is written in C++ and when invoked along with two or more arguments it puts them in a vector and then changes the order randomly.</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="cp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;random&gt;
#include &lt;string&gt;
#include &lt;sys/poll.h&gt;
</span><span class="n">using</span> <span class="n">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">pollfd</span> <span class="n">stdin_poll</span> <span class="o">=</span> <span class="p">{</span>
<span class="p">.</span><span class="n">fd</span> <span class="o">=</span> <span class="n">fileno</span> <span class="p">(</span><span class="n">stdin</span><span class="p">),</span> <span class="p">.</span><span class="n">events</span> <span class="o">=</span> <span class="n">POLLIN</span>
<span class="p">};</span>
<span class="kt">void</span> <span class="nf">read_from_piped_input</span> <span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;&amp;</span> <span class="n">lst</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">string</span> <span class="n">x</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">getline</span> <span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span> <span class="p">{</span>
<span class="n">lst</span><span class="p">.</span><span class="n">push_back</span> <span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">read_from_arguments</span> <span class="p">(</span><span class="k">const</span> <span class="kt">int</span><span class="o">&amp;</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[],</span> <span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;&amp;</span> <span class="n">lst</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Usage: asino [string] [string] ..."</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">exit</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">for</span> <span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;::</span><span class="n">size_type</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">argc</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
<span class="n">lst</span><span class="p">.</span><span class="n">push_back</span> <span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">main</span> <span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">lst</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">poll_ret</span> <span class="o">=</span> <span class="n">poll</span> <span class="p">(</span><span class="o">&amp;</span><span class="n">stdin_poll</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">poll_ret</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">read_from_piped_input</span> <span class="p">(</span><span class="n">lst</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">read_from_arguments</span> <span class="p">(</span><span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">,</span> <span class="n">lst</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">random_device</span> <span class="n">rd</span><span class="p">;</span>
<span class="n">mt19937</span> <span class="n">m</span><span class="p">(</span><span class="n">rd</span><span class="p">());</span>
<span class="n">shuffle</span> <span class="p">(</span><span class="n">lst</span><span class="p">.</span><span class="n">begin</span> <span class="p">(),</span> <span class="n">lst</span><span class="p">.</span><span class="n">end</span> <span class="p">(),</span> <span class="n">m</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;::</span><span class="n">iterator</span> <span class="n">it</span> <span class="o">=</span> <span class="n">lst</span><span class="p">.</span><span class="n">begin</span> <span class="p">();</span> <span class="n">it</span> <span class="o">!=</span> <span class="n">lst</span><span class="p">.</span><span class="n">end</span> <span class="p">();</span> <span class="o">++</span><span class="n">it</span><span class="p">)</span> <span class="p">{</span>
<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="o">++</span> <span class="o">&lt;&lt;</span> <span class="s">". "</span> <span class="o">&lt;&lt;</span> <span class="o">*</span><span class="n">it</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>I have used the <a href="http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine" target="_blank">Mersenne Twister PRNG</a> just to give it a try.</p>
<p style="text-align: left;">
One of the challenges was to read from stdin instead of arguments when the program is piped after another program in the shell:
</p>
<figure class="highlight"><pre><code class="language-sh" data-lang="sh">ls /media/my_movies/ | buridan</code></pre></figure>
<p>So I have used poll() that checks for a specified amount of time if the selected device (/dev/stdin in my case) can perform I/O operations; in my code:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">poll <span class="o">(</span>&amp;stdin_poll, 1, 0<span class="o">)</span></code></pre></figure>
<p>I selected the POLLIN as event so poll() only checks if there is data to read, 1 as the number of items in the fds array, 0 milliseconds of timeout because when the program is invoked /dev/stdin may already contain input.</p>
<p>The program should be compiled this way:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">g++ -std<span class="o">=</span>c++11 ./program.cpp -o output</code></pre></figure>
<p>You are free to reuse this little piece of code as you wish.</p>
<p>EDIT: 02-04-2016
The original idea for the Buridan&#39;s donkey came from my mentor <a href="https://twitter.com/bassosimone">Simone Basso</a> who wrote the original one in haskell.</p>
<p style="text-align: right;">
Francesco Mecca
</p>
</div>
</br>
<hr>
</br>
<span> </span>
<div class="post">
<h1 class="post-title">
<a href="/index.php/archives/198">
About Perl packages in Gentoo
</a>
</h1>
<span class="post-date">11 Sep 2015</span>
<p>In this post I wish to explain a bit about Perl related ebuilds in Gentoo and how to update from perl-core to virtual/perl without conflicts.</p>
<p>First of all the core distribution of Perl is included in <a href="https://packages.gentoo.org/package/dev-lang/perl">dev-lang/Perl</a> along with <a href="http://perldoc.perl.org/index-modules-A.html">modules</a>.</p>
<p>The <a href="https://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/perl-core/">perl-core category</a> in portage contains ebuilds for perl modules, some of which are included in the core perl distibution. The perl-core ebuilds overrides the perl-core modules such that you can update them without recompiling dev-lang/Perl.</p>
<p>Each perl-core module has a corrensponding virtual/perl-* package. The virtual category is used by portage when emerging to take into account that a module can be either provided by dev-lang/perl package or found in perl-core category.</p>
<p>So, what to do in case of blocks when upgrading your Gentoo machine?</p>
<p>Perl-cleaner is a package provided to get rid of these blocks and to migrate from perl-core to virtual/perl-*.</p>
<pre class="wp-code-highlight prettyprint linenums:1">emerge perl-cleaner
emerge dev-lang/perl
perl-cleaner --all</pre>
<p>if you still have problems when upgrading @world you add a backtrack option</p>
<pre class="wp-code-highlight prettyprint linenums:1">emerge -uDN --backtrack=30 @world</pre>
<p>or you can unmerge all perl-core packages and then finally upgrade world</p>
<pre class="wp-code-highlight prettyprint linenums:1">emerge --unmerge perl-core/*</pre>
<p>This will remove all perl-core packages that virtual/perl-* packages will bring back if needed.</p>
<p style="text-align: right;">
Francesco Mecca
</p>
</div>
</br>
<hr>
</br>
<span> </span>
</div>
<div class="pagination">
<a class="pagination-item older" href="/page6">Older</a>
<a class="pagination-item newer" href="/page4">Newer</a>
</div>
<footer class="site-footer">
<!--<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>-->
<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>
</body>
</html>