Use Docker Machine to setup Docker environments

Docker Machine is a command-line tool to help you create Docker environments on your computers or cloud providers.

ref:
https://docs.docker.com/machine/

Installatino on Mac

$ curl -L https://github.com/docker/machine/releases/download/v0.3.0/docker-machine_darwin-amd64 > /usr/local/bin/docker-machine
$ chmod +x /usr/local/bin/docker-machine

Usage

$ docker-machine ls

$ docker-machine create --driver virtualbox dev

# list arguments for creating a VirtualBox VM
$ docker-machine create -d virtualbox

# see how to connect Docker to this machine
$ docker-machine env dev

# run this command to configure your shell
$ eval "$(docker-machine env dev)"

# then
$ docker info

Issues

Error response from daemon: client and server don't have same version (client : 1.19, server: 1.18)

$ docker-machine upgrade dev
Slack: Build a chat bot with Hubot in JavaScript

Slack: Build a chat bot with Hubot in JavaScript

Hubot is an scriptable chat bot framework created by GitHub. The newer version supports JavaScript (ES6+), no more CoffeeScript!

ref:
https://hubot.github.com/
https://slack.dev/hubot-slack/

Installation

$ npm install -g yo generator-hubot

$ mkdir codetengu-bot
$ cd codetengu-bot
$ yo hubot --adapter=slack

You could find all available adapters here:
https://hubot.github.com/docs/adapters/

Slack Token

The next thing you need is a Slack Bot Token (API Token) which looks like xoxb-xxx for your chat bot app. You could create a Hubot app in your Slack workspace to request a token, for instance https://vintachen.slack.com/apps/A0F7XDU93-hubot.

Otherwise, you could also create an universal Slack app, install it to your workspace. In your app settings, under "Install App" section, you are able to find OAuth Tokens for your chat bot. See https://api.slack.com/apps.

ref:
https://api.slack.com/bot-users

Development

$ HUBOT_SLACK_TOKEN=YOUR_SLACK_BOT_TOKEN \
./bin/hubot --adapter slack

I fork a script named hubot-reload-scripts to help you reload your scripts when developing them.
https://github.com/vinta/hubot-reload-scripts

Hear And Respond Messages

Writing your own script
https://hubot.github.com/docs/scripting/

// scripts/your_script.js
// Description
//   Do your shit
//
// Author:
//   Vinta Chen
//
// Commands:
//   * `restart <service>`* - Restart the service
//
const _ = require('lodash');

module.exports = (robot) => {
  robot.hear(/restart ([a-z0-9_\-]+)/i, (res) => {
    robot.logger.debug(`Received message: ${res.message.rawText}`);
    const [ serviceName ] = res.match.slice(1);
    res.send(`Restarting ${serviceName}`);
    doYourShit();
  });
};

Call Slack APIs

robot.slack.channels.info({'channel': res.message.rawMessage.channel})
  .then(apiRes => {
    const purpose = apiRes.channel.purpose.value;
    const topic = apiRes.channel.topic.value;
    res.send(`purpose: ${purpose}`);
    res.send(`topic: ${topic`);
  })
  .catch(apiErr => {
    robot.logger.error('apiErr', apiErr);
  });

ref:
https://slack.dev/hubot-slack/basic_usage#using-the-slack-web-api
https://api.slack.com/methods

Computer names for Sci-Fi maniac developers

Computer names for Sci-Fi maniac developers

