Sunday, June 21, 2015

When working on a website or application, it's better not to waste your time trying to make it perfect. Instead you should focus on making it work first, creating a simple demo. And the faster you go, the better it is.

Only after the first demo is finished, you can massage it by refactoring the code,
dealing with the performance, etc. But not at beginning, otherwise you'll never finish it.

Saturday, June 13, 2015

An awfully simple definition of a monad

Here's a definition of a monad from wikipidea which seems pretty simple


Formally, a monad consists of a type constructor M and two operations, bind and return (where return is often also called unit). The operations must fulfill several properties to allow the correct composition of monadic functions (i.e. functions that use values from the monad as their arguments or return value). The return operation takes a value from a plain type and puts it into a monadic container using the constructor, creating a monadic value. The bind operation performs the reverse process, extracting the original value from the container and passing it to the associated next function in the pipeline, possibly with additional checks and transformations.


That is, a monad is a type which is used by some functions (that accept monads) to combine them into a chain.

Tuesday, June 2, 2015

A simple javascript notification

If you want to show a simple notification on the page using javascript and don't want to use any third-party librariy because of their overhead, here's what you can do: you can create a div, show and then remove it automatically.

  function showAlert(msg) {
    var el = document.createElement("div");
    el.setAttribute('style', 'position:fixed; top:50%; left:40%; background-color:white;');
    el.innerHTML = msg;
    setTimeout(function(){
      el.parentNode.removeChild(el);
    }, 1000);
    
    document.body.appendChild(el);
  }

That's a pretty light and easy solution which requires no third-party libraries.

Monday, May 18, 2015

false can be equal to True in Python

In Python 2.7 everything but an empty string and False itself evaluates to True. This also returns True:


(Pdb) bool('false')
True

(Pdb) bool(u'false')
True

Tuesday, April 28, 2015

has_many in Django 1.8

In Rails you can create a relationship between the models by has_many and belongs_to. Django has an analog of belongs_to which is ForeignKey but it seems it doesn't have something like has_many. Actually it does:

from django.db import models
class MyModel1(models.Model):
    field1 = models.CharField(max_length=255)
    # has_many MyModel2, implicitly


class MyModel2(models.Model):
    field1 = models.CharField(max_length=255)
    my_model1 = models.ForeignKey(MyModel1) # has_one MyModel1 


my_model1 = MyModel1.object.get(id=1)
my_model1.mymodel2_set.all() # has_many

Pay attention that there's no white-space in mymodel2_set between my and model2_set