Recent Content

Https rocks
posted on 2015-05-25 1:05:20

Https is enabled for this site.

Bool is a subclass of Int in Python
posted on 2015-05-23 11:57:30

A few days ago, when I debuged others' interfaces, I got some special harvest.

Imagine a scenario like this:

value = True
if isinstance(value, int):
    print("The value {0} is an int type".format(value))
elif isinstance(value, bool):
    print("The value {0} is an int type".format(value))
    print("The value {0} is others' type".format(value))

But, what will be printed ? It will print The value True is an int type.

So If an interface returns both int and bool, how to deal with it ? Maybe two ways:

  • If bool first, then int etc...

  • Avoid returning bool and int at the same time

About bool, Python offical docs have marked this:

Actually it behaves in that:

>>> True == 1
>>> False == 0

Based on this, we can access list elements as this:

>>> [1, 2][True]
>>>>[1, 2][False]

The fact that True and False are useable as indexes is because they're treated as integers.

And we can use it to implement a ternary operator (:?):

>>> a = 1
>>> b = 2
>>> c = [b, a][a > b]
>>> c

It's just the same like c = a if a > b else b.

That's it.

Imenu: A rapid buffer navigation tool in Emacs
posted on 2015-05-22 23:03:40

Sometimes we have a very large file in Emacs, and need to find a method definition very quickly, how will we do it ?

  • Search with C-s

    Most of the time, we use C-s to do it; but when we don't remember their exact names, and maybe most of them look like a lot, this will be very inefficient.

  • Ctags or etags

    Either ctags or etags, we must produce TAGS first.

  • Imenu

    The Imenu facility offers a way to find the major definitions in a file by name. It is also useful in text formatter major modes, where it treats each chapter, section, etc., as a definition. (from Emacs Manual)

    Ok, below is a Clojure snippet:

    (defn exit [status msg]
      (println msg)
      (System/exit status))
    (def auth
      (let [f (file (System/getProperty "user.home") ".cist")]
    (if (and (.exists f) (.canRead f)) (slurp (.getPath f))
    (defn ls-gists [& {:keys [all-pages private?]}]
      (let [gists (gists/gists {:oauth_token auth :all-pages all-pages})]
    (doseq [g gists]
      (let [filesname (doall (map #(str (:filename %)) (vals (:files g))))]
        (when (not= private? (:public g))
          (println (format "%-50s%s" (:html_url g) (join ", " filesname))))))))
    (defn delete-gist [id]
      (let [result (gists/delete-gist id {:oauth-token auth})]
    (if (true? result)
      (println (format "Gist <%s> has been deleted successfully." id))
      (println "Failed"))))
    (defn- files-contents [files]
      (let [fs (set files)]
    (map #(vector (.getName (file %)) (slurp (.getPath (file %)))) fs)))
    (defn create-gist [files & {:keys [description public] :or {description "" public true}}]
      (let [result (gists/create-gist (files-contents files) {:oauth-token auth :description description :public public})]
    (if (:id result)
      (println (:html_url result))
      (println result))))

    Let's use imenu to deal with it.

    • M-x imenu RET, then press Tab to enter completion buffer, imenu-this

    • A little more, use imenu-anywhere, imenu-anywhere.

      *imenu-anywhere* pops an IDO interface with all the imenu tags across all buffers with the same mode as the current one.
Rust Quotation
posted on 2015-05-16 14:08:00

Who is Rust

From the Official, Rust is a systems programming language that runs blazingly fast, prevents nearly all segfaults, and guarantees thread safety.

  • Rust is an expression oriented language, which means that most things are expressions, rather than statements.


  • A Rust macro, which is how metaprogramming is done in Rust.

  • Rust implements println! as a macro rather than a function for good reasons, but that's an advanced topic.

  • Don’t be scared of using macros.


  • "Hello, world!" is a string. Strings are a surprisingly complicated topic in a systems programming language, and this is a statically allocated string.


  • Everything is a tradeoff in language design, and Rust has made its choice.

Variable Bindings

  • In many languages, this is called a variable, but Rust's variable bindings have a few tricks up their sleeves. For example the left-hand side of a let expression is a pattern, not just a variable name. This means we can do things like:

    let (x, y) = (1, 2);
  • By default, bindings are immutable.

  • There is no single reason that bindings are immutable by default, but we can think about it through one of Rust's primary focuses: safety.

  • Rust variable bindings have one more aspect that differs from other languages: bindings are required to be initialized with a value before you're allowed to use them.

Type Inference

  • If it can figure out what the type of something is, Rust doesn't require you to actually type it out.


  • Function arguments work very similar to let declarations: you add a type to the argument name, after a colon.

  • Unlike let, you must declare the types of function arguments.

    fn print_sum(x: i32, y: i32) {
    println!("sum is: {}", x + y);

    This is a deliberate design decision. While full-program inference is possible, languages which have it, like Haskell, often suggest that documenting your types explicitly is a best-practice. We agree that forcing functions to declare types while allowing for inference inside of function bodies is a wonderful sweet spot between full inference and no inference.

Expressions vs. Statements

  • Rust is primarily an expression-based language. There are only two kinds of statements, and everything else is an expression.

  • Expressions return a value, and statements do not.

  • There are two kinds of statements in Rust: declaration statements and expression statements.

  • A let can only begin a statement, not an expression.

    let mut y = 5;
    let x = (y = 6);  // x has the value `()`, not `6`

Early Returns

  • Using a return as the last line of a function works, but is considered poor style:

    fn foo(x: i32) -> i32 {
    return x + 1;

Primitive Types


  • The char type represents a single Unicode scalar value. Unlike some other languages, this means that Rust's char is not a single byte, but four.


  • A 'slice' is a reference to (or "view" into) another data structure. They are useful for allowing safe, efficient access to a portion of an array without copying.


  • A tuple is an ordered list of fixed size. Like this:

    let x = (1, "hello");
    let x: (i32, &str) = (1, "hello");
  • Tuples are heterogeneous: we have an i32 and a &str in this tuple.

    You can disambiguate a single-element tuple from a value in parentheses with a comma:

    (0,); // single-element tuple
    (0); // zero in parentheses, like Python's tuple.

    You can also access fields of a tuple with indexing syntax:

    let tuple = (1, 2, 3);
    let x = tuple.0;
    let y = tuple.1;
    let z = tuple.2;
    println!("x is {}", x);

    Like array indexing, it starts at zero, but unlike array indexing, it uses a ., rather than []s.


  • Functions also have a type! They look like this:

    fn foo(x: i32) -> i32 { x }
    let x: fn(i32) -> i32 = foo;

    In this case, x is a 'function pointer' to a function that takes an i32 and returns an i32.


  • Rust has two kinds of comments that you should care about: line comments and doc comments.

    // Line comments are anything after ‘//’ and extend to the end of the line.
    let x = 5; // this is also a line comment.
    // If you have a long explanation for something, you can put line comments next
    // to each other. Put a space between the // and your comment so that it’s
    // more readable.

    Doc comments use /// instead of //, and support Markdown notation inside:

    /// Adds one to the number given.
    /// # Examples
    /// ```
    /// let five = 5;
    /// assert_eq!(6, add_one(5));
    /// ```
    fn add_one(x: i32) -> i32 {
    x + 1


  • Rust's if is an expression.

    let x = 5;
    let y = if x == 5 {
    } else {
    }; // y: i32

    Rust goes even further:

    let x = 5;
    let y = if x == 5 { 10 } else { 15 }; // y: i32

    An if without an else always results in () as the value.

for Loops

  • Rust does not have the “C-style” for loop on purpose.

    for var in expression {

    The expression is an iterator.

while Loops

  • Rust has a dedicated keyword, loop, to handle while true:

    loop {

    Rust’s control-flow analysis treats this construct differently than a while true, since we know that it will always loop.



  • Rust has a focus on safety and speed. It accomplishes these goals through many zero-cost abstractions, which means that in Rust, abstractions cost as little as possible in order to make them work. The ownership system is a prime example of a zero-cost abstraction.

  • However, this system does have a certain cost: learning curve. Many new users to Rust experience something we like to call fighting with the borrow checker, where the Rust compiler refuses to compile a program that the author thinks is valid.


  • Variable bindings have a property in Rust: they ‘have ownership’ of what they’re bound to.

    fn foo() {
    let v = vec![1, 2, 3];

    When v goes out of scope at the end of foo(), Rust will clean up everything related to the vector, even the heap-allocated memory.

Move semantics

  • Rust ensures that there is exactly one binding to any given resource:

    let v = vec![1, 2, 3];
    let v2 = v;
    println!("v[0] is: {}", v[0]);

    It looks like this:

    error: use of moved value: `v`
    println!("v[0] is: {}", v[0]);

    When we transfer ownership to something else, we say that we’ve ‘moved’ the thing we refer to.

Copy types

  • We’ve established that when ownership is transferred to another binding, you cannot use the original binding. However, there’s a trait that changes this behavior, and it’s called Copy.

    let v = 1;
    let v2 = v;
    println!("v is: {}", v);

    In this case, v is an i32, which implements the Copy trait. This means that, just like a move, when we assign v to v2, a copy of the data is made. But, unlike a move, we can still use v afterward. This is because an i32 has no pointers to data somewhere else, copying it is a full copy.

References and Borrowing


  • ...

    fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
     // do stuff with v1 and v2
    // return the answer
    let v1 = vec![1, 2, 3];
    let v2 = vec![1, 2, 3];
    let answer = foo(&v1, &v2);
    // we can use v1 and v2 here!

    Instead of taking Vecs as our arguments, we take a reference: &Vec. And instead of passing v1 and v2 directly, we pass &v1 and &v2.

    We call the &T type a ‘reference’, and rather than owning the resource, it borrows ownership.

    A binding that borrows something does not deallocate the resource when it goes out of scope. This means that after the call to foo(), we can use our original bindings again.

  • References are immutable, just like bindings. This means that inside of foo(), the vectors can’t be changed at all:

    fn foo(v: &Vec<i32>) {
    let v = vec![];

    errors with:

    error: cannot borrow immutable borrowed content `*v` as mutable

&mut references

  • There’s a second kind of reference: &mut T. A ‘mutable reference’ allows you to mutate the resource you’re borrowing. For example:

    let mut x = 5;
    let y = &mut x;
    *y += 1;
    println!("{}", x);
Make a dynamic class in Python
posted on 2015-05-14 23:17:00

Sometimes, we hope a class can be dynamicly created in python runtime.

Take this scenario, we have two Django model classes:

from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    sex = models.IntegerField()
    instrument = models.CharField(max_length=100)

    def update_sex(cls, id, sex):
    if cls.objects.filter(pk=id).update(sex=sex) > 0:
        # update Redis

class Album(models.Model):
    name = models.CharField(max_length=30)
    release_date = models.DateField()
    num_stars = models.IntegerField()

    def update_name(cls, id, name):
    if cls.objects.filter(pk=id).update(name=name) > 0:
        # update Redis

Ok, what if we just need Redis key-value cache, how can we design our Redis class ? Most of all, we think about two same like classes like this:

class RMusician:

    def set_sex(cls, key, sex):

    def get_sex(cls, key):

class RAlbum:

    def set_sex(cls, key, sex):

    def get_sex(cls, key):

This may be very ugly ! But we can make things better in Python:

def make_class(cls, *fields):
    RedisFactory = type('RedisFactory', (), {'r': redis.Redis()})

    def set_x(key, x):
    Redisfactory.r.set(key, x)

    def get_x(field, key):
    x = Redisfactory.r.get(key)
    if not x:
        obj = globals()[cls].objects.filter(id=key).first()
        if obj:
        x = getattr(obj, field)
        set_x(key, x)
    return x

    for f in fields:
    # setattr(RedisFactory, 'get_{0}'.format(f), classmethod(lambda kls, key: get_x(f, key)))
    # setattr(Redisfactory, 'set_{0}'.format(f), classmethod(lambda kls, key, x: set_x(key, x)))

    return RedisFactory

RMusician = make_class('Musician', 'sex')
RAlbum = make_class('Album', 'name')

That's it.

Emacs is a habit
posted on 2015-05-09 00:21

What is Emacs

EMACS may stand for Editing MACroS, but it has some more creative definitions. Here they are.

  • Emacs Makes A Computer Slow
  • Emacs May Annihilate Command Structures
  • Evenings, Mornings, And a Couple of Saturdays
  • Ever Made A Control-key Setup?
  • Elephantine Memory Absolutely Considered Sine que non
  • Emacs Makers Are Crazy Sickos
  • ...

My road to mutiny

Interactive Mode

Before Emacs, I used Vim for three more years, so I think I'm very familiar with it.

For Vim users (including me), a small detail is they often restart vim. they open a file, edit it , exit vim; then another, and so on. One reason may be Vim really starts very quickly, but the most important is that Vim lacks interactive mode.

Compared with Emacs's super-charging completion engine and Ido Mode, Vim is a bullshit in doing things interactively.

Of course, Ido Mode in Emacs is off by default, you can enable it like this:

(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode 1)

It'll enable basic Ido support for files and buffers and the very useful flex matching as well.

Package management

Since Emacs 24 (2012), It (package.el) became part of Emacs. But util now, Vundle is still a plug-in manager for Vim.

What does it mean ? It means Emacs has an official maintained package repository (GNU ELPA Packages) while Vim not. So What Vim missing ? It is Software Reuse.

Ok, You maybe justify that Vim is just an editor not an OS; so package is meaningless for Vim; and Emacs is an OS that a package can be installed in it. For me it is a meaningless excuses.

Either Emacs or Vim, They both have a runtime environment to ensure that plugins or packages run. But on top of Emacs runtime, there is a library layer. Maybe it is the important difference between Emacs and Vim runtime environment.

End result, in Emacs, either a library or plug-in like package, they are treated equally; and a plug-in like package can reuse a library package.

Relative to Vim, I'm more optimistic about Neovim, I hope it can be better than Vim in plugin system, and I donated $15 to it.

Emacs package interface is very good, you can not only use GNU ELPA Packages but also third ones.

(require 'package)

(add-to-list 'package-archives '("melpa" . "") t)

(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "")))

PostgreSQL Aggregate Functions
posted on 2015-05-09 00:11

Sometimes, when we use SQL aggregate functions, we hope to collect all primary keys in each column group.

Take below for example:

SELECT date_trunc('day', create_time) AS date,
FROM s_table
ORDER BY date;

In this sql code, we can just get columns grouped by or aggregate results; we can not get id columns (or others) in each row.

But how can we get that ? We can use string_agg:

SELECT date_trunc('day', create_time) AS date,
   string_agg(cast(id as text), ',')
FROM s_table
ORDER BY date;

Maybe you have noticed a comma , as sting_agg's second parameter, It is just a delimiter, you can use any one you would like.

So you maybe get some records as below:

2015-01-17 00:00:00+08 | 5 | 1461491,1461508,1461482,1461483,1461509

It is very Excited !

Rock with Coleslaw
posted on 2015-05-06 22:21:50

A long time, I used jekyll as my static site generator. But now I will play some lisp, so Coleslaw comes.

This blog covers coleslaw, django, editor, emacs, https, lisp, postgre, python, rust, sql

View content from 2015-05

Unless otherwise credited all material Creative Commons License by Lingchao Xin