How to use yield in Python

Notes on the yield statement in Python

From the Python documentation we can read that:

  1. What it is: The yield statement is used when defining a generator within the body of a generator function. Thus, if you use a yield statement in a function, this creates a generator function instead of a normal function.
  2.  What it does: When a yield statement is executed, the state of the generator is frozen and the value of expression_list is returned to next()’s caller.
  3. How to use it: When a generator function is called, it returns an iterator known as a generator iterator, or simply, a generator. The body of the generator function is executed by calling the generator’s next() method repeatedly until it raises an exception.
Continue reading “How to use yield in Python”

Second Cambridge Stoics meet-up

What is the promise of Stoic philosophy?

Tuesday, Feb 19, 2019, 7:00 PM

The Waterman
32 Chesterton Rd Cambridge, GB

18 Members Went

Since antiquity, all philosophers have tried to answer the fundamental questions, providing what they consider to be the true “truth of existence”. Despite that, a question is left: What kind of advantages can the study of philosophy bring to me? especially in my daily life? Can I turn knowledge into power? The philosopher Epictetus, promises us, t…

Check out this Meetup →

Since antiquity, all philosophers have tried to answer the fundamental questions, providing what they consider to be the true “truth of existence”.

Despite that, a question is left:
What kind of advantages can the study of philosophy bring to me? especially in my daily life? Can I turn knowledge into power?

The philosopher Epictetus, promises us, that the study of Stoicism can deliver us practical and useful tools, that we can apply in our life, to reach what the Greeks called eudaimonia, in English happiness (or serenity of the spirit).

In this new meeting we are going to discuss these tools and how can we reach the ultimate goal of Stoic philosophy: Happiness!.

Come fare un grafico a barre con ggplot2

Il pacchetto ggplot2 è una delle risorse più potenti per la creazione di grafici in R.

Anche se, ggplot2 ha una curva di apprendimento piuttosto alta che potrebbe scoraggiare chi inizia a usarlo, ma credetemi ne vale sicuramente la pena.

Qui voglio mostrare un paio di esempi dei grafici a barre:

# Per questi grafici abbiamo le informazioni relative a un database di topi immunizzati con due diversi antigeni OVA e CFA, 
# e sono riportati i tempi  dopo la vaccinazione. 
# Il gruppo di controllo ha il tempo zero perché quelli non sono stati immunizzati.

library(ggplot2) time_days=c(0,0,0,0,0,0,0,0,0,5,5,5,5,5,5,7,7,7,7,7,14,14,14,14,14,14,60,60,60,60,60,60,60,60,60,60,60)
db <- data.frame(time_days,antigens) # Il database come data.framse 
ggplot2::qplot( data = db, factor(time_days), fill = factor(time_days), geom = "bar" )+
 ggplot2::scale_fill_discrete(name='Time in days') +
 ggplot2::ggtitle('Group of mice per date of sacrifice') + 
 ggplot2::xlab('Time in days') +
ggplot2::ylab('Number of mice') 

Con il seguente risultato:

Con questa trama vediamo il numero di topi e il tempo in cui sono stati sacrificati.

Ora se vogliamo vedere sia il numero di topi che gli antigeni usati potremmo fare il seguente:

ggplot2::qplot( data=db, geom="bar", factor(time_days), fill=factor(antigens) ) +
 ggplot2::theme_classic() +
 ggplot2::ggtitle('Group of mice per date of sacrifice and antigens')+
 ggplot2::scale_fill_discrete(name='Antigens') +
 ggplot2::xlab('Time in days') +

Con il seguente risultato:

Intro for the first Stoic event in Cambridge

This is a transcription of my introductory speech for the first Stoic meeting of the Cambridge Stoa


So, we are going to divide the meeting in the following parts:

  1. I want to explain to you, who am I; why I decide to create this meet-up; I want to give a short intro an what is Stoicism and what it means to me and possible to you.
  2. I want to hear from you, what is your experience with Stoicism? Do you know it? Who of you ever heard about Stoicism?
  3. Here with me I have many books, I thought we can start by discussing one of the main topics of the Stoicism, the duality of control, reading from Epictetus. If you want to discuss a specific topic, we can discuss it, or better we can discuss next time.

Why this meeting?

For what concern me. I encountered Stoicism for the first time, like many in school, but in more recent time, it started appearing in my podcasts then in and some videos on you tube. I wandered what was all that fuss about and, in the last year started or so I start having an increasing interest in stoicism.

I started reading the books, Donald Robertson, Pigliucci, etc. I also went to a Stoic conference in London, the “Stoicon”.

There three-hundred people gather to discuss and listen about stoicism. There again I met people from the Stoic Fellowship, an international organization that promote the spread of Stoicism. I have joined them and decide to start and seek people with this interest.

Continue reading “Intro for the first Stoic event in Cambridge”

Labelled plot in ggplot2

ggplot2 is an amazing tool for plotting in R. One of the latest feature I found out it is the possibility to label the plot automatically. Let’s check it out:

data <- data.frame(class=c('A','B','C'), 
                   value=c(50, 30, 20))

g <- ggplot2::ggplot(data = data,
                ggplot2::aes(x = class,
                             y = value)
                ) +
  ggplot2::geom_bar(stat = "identity", 
                    ) +
                      size=5) +

ggplot2::ggsave('labelled_plot.pdf', plot=g, device = 'pdf')

This would be the result:

Very easy and fast!

First Cambridge Stoics event

At the beginning of the year I have decide to create a meet-up event in Cambridge open for who is interested in the philosophy of Stoicisms.

Philosophy can be seen as dull and useless for the average man. However, Stoicism is nothing like that, it’s practical teaching and methods to coping mechanisms are aimed especially to who live a stress-full and chaotic life and Stoicism is becoming an important part of my life.

Therefore I personally invite everyone who is interested in coming at the coffee area of the Waterman Pub in Cambridge at the 19:00. For all infos and signing-up follow this link:

How to use R to create Latex documents

At work I have been asked to create a R code that could automatic generate a Latex document.

This was great! Latex was the first language that I learnt and it has a special place in my nerd heart.

It is nice to see that is possible to work with R for Latex and create a PDF document that can change in relation to the input given to the code.

All the code is present in my GitHub repository:

Continue reading “How to use R to create Latex documents”

How to do tables in Latex

Here reported the code for a simple table in $latex \LaTeX$:

\hline Rows  &amp;amp;amp; Columns 1 &amp;amp;amp;amp; Column 2} \\
\hline Row 1 &amp;amp;amp;amp; Value 1,1 &amp;amp;amp;amp; Value 2,1 \\
\hline Row 2 &amp;amp;amp;amp; Value 1,2 &amp;amp;amp;amp; Value 2,2 \\
\hline Row 3 &amp;amp;amp;amp; Value 1,3 &amp;amp;amp;amp; Value 2,3 \\
\hline Row 4 &amp;amp;amp;amp; Value 1,4 &amp;amp;amp;amp; Value 2,4 \\ \hline
\caption[Short title for contains]{Description of table content}

We the command \begin{tabular} we position the table within the the float environment table at which we add the position between [] (h, t, b, etc).

Continue reading “How to do tables in Latex”

bar-plots using ggplot2

The package ggplot2 is one of the most powerful resource for plot making available in R.

Although, it has with quite a learning curve, that could be intimidating, it is definitely worth the effort.

In here I want to show a couple of the first bar plot I ever made with the ggplot2 package:

Continue reading “bar-plots using ggplot2”