json, bson, sqlite3 IOの実験メモ

Windows 10 Python 3.6.6 bson==0.5.7 SSD JSON import json import random import time file = 'test.tmp' N = 500 # Generate start = time.time() entries = [] for i in range(N): title = ''.join([ chr(random.randint(ord('あ'), ord('ん')+1)) for i in range(32) ]) body = ''.join([ chr(random.randint(ord('あ'), ord('ん')+1)) for i in range(30000) ]) entries.append({ 'id': i, 'title': title, 'body': body, }) data = { 'entries': entries, } end = time.time() print('Generate: %.1fs' % (end - start, )) Generate: 38.6s import os # Write start = time.time() with open(file, 'w', encoding='utf-8') as fp: json.dump(data, fp, ensure_ascii=False) end = time.time() print('Write: %.3fs' % (end - start, )) print('%.2fMB' % (os.path.getsize(file)/(1024**2), )) # Read start = time.time() with open(file, 'r', encoding='utf-8') as fp: data = json.load(fp) end = time.time() print('Read: %.3fs' % (end - start, )) Write: 0.252s 42.98MB Read: 0.214s BSON pip install bson import bson # Write start = time.time() s = bson.dumps(data) print('%.2fMB' % (len(s)/(1024**2), )) dumpEnd = time.time() with open(file, 'wb') as fp: fp.write(s) end = time.time() print('Write: %.3fs (Dump: %.3fs)' % (end - start, dumpEnd - start)) # Read start = time.time() with open(file, 'rb') as fp: s = fp.read() loadStart = time.time() data = bson.loads(s) end = time.time() print('Read: %.3fs (Load: %.3fs)' % (end - start, end - loadStart)) 42.98MB Write: 0.830s (Dump: 0.638s) Read: 0.156s (Load: 0.095s) SQLite3 import sqlite3 if os.path.exists(file): os.unlink(file) start = time.time() with sqlite3.connect(file) as db: cur = db.cursor() cur.execute( 'CREATE TABLE entries(id INTEGER AUTO INCREMENT, title TEXT, body TEXT)' ) for entry in data['entries']: cur.execute( 'INSERT INTO entries VALUES(?,?,?)', ( entry['id'], entry['title'], entry['body'], ), ) end = time.time() print('Write: %.3fs' % (end - start, )) start = time.time() with sqlite3.connect(file) as db: cur = db.cursor() results = [] for row in cur.execute('SELECT * FROM entries'): results.append(row) end = time.time() print('Read: %.3fs' % (end - start, )) print('%.2fMB' % (os.path.getsize(file)/(1024**2), )) db.close() Write: 1.093s Read: 0.243s 43.22MB

2018年12月15日 · aoirint

HOG特徴量の計算(Scikit-image)

pip install scikit-image from skimage.feature import hog import numpy as np size = 32 channel = 1 image = np.random.randn(size, size, channel) feature = hog(image) print(feature.shape) print(feature) デフォルト(勾配方向数9, セルサイズ8, ブロックサイズ3)では32x32の画像に対して324次元のHOG特徴量が出力される。 出力例 (324,) [0.01877925 0.00125122 0.00899619 0.00630656 0.01330613 0.0150924 ... 0.00763393 0.01668535 0.01852621 0.01204107 0.00433798 0.01147866] 画像からHOG特徴量の抽出 - Qiita https://web.archive.org/web/20170917002713/http://qiita.com/mikaji/items/3e3f85e93d894b4645f7 ドキュメント: https://scikit-image.org/docs/dev/api/skimage.feature.html#skimage.feature.hog

2018年12月15日 · aoirint

PythonからPaSoRiを使って交通系ICカードのIDmを読む