The list is collected from books I read, movies I watched, and video games I played.

  • ADA (from Zone of the Enders: The 2nd Runner)
  • Albedo (from Hyperion) --> used for one of my code projects
  • Asurada (from Future GPX Cyber Formula)
  • BrainPal (from Old Man's War)
  • ctOS (from Watch Dogs)
  • Deep Thought (from The Hitchhiker's Guide to the Galaxy)
  • Eddie (from The Hitchhiker's Guide to the Galaxy)
  • EDI (from Mass Effect) --> used for one of my code projects
  • EVA-00 (from Neon Genesis Evangelion)
  • EVA-01 (from Neon Genesis Evangelion)
  • EVA-02 (from Neon Genesis Evangelion)
  • Fine Till You Came Along (from Culture: Look to Windward)
  • GERTY 3000 (from Moon)
  • GLaDOS (from Portal)
  • HAL 9000 (from 2001: A Space Odyssey) --> used for one of my code projects
  • Jane (from Ender's Game: Speaker for the Dead) --> used for My AirPods
  • JARVIS (from Iron Man)
  • KOS-MOS (from Xenosaga)
  • Limiting Factor (from The Culture: The Player of Games)
  • MAGI (from Neon Genesis Evangelion) --> used for my iPad
  • Melchizedek (from Gunnm Last Order)
  • Mike (from The Moon Is a Harsh Mistress)
  • Of Course I Still Love You (from The Culture: The Player of Games)
  • Pip-Boy 3000 (from Fallout) --> used for my iPhone
  • Project 2501 (from Ghost in the Shell)
  • Samantha (from Her)
  • Skynet (from The Terminator)
  • SAL-9000 (2010: The Year We Make Contact)
  • Sol-9000 (from Xenogears)
  • Sophon (from The Three-Body Problem)
  • Tachikoma (from Ghost in the Shell)
  • TARS (from Interstellar)
  • TechnoCore (from Hyperion) --> used for my Macbook Pro
  • Ummon (from Hyperion) --> used for one of my code projects
  • VIKI (from I, Robot)
  • Wheatley (from Portal)

ref:
https://en.wikipedia.org/wiki/List_of_fictional_computers
https://en.wikipedia.org/wiki/Artificial_intelligence_in_fiction

The templatetag {% include %} in Django

Loads a template and renders it with the current context.

# 會把 template context 也帶進去
{% include 'new_style/_bottom_action_btns' %}

{% for repost in recommend_creatives %}
    # 會把 repost 這個參數也帶進去!
    {% include 'new_style/creative_small.html' %}
{% endfor %}

# 但是你也可以額外指定要傳進去的 context
{% include 'new_style/_bottom_action_btns' with item=item repost=None %}

# 或是用 only 來限制只傳「顯式地指定的參數」進去
{% include 'new_style/_bottom_action_btns' with item=item only %}

ref:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-include

Python with Excel: xlrd, xlsxwriter and xlutils

Libraries

xlsxwriter 的文件寫得比較好

ref:
http://www.python-excel.org/
https://xlsxwriter.readthedocs.org/en/latest/
http://openpyxl.readthedocs.org/en/latest/

Usage

row 是橫排
column 是直排

Default format

import xlsxwriter

workbook = xlsxwriter.Workbook('label_copy.xlsx')

# default cell format
workbook.formats[0].set_font_size(12)
workbook.formats[0].set_text_wrap() # 要加上這個才能正常顯示多行
workbook.formats[0].set_align('vcenter')

ref:
https://xlsxwriter.readthedocs.org/en/latest/format.html

Multiple lines

lines_format = workbook.add_format({
    'align': 'left',
    'font_size': 12,
    'text_wrap': True,
    'valign': 'vcenter',
})

# 或是用 """多行"""
content = 'first line\nsecond line'
worksheet.write(0, 0, content, lines_format)

重點是要加上 text_wrap

ref:
http://stackoverflow.com/questions/15370432/writing-multi-line-strings-into-cells-using-openpyxl

Write to existing excel files

from xlutils.copy import copy as xlutils_copy
import xlrd


rb = xlrd.open_workbook('your_file.xls', formatting_info=True)
wb = xlutils_copy(rb)
ws = wb.get_sheet(0)
ws.write(0, 0, 'Hello World')

ref:
http://stackoverflow.com/questions/2725852/writing-to-existing-workbook-using-xlwt

Examples

https://xlsxwriter.readthedocs.org/en/latest/examples.html
http://www.crifan.com/python_append_new_data_into_existing_excel_xls_file/