銀月の符号

Python 使い見習いの日記・雑記

Python 札幌第3回 Ustream 参加

随時、更新中。…更新終了。

Python関連読書会 13:20〜14:10

13:20 開始、 14:30 終了。

Python チュートリアルの「3.1.4 リスト」から「4.6 関数を定義する」まで。

あと None について盛り上がったため 20分くらいオーバー。 NULL およびそれに類するものの動作は言語によってずいぶん違うので気になった方がいた模様。

ちなみに Python の None は NoneType の唯一のインスタンス。同値性検証、同一性検証結果はともに True 。

>>> None == None
True
>>> None is None
True

あるオブジェクトが None かどうかを調べる時は is 演算子で行う習慣をつけるとよい。 == 演算子でも期待した動作はするけれども。

>>> a = 1
>>> if a is None:
...   print 'a is None'
...

とはいえ、 None かどうかなんて調べる機会はあんまりないのも確か。通常の真偽判定で、ほぼ事足りる。 is None って書いた時点で、無駄なことをしていないかどうか振り返ってみるべき。

nakayoshix(中村)によるミニセミナー (数学におけるPython利用の紹介) 14:20〜14:50

14:40 開始、 15:05 終了。

sagemath について。

ソースコードリーディング (Djangoソースコードを読みます) 15:00〜16:30

15:20 開始、16:50 中断、 10 分休憩。 17:00 再開、 17:35 終了。

ソースコードではなくチュートリアルを読んでなぞる。ただし、アプリは TODO 管理。

django-admin.py startproject pyporo3
cd pyporo3
manage.py startapp todoapp1

settings.py を開いて

import os

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = os.path.join(os.path.dirname(__file__), 'dbfile')     

データベース作る。

manage.py syncdb

todoapp1/models.py 編集。

from django.db import models

class ToDo(models.Model):
    title = models.CharField(max_length=50)
    text = models.CharField(max_length=200)
    done = models.CharField(max_length=200)
    due_date = models.DateTimeField('due date')

もういちど settings.py を開いて

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'todoapp1',
)  

再度 syncdb

manage.py syncdb

manage.py shell して遊ぶ。

Admin サイト編突入。 settings.py 編集。 django.contrib.admin サイト追加。

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'todoapp1',
    'django.contrib.admin',
)  

pyporo3/urls.py 編集。 admin.autodiscover() コメントアウトはずし。 (r'^admin/(.*)', admin.site.root), もはずし。

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^pyporo3/', include('pyporo3.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    #(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/(.*)', admin.site.root),
)

manage.py runserver してブラウザを開くと管理者画面へ。

todoapp1/models.py 編集。 admin をインポートして admin.site.register(ToDo) を加える。

from django.db import models
from django.contrib import admin

class ToDo(models.Model):
    title = models.CharField(max_length=50)
    text = models.CharField(max_length=200)
    done = models.CharField(max_length=200)
    due_date = models.DateTimeField('due date')

admin.site.register(ToDo)

サイトを日本語化。 setup.py を。

TIME_ZONE = 'Asia/Tokyo'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'ja'

この先ついていけず。だれか補完して Python-Sapporo のサイトに上げてください。