jueves 26 de enero de 2012

Python: Assignments with default values

Spanish version: "Python: Asignaciones por defecto"

Some time ago, Manolowar showed me the operator "Elvis" from Groovy.

This operator, written like: "?:" is called in this way because it looks like an emoticon with toupee, just like Elvis Presley. It can be used like in C or Java (separated) or all together. When it is all together, it means something like: "If value is false, assign me it, and if not, assign me this other one". Here you are an example:

Integer i = null;
int a = i ?: 0;

Here we want to assign value of "I" to "A". This is a problem, because "I" admits nulls but "A" does not. With this easy way I solved the problem.

But... Isn't this article about Python?

Yes, it does. Yesteray David told me about the way of doing just this with Python and, IMHO, it is more pretty. You only have to use the "OR" operator.

Opposite I was thinking till now, "OR" in Python does not return a boolean value; it evals the expression and returns the first result that is not false. So, we can find:

i = None
a = i or 7

And "A" will be 7, because "I" is false and the last evaluated value that is not false is 7.

The advantage of using this is that it can be concatenated. Example (do not at home):

a = 0 or '' or [] or {} or False or "Everything else is false"

Which one is the resulting value in this case?

As you can see, every condition is false but the last one, so the final A value will be "Everything else is false".

Use it carefully!!!

Notes

I have been very careful trying to say that it searchs the first "that is not false". This do not mean "the first that is true", because "truth" is True. It is the very first that do not match a false condition.

lunes 23 de enero de 2012

Constructores simples

Esta semana me he dado cuenta de la diferencia existente entre un constructor feo y uno bonito. Es curioso lo fácil que es hacerlo bien y lo poquito que cuesta, así que voy a compartir mis apreciaciones con vosotros, a ver qué opináis.

Durante mi carrera profesional he hecho muchas cosas feas, así que no me echéis en cara si encontráis que he hecho algo diferente de lo que diga :D ¡Esto consiste en ir mejorando día a día!

Actualización 2012/01/24: Por petición de _YeBeNeS_, añado ejemplos en java.

viernes 20 de enero de 2012

Django: Building a basic site with Admin

Spanish version: Django: Creación de un sitio básico con Admin

Moving my site I realized that I have't wrote any post about Django. I was sure enough to have been done it. So I must solve this problem XD

martes 17 de enero de 2012

Repasando: Árboles y Grafos

Pues resulta que mi Sobrino_M_1 me ha pedido ayuda con Árboles y Grafos, pero ha esperado al último momento y no me va a ser posible dedicarle un rato. Así que aprovecho para publicar esta entrada y, con suerte, puede ayudarle.

Temo que esta entrada será un glosario de conceptos, aunque pueden venirnos bien para repasar

lunes 16 de enero de 2012

El patrón Singleton

Para qué sirve

Permite tener una única instancia de un objeto en toda la aplicación

viernes 13 de enero de 2012

Kohana Module

Spanish version: "Módulo Kohana"

I have just build my first Kohana module. It is a mess right now, but it is only a try that become to something. It is possible that I must to rewrite it completely.

You can get the code to test it, but it can be changed quickly and it can be not behind compatible.

This module allows to generate dinamically views for our model and controller.

lunes 9 de enero de 2012

JBehave

A pesar de mi mal comienzo con JBehave, debo decir que le voy cogiendo el tranquillo.

JBehave consiste en un sistema para hacer BDD en Java. En otras palabras: permite definir en un lenguaje no formal el comportamiento de la aplicación, utilizando expresiones regulares para transformarlo en un lenguaje formal.