Find out if every element of a list is part of another, with Python

Update 2010-08-27: Comments indicated that what I did here is not the best solution. Like noted in my original post, a set would be better in this case. I eventually used set(r).issubset(set(l)). Marius also pointed out to set(r) <= set(l), but I like the issubset one more.

I wanted to check if every element of one list or tuple is part of another one using Python. A set has the issubset()-method, but I couldn't find anything build-in for a tuple. It was, however, rather quickly done:

>>> r = (1,2)
>>> l = (3,4,1,5,2)
>>> False not in [ e in l for e in r ]
True
>>> r = (1,9)
>>> False not in [ e in l for e in r ]
False

Why I'm posting this? I just found it cute code, somehow.

A chessboard in MySQL: make your moves

Playing chess within MySQL? Over the network? In the .. cloud? Yes!

This is a follow-up post of my ‘A chessboard in MySQL’ where we create and populate a chessboard. But pieces need to move, and a few wondered how. Easy!

As an example, white openes with 1.e4:

BEGIN;
UPDATE chessboard SET e='♙' WHERE x = 4;
UPDATE chessboard SET e='' WHERE x = 2;
COMMIT;

Pretty obvious. Now lets put it in a stored procedure (source included in post) so the next move is easier on the fingers and more fun to play. Lets do a 1…e5:

CALL move_piece('e','7','e',5);

The result is the following:

mysql> SELECT * FROM chessboard;
+---+------+------+------+------+------+------+------+------+
| x | a    | b    | c    | d    | e    | f    | g    | h    |
+---+------+------+------+------+------+------+------+------+
| 8 | ♜    | ♞    | ♝    | ♛    | ♚    | ♝    | ♞    | ♜    |
| 7 | ♟    | ♟    | ♟    | ♟    |      | ♟    | ♟    | ♟    |
| 6 |      |      |      |      |      |      |      |      |
| 5 |      |      |      |      | ♟    |      |      |      |
| 4 |      |      |      |      | ♙    |      |      |      |
| 3 |      |      |      |      |      |      |      |      |
| 2 | ♙    | ♙    | ♙    | ♙    |      | ♙    | ♙    | ♙    |
| 1 | ♖    | ♘    | ♗    | ♕    | ♔    | ♗    | ♘    | ♖    |
+---+------+------+------+------+------+------+------+------+

Here is the stored procedure. It’s very, very basic, and of course, one can add much more!

DROP PROCEDURE IF EXISTS move_piece;
delimiter //
CREATE PROCEDURE move_piece(
    psrcCol CHAR(1),
    psrcRow TINYINT,
    pdstCol CHAR(1),
    pdstRow TINYINT)
BEGIN
    SET @srcCol = psrcCol;
    SET @srcRow = psrcRow;
    SET @dstCol = pdstCol;
    SET @dstRow = pdstRow;
    SET @piece = 0;
    SET @blank = '';
    
    -- Get the piece we are moving
    SET @stmt = CONCAT('SELECT ',@srcCol,
        '+0 INTO @piece FROM chessboard WHERE x = ?');
    PREPARE preStmt FROM @stmt;
    EXECUTE preStmt USING @srcRow;
    DEALLOCATE PREPARE preStmt;
    
    IF ((@piece > 1 AND @piece <= 14) AND @piece is not NULL)
    THEN
        -- Move the piece
        SET @stmt = CONCAT('UPDATE chessboard SET ',
            @dstCol,'=? WHERE x = ?');
        PREPARE preStmt FROM @stmt;
        EXECUTE preStmt USING @piece,@dstRow;
        DEALLOCATE PREPARE preStmt;
        SET @stmt = CONCAT('UPDATE chessboard SET ',
            @srcCol,'=? WHERE x = ?');
        PREPARE preStmt FROM @stmt;
        EXECUTE preStmt USING @blank,@srcRow;
        DEALLOCATE PREPARE preStmt;
    ELSE
        SELECT "No piece found at given position." AS Error;
    END IF;
END;
//

Some thoughts for future expansion:

  • You could save the moves in a separate table to record time it took.
  • Implement some other movement notation.
  • Lock a player after a move.
  • Build some other stored routines to populate and reset the chessboard.
  • Have multiple chessboard tables.
  • Event scheduler can be used to implement the non-human player!

Anyway, this was all about fun and Unicode testing. There will be probably no follow-up on this post. If somebody is crazy enough to actually implement a chess game in MySQL: awesome!

(Disclaimer: I am not a chess player.)

UPDATE 2009-12-31: Here is the insert statement for populating the chessboard:

