About json format

We always want to see the json data pretty, so there are a lot of ways to achieve this.

online web service

json formmater and validator

chrome console

Right click and click the ‘inspect element’ or ALT + COMMAND + i, enter into the chrome console, write your json data and assign it to a variable, you can control it as a dict.

terminal

read data from input: echo ‘{“test”:1,”test2”:2}’ | python -mjson.tool

retrieve select data (In this case “test”’s value): echo ‘{“test”:1,”test2”:2}’ | python -c ‘import sys,json;data=json.loads(sys.stdin.read()); print data[“test”]’)

if the json data is in a file: python -mjson.tool filename.json

in python code

use system command def print_json(data):     import os

        cmd = "echo '%s' | python -mjson.tool" % data
        os.system(cmd) use json lib
print json.dumps(json.loads(content), indent=4, ensure_ascii=False)

(Comments)


Docopt - Command-line interface description language

When we want a user-friendly command-line interface

What we want are:

  • useful help and usage messages
  • easy to parse the arguments

Docopt helps you:

  • define interface for your command-line app
  • automatically generate parser for it

Seems other libs in python can handle this also, like: argparse, optparse, getopt, click.

But after I used all of those libs, I find docopt is the easiest way to handle this, and what your see is what your get.

Demo(naval_fate.py)

"""Naval Fate.

Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

"""
from docopt import docopt


if __name__ == '__main__':
    arguments = docopt(__doc__, version='Naval Fate 2.0')
    print(arguments)

When you want to get help and usage messages

python naval_fate.py –help

Naval Fate.
	
Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

And when we type: python naval_fate.py ship new ‘Going Merry’

The parsed arguments are:

{'--drifting': False,
 '--help': False,
 '--moored': False,
 '--speed': '10',
 '--version': False,
 '<name>': ['Going Merry'],
 '<x>': None,
 '<y>': None,
 'mine': False,
 'move': False,
 'new': True,
 'remove': False,
 'set': False,
 'ship': True,
 'shoot': False}

Write less code than others, and get all what we want, this is Docopt

Visit Homepage to get all the documents.

Read Github to know the amazing code.

Try Demo Source Code in your terminal.

(Comments)


Play with Alfred

Alfred 是一款 OSX 下的效率软件,类型系统的 Spotlight,但强大许多,默认快捷键:option + space

基础功能

能够让你快速的启动系统中所安装的应用程序,比如我输入 c,回车即可打开 chrome 浏览器,也可以上下选择不同的应用

app

能够无需打开浏览器,无需打开 google 主页,直接通过 google 搜索关键字

search

能够代替系统计算器做简单的运算

cal

可以很方便的听 iTunes 里的音乐

music

这些都是 Alfred 最基本的功能,免费版就可以使用,并且没有广告及时间限制

通过查看 Usage,发现我每天大概使用 30 次左右,主要用于打开应用

增强功能

他的强大远远不止这些,他的 powerpack 才是重点,列举几个我常用的 workflow

Adium: talk with someone with adium

adium

Datetime Format Converter: switch timestamp and datetime

datetime

Dev Doctor: docs(python, java, php, etc…)

dev

Douban: search movie, book, music in douban

douban

Weather: 查看天气情况

weather

Youdao: 有道翻译

youdao

还有非常多的 workflow,更多,速度使用吧!

REFs:

下载 alfred

下载 powerpack

借助 Alfred 2.0 的 Workflows 功能可以做哪些好玩的事情?

(Comments)