2016年10月3日月曜日

開発環境

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 9(Working with External Services)、51(Pushing Notes to Firebase)を取り組んでみる。

51(Pushing Notes to Firebase)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import json
import datetime

import firecall

if len(sys.argv) < 1:
    print('usage: mynotes.py <command> [note]')
    sys.exit(1)

cmd = sys.argv[1]

URL = '<URL>'
AUTH = '<Secret>'

f = firecall.Firebase(URL, auth=AUTH)
b = f.get_sync(point='/', auth=AUTH)
if b == b'null':
    data = {}
else:
    s = b.decode('utf-8')
    data = json.loads(json.loads(s))
if cmd == 'new':
    text = ' '.join(sys.argv[2:])
    d = {datetime.datetime.now().isoformat(): text}
    data.update(d)
    s = json.dumps(data)
    f.put_sync(point='/', data=s, auth=AUTH,
               callback=lambda: print('Your note was saved'))
elif cmd == 'show':
    for k, v in data.items():
        print('{0} - {1}'.format(k[:10], v))

入出力結果(Terminal, IPython)

$ ./mynotes.py show
$ ./mynotes.py new Learn how to invert binary trees
$ ./mynotes.py show
2016-10-03 - Learn how to invert binary trees
$ ./mynotes.py new Notetaking on the command line is cool.
$ ./mynotes.py show
2016-10-03 - Notetaking on the command line is cool.
2016-10-03 - Learn how to invert binary trees
$ 

0 コメント:

コメントを投稿