環境 Ubuntu 18.04 (VirtualBox on Windows 10) Python 2.7.15rc1(nfcpyはPython3非対応のため) Sony PaSoRi RC-S380 Suica (2019/12/19 追記)未検証ですがnfcpyがv1.0.0でPython3対応したみたいです。https://github.com/nfcpy/nfcpy/issues/47#issuecomment-499693493 セットアップ まずPaSoRiを接続する。PaSoRiをUSBポートに挿して、VirtualBox仮想マシンの設定からUSB、USB デバイスフィルターにSONY RC-S380/Pを追加。 $ lsusb Bus 001 Device 004: ID 054c:06c3 Sony Corp. ... これでOK。 次にnfcpyのインストール(※Python2に導入すること、virtualenvを使うといいのでは)。 pip install nfcpy いくらか準備が必要なので、python -m nfcを実行する。 $ python -m nfc No handlers could be found for logger "nfc.llcp.sec" This is the 0.13.5 version of nfcpy run in Python 2.7.15rc1 on Linux-4.15.0-33-generic-x86_64-with-Ubuntu-18.04-bionic I'm now searching your system for contactless devices ** found usb:054c:06c3 at usb:001:004 but access is denied -- the device is owned by 'root' but you are 'USERNAME' -- also members of the 'root' group would be permitted -- you could use 'sudo' but this is not recommended -- better assign the device to the 'plugdev' group sudo sh -c 'echo SUBSYSTEM==\"usb\", ACTION==\"add\", ATTRS{idVendor}==\"054c\", ATTRS{idProduct}==\"06c3\", GROUP=\"plugdev\" >> /etc/udev/rules.d/nfcdev.rules' sudo udevadm control -R # then re-attach device I'm not trying serial devices because you haven't told me -- add the option '--search-tty' to have me looking -- but beware that this may break other serial devs Sorry, but I couldn't find any contactless device 一般ユーザーの場合こんなメッセージが出るので、指示通りデバイスをplugdevグループに割り当てる。 ...

2018年8月30日 · aoirint

SSD換装時のOS移動(スケールダウン)

容量の小さなSSDに換装する(HDD 500GBからSSD 250GB)。 対象のHDD, SSD以外でUbuntuを起動(USBブートなど) GPartedで必要なパーティションをHDDからSSDにコピー(Boot, OSとか) コピー先でパーティションを右クリック、Manage Flagsからコピー元のパーティションとFlagsを一致させる Ubuntuとのデュアルブートにしていたため、EFIをマウントして"ubuntu"を削除

2018年8月29日 · aoirint

プライベートリポジトリに対してgit cloneがNot Found吐くとき(複数アカウント運用)

環境 Windows 10 原因 Windows 資格情報に対象のリポジトリのあるアカウント以外の認証情報が記録されていた。 解決 資格情報マネージャー(Credential Manager)を開いてWindows 資格情報、汎用資格情報からgitを削除。 これでclone時にログイン用のブラウザが表示される(GitHub)。 参考 GitHubでクローンしてRepository not foundになった | ねこブログ Solved: git clone is not working for a private repo - GitHub Community Forum

2018年8月29日 · aoirint

Windows10でCortanaの検索結果から"Recent"を消去する

17/11/05 追記:この記事でやっているのは履歴の消去のみ。記録を無効にするものではない。 Winキー、アプリケーション名入力、Enterでアプリケーションを開くのが便利だが、Cortanaは"最近開いたファイル" = “Recent"を記録する。 この"Recent”、右クリック、“この一覧から消去"では消えなかった。Cortana(検索)を開き直すと復活する。アプリケーションを右クリックして出るジャンプリストも同じだ。 https://account.microsoft.com/privacy/cortana ここから"Cortana データを消去する"で消すことができた。

2017年10月29日 · aoirint

VS2017でOpenCVをビルドする

What Visual Studio 2017でOpenCVをビルドしたい。 Environment Windows 10 Home Visual Studio 2017 CMake 3.9.3 How Download Sources git clone https://github.com/opencv/opencv.git または OpenCV library CMake CMake CMake-GUIを起動、上部のテキストフィールドにソースのパスと出力先パスを入力する。 ソースはgit cloneしてきたならopencvフォルダ、CMakeLists.txtのあるところ。出力先は適当にbuildとか。 Configureを押してVisual Studio 15 2017 Win64を選択、“Finish”。設定が始まる。 終わったら"Generate"。出力先にOpenCV.slnが生成される。 17/11/05追記:“Generate"の前にBUILD_*の設定を変えると出力されるlibが変わる。 Visual Studio OpenCV.slnを開いて、構成をReleaseに変えてCMakeTargets/INSTALLをビルド。 17/11/05追記:_dのつくデバッグ用ライブラリを生成したいならDebug構成でビルドすればよい。 正常に終われば、出力先のinstall\x64\vc15\libにopencv_core330.libなどが出力されている。 VS2017でOpenCVを使う(Win pack) install\x64\vc15\binにPATHを通し、上のページに従ってVSプロジェクトを設定(ライブラリは適宜必要なもの)する。 Reference OpenCV: Installation in Windows

2017年9月24日 · aoirint

VS2017でOpenCVを使う(Win pack)

