Skip to main content

Pyenv のインストールと設定

· 2 min read
ひかり
Main bloger
  1. Pyenv をクローンする

    Pyenv のリポジトリーをクローンする。 ディレクトリーは ~/.pyenv がおすすめ。

git clone https://github.com/pyenv/pyenv ~/.pyenv

    

0. 高速化のための Bash 拡張をコンパイル

高速化のための Bash 拡張のコンパイルが可能。
コンパイルが失敗しても正常に動作する。

```sh
cd ~/.pyenv && src/configure && make -C src
  1. 設定 (bash)

    パスとかの設定。

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile echo 'eval "$(pyenv init --path)"' >> ~/.profile export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" echo -e 'if shopt -q login_shell; then'
'\n export PYENV_ROOT="$HOME/.pyenv"'
'\n export PATH="$PYENV_ROOT/bin:$PATH"'
'\n eval "$(pyenv init --path)"'
'\nfi' >> /.bashrc echo -e 'if [ -z "$BASH_VERSION" ]; then'
'\n export PYENV_ROOT="$HOME/.pyenv"'
'\n export PATH="$PYENV_ROOT/bin:$PATH"'
'\n eval "$(pyenv init --path)"'
'\nfi' >>
/.profile



# Python 環境のインストール

## 依存するライブラリーのインストール
```sh
sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev -y

インストール可能な環境を調べる

pyenv install -l

インストールする

コンパイル時間が長いので待つ。

CONFIGURE_OPTS="--enable-shared" pyenv install 3.9.5

バージョンの確認

pyenv versions

バージョンの切り替え

pyenv global 3.9.5

ガウシアンフィルターでノイズ除去 (Python / Scipy)

· One min read
ひかり
Main bloger

例として、1[Hz] の sin 波を信号とする。 平均 0、標準偏差 0.5 の正規分布から乱数を作成したノイズを信号に加える。

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter1d

t = np.arange(1000) / 100
s = np.sin(2*np.pi*t)
noise = np.random.normal(0, 0.5, size=len(t))
x = s + noise

plt.plot(t, x, label="+noise")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()

pyplot

標準偏差 5 のガウシアンフィルターをかける。 標準偏差が大きいほど、滑らかになるが元の信号と外れる。

y = gaussian_filter1d(x, 5)
plt.plot(t, y, label="filtered")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()

pyplot

Numo::NArray でドット積

· One min read
ひかり
Main bloger
require "numo/narray"

実数ベクトルのドット積

a = Numo::NArray[4, -1, 2]
b = [2, -2, -1]
c = a.dot b
8

複素数ベクトルのドット積

a = Numo::NArray[1+1i, 1-1i, -1+1i, -1-1i]
b = [3-4i, 6-2i, 1+2i, 4+3i]
c = a.conj.dot b
(1.0-5.0i)

複素数とそれ自身のドット積

d = a.conj.dot a
(8.0+0.0i)

行列のドット積

a = Numo::NArray[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
c = (a * b).sum(0)
Numo::Int64#shape=[3]
[54, 57, 54]

行ベクトルとしてドット積を求める

c = Numo::NArray[(a * b).sum(1)].transpose
Numo::Int64(view)#shape=[3,1]
[[46],
[73],
[46]]

era.js

· 2 min read
ひかり
Main bloger

JavaScript で使える和暦のライブラリーを作りました。

https://himeyama.github.io/era.js/era.js

let date = new Era()
date.getWareki() // "令和X年X月X日" (今日の日付)

date = new Era("2020-1-1")
date.getWareki() // "令和2年1月1日"

date.getWareki("西暦") // "2020/01/01"

date.getDateAry() // ["令和", 2, 1, 1]

Era.date2wareki(id) // id を指定して要素を和暦に変換

getWareki() の引数

calshorttype表示
"和暦"true0
1
2
3
4
5
false0
1
2
3
"西暦"true0
1
2
3
4
5
false0
1
2
3
4

JavaScript 便利ワザ

· One min read
ひかり
Main bloger

添字を添えて for で繰り返し

for([i, e] of ["a", "b", "c"].entries()){
console.log(i, e)
}

forEach でも

["a", "b", "c"].forEach((e, i) => {
console.log(e, i)
})

長さ n の配列を作成

const n = 10
let ary = [...Array(n)].map((_, i) => i)
// もしくは
ary = Array.from({length: n}).map((_, i)=> i)

配列の和

let ary = [1, 2, 3, 4, 5]
ary.reduce((_, v) => _ + v)

youtube-dl のインストールと使い方

· One min read
ひかり
Main bloger

注意

  • 某 Tube からのダウンロードは利用規約に反する場合があること。
  • 違法にアップロードされた動画をダウンロードするのは違法であること。
  • 私的利用のみにすること。

pip3 のインストール

sudo apt update -y && sudo apt install python3-pip -y

youtube-dl のインストール

apt 経由でインストールすると、何故かうまくいかなかったりしたので pip3 でインストールする。

pip3 install youtube-dl

ダウンロード

youtube-dl が実行できない場合は、パスが通っているか確認する。

youtube-dl https://xxx.xxxxxxx.xxx/?xxx=xxxxxxxx
[XXXXXXX] xxxxxxxxxxx: Downloading webpage
[download] Destination: xxxxxxxxxxxx.mp4
[download] 100% of 50.00MiB in 00:05

アイコン (.ico) の作り方

· One min read
ひかり
Main bloger
  1. アイコンを作成します。

    icon の作り方

  2. 1624324864128256 の7種類の大きさの PNG 画像を用意します。 icon の作り方

  3. convert アイコンでアイコンを作成します。 icon の作り方

convert *.png favicon.ico

Electron アプリ作成

· One min read
ひかり
Main bloger

作業ディレクトリーの作成

mkdir test-electron-app
cd test-electron-app

package.json の作成

npm init -y

electron のインストール

npm i --save-dev electron

index.js の作成

const { app, BrowserWindow } = require("electron")
const path = require("path")

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js")
}
})

win.loadFile("index.html")
}

app.whenReady().then(() => {
createWindow()

app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})

index.html の作成

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
<p>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</p>
</body>
</html>

package.json の修正

scripts の部分を書き換える。

"scripts": {
"start": "electron ."
}

アプリの実行

npm start

Chart.js の使い方メモ

· One min read
ひかり
Main bloger

Chart.js で線をプロットする

let x = nj.arange(-50, 50).divide(10)
let ctx = document.getElementById('chart').getContext('2d');
ctx.canvas.parentNode.style.height = "240px";
ctx.canvas.parentNode.style.width = "320px";
new Chart(ctx, {
type: 'line',
data: {
labels: x.selection.data,
datasets: [
{
label: "sin(x)",
data: nj.sin(x).selection.data,
fill: false,
borderColor: "#2196F3",
radius: 0,
},
{
label: "cos(x)",
data: nj.cos(x).selection.data,
fill: false,
borderColor: "#FF9800",
radius: 0,
}
]
},
options: {
responsive: true,
}
});

Ruby の作り方

· One min read
ひかり
Main bloger

例として3を返すプログラムを作る。

まず、C でソースコードを書く。

// three.c
#include <ruby.h>

static VALUE int_three(void){
return INT2NUM(3);
}

void Init_three(void){
rb_define_singleton_method(rb_cInteger, "three", int_three, 0);
}

Makefile を作成するための スクリプトを作成。

# extconf.rb
require 'mkmf'
create_makefile "three"

Make

$ make

作成したプログラムを呼び出す Ruby スクリプトを書く。

# main.rb
require "./three"
p Integer.three

実行

$ ruby main.rb
3