`(blog ,garaemon)

ポップカルチャーを摂取して、コードを吐き出す機械

micropythonのusocketで大きなファイルをHTTP越しに読む

micropythonを動かす環境はm5stackといったメモリが貧弱であることが多く, 普段は気にならないサイズでもMemory Allocation Errorが発生して読み込めないことがある.

micropythonのurequestsパッケージは便利だけど、データを逐次的に読み出すインタフェイスがないのが問題. なので直接usocketオブジェクトに触ってサイズを区切って読み出すようにすればいい. 最後に忘れずにrequestをcloseするのを忘れないように。これを忘れるとmemory allocation errorを引き起こす.

from m5stack import lcd
import urequests

def download_to_file(image_url, filename, step_size=1024):
    r = urequests.get(image_url)
    try:
        with open(filename, 'wb') as f:
            while True:
                c = r.raw.read(step_size)
                if c:
                    f.write(c)
                else:
                    return
    except Exception as e:
        lcd.println('Exception(download_image): ' + str(e))
        raise e
    finally:
        r.close()

新iPad(2018年モデル)でApple Pencilをつけられるカバー

iPad (2018年モデル)を購入したので、Apple Pencilをつけられるカバーということで以下のものを購入して利用している.

https://www.amazon.co.jp/gp/product/B07C4RZMS9

Apple Pencilもしっかり収納できて良い感じ. 少し重いのと、Apple Pencilを立てる穴はちょっと使いにくいかも.

javascript版jupyterのようなobservable

beta.observablehq.com

observableはjavascript版のjupyterのようなサービス. d3.jsを利用した美しい可視化が特徴的.

どうやら今年の1月31日にサービスインしたらしい.

ここから基本的なサンプルを色々見ることができる. jupyterと正面から勝負するのは大変そうな気もするけど、出力となるノートはobservableの方がきれいなので注目している.

ちなみに、d3.expressというURLからも飛ぶことができる.

emacsでカーソル位置の単語をispellの辞書に追加する

emacsでカーソル位置の単語をispellの辞書に追加するelispコード

(require 'thingatpt)

(defun add-word-to-ispell-dictionary ()
  "Add word to dictionary file for ispell."
  (interactive)
  (let ((user-dictionary-file (expand-file-name "~/.aspell.en.pws")))
    (let ((buf (find-file-noselect user-dictionary-file)))
      (if (not (file-exists-p user-dictionary-file))
          (with-current-buffer buf
            (insert "personal_ws-1.1 en 0\n")
            (save-buffer)))
      (let ((theword (thing-at-point 'word)))
        (if theword
            (with-current-buffer buf
              (goto-char (point-max))
              (insert (format "%s\n" theword))
              (save-buffer))))
      )))

(global-set-key "\M-." 'add-word-to-ispell-dictionary)

emacsでカーソル位置の数字を上下させる

emacsでカーソル位置の数字を上下させるemacs lispコード. どこかから拾ってきたのかもしれないもの. 意外と便利.

(defun increment-number-at-point ()
  "Increase number at current cursor."
  (interactive)
  (skip-chars-backward "0123456789")
  (or (looking-at "[0123456789]+")
      (error
       "No number at point"))
  (replace-match (number-to-string (1+ (string-to-number (match-string 0))))))

(defun decrement-number-at-point ()
  "Decrease number at current cursor."
  (interactive)
  (skip-chars-backward "0123456789")
  (or (looking-at "[0123456789]+")
      (error
       "No number at point"))
  (replace-match (number-to-string (1- (string-to-number (match-string 0))))))

(global-set-key (kbd "C-c C-+") 'increment-number-at-point)
(global-set-key (kbd "C-c C--") 'decrement-number-at-point)

ambie + TT-BR06が悪くない

ambieは耳をふさがないイヤホンということで周囲の音が聞きやすいということを売りに出されているイヤホンだ.

ambie.co.jp

もともとはイヤホンジャックを備えている有線イヤホンとして販売されていた.

ambie.co.jp

先日Bluetoothによって無線化された新製品が発売された.

ambie.co.jp

しかしこの新製品、いくつかの残念な点がある.

個人的には旧有線ambieにtaotronicsのTT-BR06というBluetooth レシーバをつけて利用している.

TT-BR06 Bluetoothレシーバー

このTT-BR06という製品の素晴らしいところは

  • 連続再生時間が15時間(!)
  • 充電しながら利用できる

という2点である. また、Bluetooth 4.1なのでBluetooth 3.0の無線ambieに比べるとその点においても良い.