Skip to content

Commit

Permalink
[python,docker,mysql,sql] Update
Browse files Browse the repository at this point in the history
  • Loading branch information
progrhyme committed Aug 20, 2020
1 parent 332bb8e commit 1a0c7c7
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 83 deletions.
17 changes: 0 additions & 17 deletions content/ja/a/program/python/_index.md
Expand Up @@ -64,23 +64,6 @@ VS CodeのWindowをリロードすると、Pythonインタプリタのリスト
- [\[VS Code\] デフォルトで読み込む venv/virtualenv 環境のパス - てくなべ (tekunabe)](https://tekunabe.hatenablog.jp/entry/2018/12/28/vscode_venv_default_rolad)
- Windows: [【vscode】環境構築 #1-venvで仮想環境作成-【Python】 | ハチアンアーカイブズ](https://hachian.com/2019/09/19/vscode_venv/)

## How-to
### 文字列処理
#### 文字列検索 `in`, `not in`

```python
word = 'abcde'
'abc' in word #=> True
'x' in word #=> False

'abc' not in word #=> False
'x' not in word #=> True
```

参考:

- [Python3の文字列操作 \- Qiita](https://qiita.com/Kenta-Han/items/e64035e9c3e4ef08e394)

## Python3
### Python2との違い
#### 書式付き文字列
Expand Down
114 changes: 114 additions & 0 deletions content/ja/a/program/python/dojo.md
@@ -0,0 +1,114 @@
---
title: "道場"
linkTitle: "道場"
date: 2020-08-20T16:32:43+09:00
weight: 30
---

Road to Pythonian.
Pythonianを名乗るための基礎的なトピックを並べる予定。

## データ型
### 型変換

参考:

- [Python 3 で16進数とバイト列の相互変換 - Qiita](https://qiita.com/masakielastic/items/21ba9f68ef6c4fd7692d)

#### 整数 <=> 16進数文字列

```Python
# 整数 => 16進数文字列
format(0xabcd, 'x') #=> 'abcd'
'{:02x}'.format(0xabcd) #=> 'abcd'
'%02x' % 0xabcd #=> 'abcd'
hex(0xabcd) #=> '0xabcd'

# 16進数文字列 => 整数
int('0xabcd', 16) #=> 43981
int('abcd', 16) #=> 43981
```

#### 16進数文字列 <=> バイト列

```Python
import binascii
import codecs

# 16進数文字列 => バイト列
bytes.fromhex('abcd') #=> b'\xab\xcd'
binascii.unhexlify('abcd') # 同上
codecs.decode('abcd', 'hex') # 同上

# バイト列 => 16進数文字列
b'\xab\xcd'.hex() #=> 'abcd'
str(binascii.hexlify(b'\xab\xcd'), 'utf-8') # 同上
codecs.encode(b'\xab\xcd', 'hex') #=> b'abcd'
str(b'abcd', 'utf-8') #=> 'abcd'
```

## 文字列処理
### 文字列検索 `in`, `not in`

```python
word = 'abcde'
'abc' in word #=> True
'x' in word #=> False

'abc' not in word #=> False
'x' not in word #=> True
```

参考:

- [Python3の文字列操作 \- Qiita](https://qiita.com/Kenta-Han/items/e64035e9c3e4ef08e394)

### 正規表現

See [library#re]({{<ref "library.md#">}}#re)

Examples:

```python
import re

# マッチ
content = r'hellow python, 123, end.'
pattern = 'hel'

## compileしない
result = re.match(pattern, content)
## compileしてマッチ
repatter = re.compile(pattern)
result = repatter.match(content)

# 置換
re.sub('^H\w+', 'Good morning', 'Hello, world.')
## 大文字小文字を無視
re.sub('test', 'xxxx', 'Testing', flags=re.IGNORECASE)
```

参考:

- [分かりやすいpythonの正規表現の例 - Qiita](https://qiita.com/luohao0404/items/7135b2b96f9b0b196bf3)
- [Pythonで文字列を置換(replace, translate, re.sub, re.subn) | note.nkmk.me](https://note.nkmk.me/python-str-replace-translate-re-sub/)
- [python — re.compileせずに大文字と小文字を区別しない正規表現?](https://www.it-swarm.dev/ja/python/recompile%E3%81%9B%E3%81%9A%E3%81%AB%E5%A4%A7%E6%96%87%E5%AD%97%E3%81%A8%E5%B0%8F%E6%96%87%E5%AD%97%E3%82%92%E5%8C%BA%E5%88%A5%E3%81%97%E3%81%AA%E3%81%84%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE%EF%BC%9F/958186732/)

## ロギング
### 変数のダンプ

クラスのインスタンスなど、 `__dict__` attributeを持つオブジェクトであれば `vars` 関数で1階層のダンプができる:

```Python
class MyClass:
def __init__(self, val1, val2):
self.val1 = val1
self.val2 = val2

mc = MyClass(10, 20)
print(vars(mc)) #=> {'val1': 10, 'val2': 20}
```

参考:

- [Pythonでインスタンスの状態をダンプするにはvars()を使う - minus9d's diary](https://minus9d.hatenablog.com/entry/2015/08/02/204226)
16 changes: 16 additions & 0 deletions content/ja/a/program/python/library.md
Expand Up @@ -42,6 +42,22 @@ https://docs.python.org/ja/3/library/pprint.html

Rubyの `pp` 的なもの。

## re

https://docs.python.org/ja/3/library/re.html

正規表現操作モジュール。

See also [道場#正規表現]({{<ref "dojo.md">}}#正規表現)

### フラグ

RegexFlagのインスタンス(Python v3.6〜)

flag | 機能
------|-----
re.I, re.IGNORECASE | 大文字・小文字を区別しない

## urllib.request

https://docs.python.org/ja/3/library/urllib.request.html
Expand Down
19 changes: 19 additions & 0 deletions content/ja/a/program/python/spec.md
Expand Up @@ -219,6 +219,25 @@ https://docs.python.org/ja/3/tutorial/classes.html

https://docs.python.jp/3/tutorial/errors.html

Examples:

```Python
import sys

try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
```


## 書式付き文字列

```python
Expand Down
15 changes: 15 additions & 0 deletions content/ja/a/software/docker/_index.md
Expand Up @@ -25,6 +25,21 @@ weight: 90

- [Engine tutorials | Docker Documentation](https://docs.docker.com/engine/tutorials/)

## How-to
### dockerコンテナからホストOSのlocalhostにアクセスする

Docker for MacやDocker for Windowsなら、 `host.docker.internal` でアクセスできる。

Examples:

```sh
docker run -it --rm mysql mysql -hhost.docker.internal -uroot -p
```

参考:

- [Dockerのコンテナの中からホストOS上のプロセスと通信する方法 - Qiita](https://qiita.com/ijufumi/items/badde64d530e6bade382)

## Registry
### API

Expand Down
76 changes: 10 additions & 66 deletions content/ja/a/software/mysql/_index.md
Expand Up @@ -90,6 +90,16 @@ GRANT文やREVOKE文で指定できる権限。
-----|--------------|-----
PROCESS | グローバル | ユーザーが SHOW PROCESSLIST を使用してすべてのプロセスを表示できるようにする

### INFORMATION_SCHEMA

https://dev.mysql.com/doc/refman/5.6/ja/information-schema.html

メタデータDB

#### COLUMNS

https://dev.mysql.com/doc/refman/5.6/ja/information-schema-columns-table.html

## Features
### オンラインDDL

Expand Down Expand Up @@ -186,10 +196,6 @@ https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-21.html

- mysqldumpに `PROCESS` 権限が必要に(または `--no-tablespaces` オプション指定)

参考:

-

#### 8.0.16

https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-16.html
Expand All @@ -199,65 +205,3 @@ https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-16.html
参考:

- [MySQL 8.0.16 でテーブルスペース・REDO ログ/UNDO ログ・システムテーブル暗号化 - Qiita](https://qiita.com/hmatsu47/items/bae53fd0f6d09511732c)

## Cookbooks

TODO: 標準SQLについて別ページにまとめ、ここには差分だけ記す。

### CREATE TABLE

Examples:

```sql
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
```

### CREATE DATABASE

Examples:

```sql
CREATE DATABASE foo CHARACTER SET utf8mb4;
```

参考:

- [データベースを作成する(CREATE DATABASE文) | MySQLの使い方](https://www.dbonline.jp/mysql/database/index1.html)

### INSERT

Examples:

```sql
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
```

## Reference
### CREATE TABLE

- https://dev.mysql.com/doc/refman/5.6/ja/create-table.html

Tips:

- `CREATE TABLE B LIKE A;` でテーブルAと同じカラム構成のテーブルBを作れる。

参考:

- [MySQLの CREATE TABLE ... LIKE ... - 不思議なサービスをつくる新人プログラマーの日記](http://d.hatena.ne.jp/tnnsst35/20110604/1307181215 "MySQLの CREATE TABLE ... LIKE ... - 不思議なサービスをつくる新人プログラマーの日記")

### SHOW GRANTS

https://dev.mysql.com/doc/refman/8.0/en/show-grants.html

Examples:

```sql
-- 現在のユーザの権限を表示
SHOW GRANTS;
```
93 changes: 93 additions & 0 deletions content/ja/a/software/mysql/sql.md
@@ -0,0 +1,93 @@
---
title: "SQL"
linkTitle: "SQL"
date: 2020-08-20T11:54:48+09:00
---

SQLリファレンス。

標準SQLについてはSee [SQL]({{<ref "/a/sql.md">}})

## DML
### INSERT

Examples:

```sql
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
```

## DDL
### CREATE DATABASE

Examples:

```sql
CREATE DATABASE foo CHARACTER SET utf8mb4;
```

参考:

- [データベースを作成する(CREATE DATABASE文) | MySQLの使い方](https://www.dbonline.jp/mysql/database/index1.html)

### CREATE TABLE

- https://dev.mysql.com/doc/refman/5.6/ja/create-table.html

Examples:

```sql
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
```

Tips:

- `CREATE TABLE B LIKE A;` でテーブルAと同じカラム構成のテーブルBを作れる。

参考:

- [MySQLの CREATE TABLE ... LIKE ... - 不思議なサービスをつくる新人プログラマーの日記](http://d.hatena.ne.jp/tnnsst35/20110604/1307181215 "MySQLの CREATE TABLE ... LIKE ... - 不思議なサービスをつくる新人プログラマーの日記")

## その他
### SHOW BINARY LOGS

- https://dev.mysql.com/doc/refman/5.6/ja/show-binary-logs.html

binlogファイルを一覧表示する

```sql
SHOW BINARY LOGS
SHOW MASTER LOGS
```

### SHOW BINLOG EVENTS

- https://dev.mysql.com/doc/refman/5.6/ja/show-binlog-events.html

Syntax:

```sql
SHOW BINLOG EVENTS
[IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
```

NOTE:

- LIMIT句なしで指定すると全バイナリログの内容を出力するので、リソースを大量に消費する可能性がある

### SHOW GRANTS

https://dev.mysql.com/doc/refman/8.0/en/show-grants.html

Examples:

```sql
-- 現在のユーザの権限を表示
SHOW GRANTS;
```

0 comments on commit 1a0c7c7

Please sign in to comment.