pythonその9 データ型
リフレクションはうまくできなかったので、先にデータ型について学習。公式見るのがしんどくなったので、他の人を参考にさせてもらいました。
Pythonらしいコードの書き方 – Kesin’s diary
それで、出来上がったのがコレ。エラー時の挙動とか書かないとサンプルにもなんないなぁ。
Related posts
python
rubyできればいいかなと思ってたけど、そんなにrubyも究めることもないだろうし、pythonもやってみようと思った。ということで、githubにお勉強用のリポジトリをこさえた。assemblaに置いてたコードもgithubにまとめようかなー。 昔触った事はあるはずだが、class定義して呼び出すだけで悪戦苦闘… メソッドの後ろに”():”が必要だったり、引数なしの場合でも”()”じゃなくて”(self)”としないと、自分の作ったexecute()メソッドがこんなエラーになったり。前途多難ですな。 TypeError: execute() takes no arguments (1 given)
c++のコードを自動生成する
codegen.sh c++のコードを自動生成するシェルスクリプト。 使い方の例は以下。 まず、~/binとかにcodegen.shを置いて とすると、personディレクトリができる。中身を見てみると、 Personクラスとmain()があるmain.cppとMakefileが出来上がり。 これでmakeするとmainって実行可能プログラムが出来る。 あとはPersonクラスにsay()だのeat()だのを追加していくですかね。 仕事でサンプルプログラムを書くのに、なんでもmainに突っ込んで確認するのもしょっぱいので、こんなもん作ってみた。
- Author: vottie
- Category: コンピュータ
- Tags: github, shell script
vagrantでdjangoでユーザ認証
ユーザ認証して、認証後のユーザ利用まで動作できたのでまとめる。 できたけど、フレームワークが提供してくれてるがいろいろ面倒くさい。 djangoって結構自前で作る部分が多いかと思うので、フレームワークがやってくれるのか、そこは自前なの?!、とかが分からなくてしんどかった。 Djangoのユーザ認証まとめ | c-bata web とはいえユーザ認証する部分は上記サイトをまるまる参考にさせていただきました。 せいぜいurls.pyの書き方が違うくらい。 ユーザ認証を別のアプリケーションから利用するところが今回の記事のメインなんだけど、モデルを起こすところも、c-bata webさんの記事を参考にさせてもらいました。しかし、差分だけ書くと作業の流れがわかりにくいので丸々書く。 アプリケーションをこさえる。todoを残すだけのアプリ。 $ python manage.py startapp todo setting.pyのINSTALLED_APPSにtodoを追加。 +++ b/django_auth/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = ( ‘django.contrib.messages’, ‘django.contrib.staticfiles’, ‘account’, + ‘todo’, ) urls.pyにルーティングを追加。 +++ b/django_auth/urls.py @@ -23,5 +23,6 @@ urlpatterns = [ url(r’^logout/$’, ‘django.contrib.auth.views.logout’, {‘template_name’: ‘account/logout.html’}), url(r’^account/’, include(‘account.urls’, namespace=’account’)), + url(r’^todo/’, include(‘todo.urls’)), ] モデルを定義。外部キーとしてdjangoのUserモデルを指定。 admin.py。 todo/urls.pyはフレームワーク生成ではなく、自分で作成。表示のみ。更新はadminからやる。 views.py。何をこんなにimportしなきゃいかんのか分かってないけど、やる。しょうがない。まだdjangoはチュートリアルと、これしか作ってないし。 テンプレートの追加。ディレクトリ構成が気持ち悪いけど、他の書き方がよく分からない。 % mkdir -p todo/templates/todo/ % touch todo/templates/todo/index.html あとはDBを更新すれば出来上がり。 % python manage.py makemigrations % python manage.py migrate 出来上がったのはこれ。ログイン後にhttp://127.0.0.1:8080/todoとかでアクセスすれば、adminで追加したメモがUser毎に表示されるハズ。 https://github.com/vottie/django_auth 苦労したのはtodoのviews.pyでどうやればUserを利用できるか、だった。 結果Userモデルをimportしてあげることで解決できた。 もう1点はListViewでどうやればログインしたUserがGETできるかだったが、ドキュメントをしらみつぶしに読んでいったらUserはRequestオブジェクトに入っていることが分かって、ListViewのselfはrequestオブジェクトを含むことが分かって解決した。 最後に今回の環境は以下。 vagrant ubuntu14.04 python 3.4.3(pyevn) django 1.8
- Author: vottie
- Category: コンピュータ
- Tags: authentication, django, python
Trackbacks and Pingbacks on this post
No trackbacks.
- TrackBack URL
Comments on this post