INSERT INTO `chessboard` VALUES
  (8,'♜','♞','♝','♛','♚','♝','♞','♜'),
  (7,'♟','♟','♟','♟','♟','♟','♟','♟'),
  (6,'','','','','','','',''),(5,'','','','','','','',''),
  (4,'','','','','','','',''),<br />  (3,'','','','','','','',''),
  (2,'♙','♙','♙','♙','♙','♙','♙','♙'),
  (1,'♖','♘','♗','♕','♔','♗','♘','♖');

A chessboard in MySQL

Something to keep you warm during cold winter nights, or cool during hot summer days: a chessboard in MySQL!

Note: You should see chess pieces here below. If not, you’re not watching it using UTF-8, or get yourself a good browser!

CREATE TABLE `chessboard` (
  `x` tinyint unsigned NOT NULL,
  `a` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `b` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `c` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `d` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `e` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `f` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `g` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟'),
  `h` enum('','♔','♕','♖','♗','♘','♙','♚','♛','♜','♝','♞','♟')
) DEFAULT CHARSET=utf8;

Populating it with Python using MySQL Connector/Python (only piece of script shown):

def create_board(db):
    c = db.cursor()
    table = """CREATE TABLE chessboard (
        x tinyint unsigned not null,
        a ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',            
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        b ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        c ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        d ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        e ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        f ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        g ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F'),
        h ENUM('','\u2654','\u2655','\u2656','\u2657','\u2658','\u2659',
            '\u265A','\u265B','\u265C','\u265D','\u265E','\u265F')
        ) default charset='utf8'"""
    c.execute("DROP TABLE IF EXISTS chessboard")
    c.execute(table)

def set_start_position(db):
    c = db.cursor()
    # Numbers correspondent to the ENUM fields
    wdata = { 'x' : 1,
        'a': 4, 'b': 6, 'c': 5,
        'd': 3, 'e': 2,
        'f': 5, 'g': 6, 'h': 4}
    bdata = { 'x' : 8,
        'a': 10, 'b': 12, 'c': 11,
        'd': 9, 'e': 8,
        'f': 11, 'g': 12, 'h': 10}
    
    stmt = """UPDATE chessboard SET a=%(a)s,b=%(b)s,c=%(c)s,
        d=%(d)s,e=%(e)s,f=%(f)s,g=%(g)s,h=%(h)s WHERE x = %(x)s"""
    c.executemany(stmt, [wdata,bdata])
    
    stmt = """UPDATE chessboard SET a=7,b=7,c=7,d=7,e=7,f=7,g=7,h=7
        WHERE x = 2"""
    c.execute(stmt)
    stmt = """UPDATE chessboard SET a=13,b=13,c=13,d=13,e=13,f=13,g=13,h=13
        WHERE x = 7"""
    c.execute(stmt)

Now you can select the chessboard:

mysql> select * from chessboard;
+---+------+------+------+------+------+------+------+------+
| x | a    | b    | c    | d    | e    | f    | g    | h    |
+---+------+------+------+------+------+------+------+------+
| 8 | ♜    | ♞    | ♝    | ♛    | ♚    | ♝    | ♞    | ♜    |
| 7 | ♟    | ♟    | ♟    | ♟    | ♟    | ♟    | ♟    | ♟    |
| 6 |      |      |      |      |      |      |      |      |
| 5 |      |      |      |      |      |      |      |      |
| 4 |      |      |      |      |      |      |      |      |
| 3 |      |      |      |      |      |      |      |      |
| 2 | ♙    | ♙    | ♙    | ♙    | ♙    | ♙    | ♙    | ♙    |
| 1 | ♖    | ♘    | ♗    | ♕    | ♔    | ♗    | ♘    | ♖    |
+---+------+------+------+------+------+------+------+------+

The possibilities.. oh yeah!

Clusterious defined

clus·te·ri·ous |ˈkləstərēəs;|
adjective
highly pleasant to MySQL Support Engineers: A clusterious issue a day, keeps the spouse away.
· clusterful : a hard day.

DERIVATIVES
clusteriously adverb
clusteriousness noun

ORIGIN MySQL Support Team: via Sun Microsystems.

Clusterious
noun
a rare variety of MySQL Support Engineers originally cultivated in Sweden.

Resize and set default column size in Mac OS X Finder

This bugged me for a long time, and just today I googled for a solution. And it’s darn simple! To set a new default column size, size them while holding the Option (Alt) key. Open a new Finder window, et voila: default size changed!

There are lots of these little tricks in Mac OS (and I’m sure in other desktop GUIs). Fun to find one each day!