What Visual Studio 2017でOpenCVを使いたい。 Environment Windows 10 Home Visual Studio 2017 How OpenCVのダウンロード OpenCV library OpenCV libraryのReleasesから最新の"Win pack"をダウンロード。 “Win pack"は自己展開exeになっているので、適当なディレクトリを指定して展開。 VSプロジェクトの設定 新規にプロジェクトを作る場合、Visual C++から空のプロジェクトを作成。 ソリューションエクスプローラからプロジェクトのプロパティを開き、“VC++ディレクトリ"のツリーに移る。 “インクルードディレクトリ”、“ライブラリディレクトリ"を編集し、展開ディレクトリ以下のbuild\includeをそれぞれに追加する。 “リンカー"のツリーに移り、“追加のライブラリディレクトリ"を編集し、展開ディレクトリ以下のbuild\x64\vc14\libを追加する。 “リンカー"のツリーから、“入力"を選択し、“追加の依存ファイル"に必要なライブラリ(.lib)を追加する。Win packにはopencv_world330d.lib(デバッグ)とopencv_world330.lib(リリース)しかないので、構成に対応する方を追加。 PATHの設定 展開ディレクトリ以下のbuild\x64\vc14\binにPATHを通す。 サンプル #include <iostream> #include <opencv2\opencv.hpp> using namespace cv; int main() { Mat mat(400, 400, CV_8UC3, Scalar(255, 128, 0)); imshow("mat", mat); waitKey(0); return 0; } 水色のウインドウが表示されればOK。ウインドウをアクティブにして何かキーを押せば終了する。

2017年9月24日 · aoirint

MSYS2でPython3を使う

What MSYS2でPython3を使いたい。 Environment Windows 10 Home How $ pacman -S pythonで最新のPythonが入る。 $ pacman -S python3-pipでPython3のpipが入る。 例えばPython 3.6.2を入れたとして、実行するにはpython、python3かpython3.6。 Appendix バージョン合わせ export PATH="/mingw64/bin:${PATH}" 上のように.bash_profileをいじってMinGWのbinをPATHに入れて動かしていると、pythonのバージョンが合わないときがある。 pythonで最新のPythonを動かしたいときは、 export PATH="${PATH}:/mingw64/bin" とすれば動く。

2017年9月24日 · aoirint

エクスプローラからMSYS2を開く + メニュー項目の追加

What MSYS2をエクスプローラから開きたい Environment Windows 10 Home How u_msys2.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell\u_msys2] @="MSYS2 Here" [HKEY_CLASSES_ROOT\Directory\Background\shell\u_msys2\command] @="C:\\msys64\\msys2.exe" 上のようにレジストリキーを追加する。 #CHERE_INVOKING=1 #MSYS2_PATH_TYPE=inherit msys2.iniで上のコメントアウトを外し、起動時にパスがエクスプローラ側のフォルダになるようにする(どこから持ってきてるんだ? 作業フォルダ?)。 Appendix エクスプローラの右クリックメニューに任意の項目を追加する 以下のレジストリキーに特定のサブキーを追加する。 対象 キー名 フォルダの背景 HKEY_CLASSES_ROOT\Directory\Background\shell フォルダ HKEY_CLASSES_ROOT\Folder\shell 任意のファイル HKEY_CLASSES_ROOT\*\shell ファイル *.hoge HKEY_CLASSES_ROOT\.hoge\shell 追加するサブキーはhoge、hoge\commandの2つ。hogeには自由な名前を付けられるっぽい。 hogeには、既定値にメニューの名称を設定。 hoge\commandには、既定値にアプリケーションパスを設定。これは、PATHを通していても無効かもしれない。%Vは"メニューを開いた"、つまり選択したパスで置換される。 Sample 以下はフォルダの右クリックメニューにcmd.exe /k dirを実行する項目"TEST"を追加するサンプル。 u_dir.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Folder\shell\u_dir] @="TEST" [HKEY_CLASSES_ROOT\Folder\shell\u_dir\command] @="cmd.exe /k dir" こちらは同キーを削除する。 !u_dir.reg Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\Folder\shell\u_dir] Reference 作業メモ:msys2インストール、エクスプローラーから起動 - milk_spoonのブログ エクスプローラの右クリックメニューをカスタマイズする - Qiita .reg ファイルを使用してレジストリ サブキーおよび値を追加、変更または削除する方法 Windowsのシンボリックリンクとジャンクションとハードリンクの違い:Tech TIPS - @IT

2017年9月24日 · aoirint