As rookie

ルーキーインフラエンジニアがインフラのこと以外も結構書いてしまうブログ

vimでESCキー以外でノーマルモードにする (1日坊主)

この記事はsolo_advent_calender 2016年12月1日のものです。

こんにちは ショート advent calender1日目です。

.vimrcにimap <キーバインド> <esc>

書けば完了

cd ~
vim .vimrc
#.vimrcの一番下に1行追加
imap <c-f> <esc>

これで、ctrl + f を押せばノーマルモード(何もしてないモード) に戻ります。

ctrl + j の方が押しやすそうと思っていたが、どうやらインサートモード中に ctrl + j で改行できるっぽい。

というかimap の記述す前に試してたら改行された!

クリスマスまで残り24日

ansibleのmysql_replicationモジュールでエラー??

ansibleのmysql_replicationモジュールで

Sending passwords in plain text without SSL/TLS is extremely insecure

というエラーがでました。

結論:解決方法は2.2.0 以上にバージョンをあげることです。

以下は戦った記録

戦記

いや、ちょっと待った。これMySQL上だとWarningだぞ。間違ってないぞ?ということで

MySQL レプリケーション」とかで調べてみると

MHAにはmasterとslaveがお互いに公開鍵認証できる状態じゃないといけないみたいな情報がありますが、これはMHAの場合らしいので単純なレプリケーションでは必要なさそう。

恐ろしいのはansibleから返ってくるエラーメッセージ

changed : false faild : true msg : Sending passwords in plain text without SSL/TLS is extremely insecure

msgがエラーメッセージじゃないのにfaild : true

なんやねんMySQL

って思っていると

こういう自分の中で解決できないことは自分を疑うことですね。

ansibleのissueに同じことがでてきているってことはansible側の普通のバグっすね

github.com

上記issueのリンクではワークアラウンドを示している部分もありますが

github.com

でマージされている情報もあるので

最新に上げればとりあえず解決していることが確認できます。

よってバージョン上げることがもっとも単純な解決方法だということがわかります。

トラブルシューティング能力をもっとあげていきたいですね。

pythonでmp3の再生時間を取得する

こんにちは。

持っている音楽(mp3)の再生時間を調べたいときありますよね?

python mp3」

で検索するとでてくるとは思います。pygame とか

今回は再生する気がありません。

どうやらmutagen というモジュールがあるらしい

https://mutagen.readthedocs.io/en/latest/

とっても簡単。pipでインストールできる。

pip install mutagen

vim ongaku.py

from mutagen.mp3 import MP3
audio = MP3('example.mp3')
print (audio.info.length)
python ongaku.py
2.1149886621315193[f:id:shigeru-mokicks:20161118032426p:plain]

f:id:shigeru-mokicks:20161118032426p:plain

finderで確認すると。2秒。

これでmp3の長さを取得することができました!

ちなみにこれはcentOS で試しまし!

それでは!

Djangoのモデル作ってるときにタイプミスしてた。

こんにちは。

最近Djangoでwebアプリケーションを作りたいということで、Djangoのチュートリアルをしています。

チュートリアルと進めていてモデルを作成してデータベースをマイグレーションするという章があります

はじめての Django アプリ作成、その2

です。

問題のタイプミスがこちら `polls/models.py

from django.db import models


class Question(models.Model):
    qeustion_text = models.CharField(max_length=200) # クェウッションってなんぞ?
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

それに気づかずドンドンチュートリアルを進めていってました。

python manage.py makemigration polls もして、 python manage.py sqlmigrate polls 0001もした よし!確認OK!

python manage.py migrate よしできた!

確認するぞ!!

python manage.py shell
>>> from polls.models import Question, Choice   # Import the model classes we just wrote.
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())

は?エラーでるやんけ!invalid?は?なんでや

そこで以前紹介した

>>> dir(Question)

qeustionってなんやねん 早速models.py 編集さっそくmakemigrations

`python manage.py makemigration polls` 
`python manage.py sqlmigrate polls 0001`

変更されてない。

polls/migrationsを確認しにいくと

0002_auto_20161111_1739.py あ、新しくできるんだ

Djangoチュートリアルと読むと

migrate コマンドはすべての適用されていないマイグレーション(Djangoはデータベース内のdjango_migrationsと呼ばれる特別なテーブルを利用してどれが適用されているかを追跡しています)を捕捉してデータベースに対してそれを実行します - 重要なのは、モデルに対して行った変更はデータベースのスキーマに同期するということです。

なるほど! じゃあもうmigrateしちゃおう

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying polls.0002_auto_20161111_1739... OK

更新された感あるぞ

python manage.py shell

>>> from polls.models import Question, Choice
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
>>> q.save()
>>> q.question_text
"What's new?"

更新された!

重要なのはモデルに対して行った変更はデータベースのスキーマに同期するということです。

また学びサンクス

pythonでつくったクラスのフィールドとかの確認

pythonインスタンスを作成するときに

h = Hoge(hoge_field='hoge')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'hoge_field' is not defined

とかなったことありませんか? あるあるですよね。

おそらくクラス変数の名前が想定しているものと違うものになっているのだろうと考えます。

クラスを定義しているところを確認すれば良いのですが、「名前何になってるんだ」ってすぐに確認する方法です。

>>> from tmp import Hoge
>>> dir(hoge)

これでクラスの変数、メソッドの一覧を確認できます。

オブジェクトの内容を表示してくます。

dir() の戻り値はリストです。ですので、

>>> 'hoge_field' in dir(Hoge)
True

で存在するか確認できます

文字列などのオブジェクトも同じように

# 文字列の特殊変数、メソッドを表示
>>> str = 'text'
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Djangoのモデルの変数名わからなくなったときに実行するとできたので、覚書です。 * tmp/models.py

from django.db import models

class Hoge(models.Model):
    hoge_field = models.CharField(max_length=200)

モデルの内容

python manage.py makemigrations
python manage.py shell
>>> from tmp import Hoge
>>> dir(Hoge)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_ordering', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_set_pk_val', 'check', 'choice_set', 'clean', 'clean_fields', 'date_error_message', 'delete', 'from_db', 'full_clean', 'get_deferred_fields', 'get_next_by_pub_date', 'get_previous_by_pub_date', 'id', 'objects', 'pk', 'prepare_database_save', 'hoge_field', 'refresh_from_db', 'save', 'save_base', 'serializable_value', 'unique_error_message', 'validate_unique']

モデルの変数を確認するときに利用してみよう

以上