francescomecca.eu/output/index-12.html

219 lines
24 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 12) | 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-12.html">
<link rel="prev" href="index-13.html" type="text/html">
<link rel="next" href="index-11.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>
<a class="btn" href="http://francescomecca.eu/git/pesceWanda">Personal Git</a>
<a class="btn" href="https://github.com/FraMecca">Github</a>
2023-08-30 18:10:14 +02:00
<a class="btn" href="https://lezzo.org/git/public/Curriculum_vitae/raw/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/2/blog-migrated/" class="u-url">How I migrated to a static blog</a></h1>
</header><div>
<span class="post-date">02 April 2016</span>
</div>
<br><div class="e-content entry-content">
<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>
<h2>Welcome to Jekyll and Hyde</h2>
<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>
{% raw %}
---
layout: page
title: Archive
---</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block"><span class="x">## Blog Posts</span>
2018-11-10 18:19:00 +01:00
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">post</span> <span class="k">in</span> <span class="nv">site.posts</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> * </span><span class="cp">{{</span> <span class="nv">post.date</span> <span class="o">|</span> <span class="nf">date_to_string</span> <span class="cp">}}</span><span class="x"> &amp;raquo; [ </span><span class="cp">{{</span> <span class="nv">post.title</span> <span class="cp">}}</span><span class="x"> ](</span><span class="cp">{{</span> <span class="nv">post.url</span> <span class="cp">}}</span><span class="x">)</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x">:wq</span>
</pre>
<p>{% endraw %}
I noticed that in _includes/head.html there is this line:</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block">&lt;link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800,600' rel='stylesheet' type='text/css'
2018-11-10 18:19:00 +01:00
</pre>
<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'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>
</article><br><hr>
<br><article class="post"><header><h1 class="post-title"><a href="blog/2016/4/2/buridan_donkey/" class="u-url">The Buridan's donkey in python</a></h1>
</header><div>
<span class="post-date">02 April 2016</span>
</div>
<br><div class="e-content entry-content">
<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's donkey program</a>.</p>
<p>.. code:: python</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block"> <span class="s s-Atom">import</span> <span class="s s-Atom">random</span><span class="p">,</span> <span class="s s-Atom">sys</span>
2018-11-10 18:19:00 +01:00
<span class="s s-Atom">if</span> <span class="k">__</span><span class="s s-Atom">name__</span> <span class="o">==</span> <span class="s s-Atom">'__main__':</span>
<span class="s s-Atom">args</span> <span class="o">=</span> <span class="nf">list</span><span class="p">()</span>
<span class="s s-Atom">if</span> <span class="o">not</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="s s-Atom">stdin</span><span class="p">.</span><span class="nf">isatty</span><span class="p">()</span><span class="s s-Atom">:</span>
<span class="s s-Atom">for</span> <span class="s s-Atom">line</span> <span class="s s-Atom">in</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="nn">stdin</span><span class="p">:</span>
<span class="s s-Atom">if</span> <span class="s s-Atom">line</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">is</span> <span class="s s-Atom">'\n':</span>
<span class="s s-Atom">line</span> <span class="o">=</span> <span class="s s-Atom">line</span><span class="p">[:-</span><span class="mi">1</span><span class="p">]</span>
<span class="s s-Atom">args</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="s s-Atom">line</span><span class="p">)</span>
<span class="nn">else</span><span class="p">:</span>
<span class="s s-Atom">args</span> <span class="o">=</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="s s-Atom">argv</span><span class="p">[</span><span class="mi">1</span><span class="s s-Atom">:</span><span class="p">]</span>
<span class="s s-Atom">argRange</span> <span class="o">=</span> <span class="nf">len</span><span class="p">(</span><span class="s s-Atom">args</span><span class="p">)</span>
<span class="s s-Atom">for</span> <span class="s s-Atom">i</span> <span class="s s-Atom">in</span> <span class="nf">range</span><span class="p">(</span><span class="s s-Atom">argRange</span><span class="p">)</span><span class="s s-Atom">:</span>
<span class="nf">print</span><span class="p">(</span><span class="nf">str</span><span class="p">(</span><span class="s s-Atom">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s s-Atom">'.'</span><span class="p">,</span> <span class="s s-Atom">args</span><span class="p">.</span><span class="nf">pop</span><span class="p">(</span><span class="s s-Atom">random</span><span class="p">.</span><span class="nf">randrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nf">len</span><span class="p">(</span><span class="s s-Atom">args</span><span class="p">))))</span>
</pre>
<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>
</article><br><hr>
<br><article class="post"><header><h1 class="post-title"><a href="blog/2015/9/20/the-buridans-donkey-paradox/" class="u-url">The Buridan&amp;#8217;s donkey paradox</a></h1>
</header><div>
<span class="post-date">20 September 2015</span>
</div>
<br><div class="e-content entry-content">
<p style="text-align: left;">
The Buridans 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'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 cant 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>
<p>.. code:: c</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block"><span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">iostream</span><span class="o">&gt;</span><span class="w"></span>
2021-12-02 16:47:14 +01:00
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">vector</span><span class="o">&gt;</span><span class="w"></span>
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">algorithm</span><span class="o">&gt;</span><span class="w"></span>
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">random</span><span class="o">&gt;</span><span class="w"></span>
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span><span class="w"></span>
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">sys</span><span class="o">/</span><span class="n">poll</span><span class="p">.</span><span class="n">h</span><span class="o">&gt;</span><span class="w"></span>
<span class="k">using</span><span class="w"> </span><span class="n">namespace</span><span class="w"> </span><span class="n">std</span><span class="p">;</span><span class="w"></span>
<span class="n">struct</span><span class="w"> </span><span class="n">pollfd</span><span class="w"> </span><span class="n">stdin_poll</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="p">.</span><span class="n">fd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fileno</span><span class="w"> </span><span class="p">(</span><span class="n">stdin</span><span class="p">),</span><span class="w"> </span><span class="p">.</span><span class="n">events</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">POLLIN</span><span class="w"></span>
<span class="err">}</span><span class="p">;</span><span class="w"></span>
<span class="n">void</span><span class="w"> </span><span class="n">read_from_piped_input</span><span class="w"> </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="w"> </span><span class="n">lst</span><span class="p">)</span><span class="w"></span>
<span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">getline</span><span class="w"> </span><span class="p">(</span><span class="n">cin</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">))</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="n">push_back</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="err">}</span><span class="w"></span>
<span class="n">void</span><span class="w"> </span><span class="n">read_from_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">const</span><span class="w"> </span><span class="nc">int</span><span class="o">&amp;</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="nc">char</span><span class="o">*</span><span class="w"> </span><span class="n">argv</span><span class="err">[]</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">lst</span><span class="p">)</span><span class="w"></span>
<span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">argc</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="ss">"Usage: asino [string] [string] ..."</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">exit</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="w"> </span><span class="k">for</span><span class="w"> </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="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">argc</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="n">push_back</span><span class="w"> </span><span class="p">(</span><span class="n">argv</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="err">}</span><span class="w"></span>
<span class="nc">int</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="nc">char</span><span class="o">*</span><span class="w"> </span><span class="n">argv</span><span class="err">[]</span><span class="p">)</span><span class="w"></span>
<span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span><span class="w"> </span><span class="n">lst</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">poll_ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">poll</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="n">stdin_poll</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">poll_ret</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">read_from_piped_input</span><span class="w"> </span><span class="p">(</span><span class="n">lst</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">read_from_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="n">argv</span><span class="p">,</span><span class="w"> </span><span class="n">lst</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="w"> </span><span class="n">random_device</span><span class="w"> </span><span class="n">rd</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="n">mt19937</span><span class="w"> </span><span class="n">m</span><span class="p">(</span><span class="n">rd</span><span class="p">());</span><span class="w"></span>
<span class="w"> </span><span class="n">shuffle</span><span class="w"> </span><span class="p">(</span><span class="n">lst</span><span class="p">.</span><span class="k">begin</span><span class="w"> </span><span class="p">(),</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">end</span><span class="w"> </span><span class="p">(),</span><span class="w"> </span><span class="n">m</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">for</span><span class="w"> </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="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">begin</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">end</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="o">++</span><span class="n">it</span><span class="p">)</span><span class="w"> </span><span class="err">{</span><span class="w"></span>
<span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="ss">". "</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="o">*</span><span class="n">it</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="err">}</span><span class="w"></span>
<span class="err">}</span><span class="w"></span>
2018-11-10 18:19:00 +01:00
</pre>
<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>
<p>.. code:: sh</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block">ls /media/my_movies/ | buridan
2018-11-10 18:19:00 +01:00
</pre>
<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>
<p>.. code:: bash</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block">poll (&amp;stdin_poll, 1, 0)
2018-11-10 18:19:00 +01:00
</pre>
<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>
<p>.. code:: bash</p>
2023-08-30 18:10:14 +02:00
<pre class="code literal-block">g++ -std=c++11 ./program.cpp -o output
2018-11-10 18:19:00 +01:00
</pre>
<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'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>
</article><br><hr>
<br>
</div>
<div class="pagination">
<a href="index-13.html" rel="prev"></a>
<a class="pagination-item newer" href="index-13.html">Newer
</a><a class="pagination-item older" href="index-11.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>