Upgrade to Pro — share decks privately, control downloads, hide ads and more …

_プログラミング教室_テキスト.pdf

Avatar for manabu ikarashi manabu ikarashi
July 29, 2014
16

 _プログラミング教室_テキスト.pdf

Avatar for manabu ikarashi

manabu ikarashi

July 29, 2014

Transcript

  1. εέδϡʔϧ ̍೔໨ ޕલ ΦϦΤϯςʔγϣϯ ࣗݾ঺հ ͜ͷڭࣨͰ࡞੒͢ΔϓϩάϥϜͷσϞ ·ͣ͸؆୯ͳϓϩάϥϜΛ࡞Ζ͏<TV[VNF> ϓϩάϥϜʹඞཁͳϑΝΠϧͷઆ໌ ޕޙ ໊લΛೖྗ͢ΔϓϩάϥϜΛ࡞Ζ͏<IBUP>

    ϓϩάϥϜΛ࡞ΔͨΊͷجຊΛ஌Ζ͏ ΫΠζͷσʔλΛೖྗ͠Α͏ ̎೔໨ ޕલ ϓϩάϥϜͷಈ͖Λ஌Ζ͏ ΫΠζϓϩάϥϜΛ࡞Ζ͏ʢ̍ʣ<XBTIJ> )5.-Λ֮͑Α͏ ޕޙ ΫΠζϓϩάϥϜΛ࡞Ζ͏ʢ̎ʣ ײ૝ͷൃද
  2. ڭࣨͷϧʔϧ 1.  Ͱ͖ͳ͍͜ͱ͸ஏ͔ͣ͘ͳ͍   –  Ͱ͖ͳͯ͘΋ஏ͔ͣ͋͘͠Γ·ͤΜɻΘ͔Βͳ ͔ͬͨΒݩؾΑ࣭͘໰͠Α͏ʂ   2.  ΈΜͳ͕ઌੜ

      –  ͜ͷڭࣨͰ͸ΈΜͳ͕ઌੜͰ͢ɻΘ͔Βͳ͍ਓ ͕͍ͨΒੵۃతʹॿ͚·͠ΐ͏ɻ   3.  ਓલͰൃද͠Α͏   –  ਓͱΘ͔Γ͋͏ͨΊʹ͸ҙݟΛൃදͰ͖Δ͜ͱ͕ େ੾Ͱ͢ɻࣗ෼ͷҙݟΛੵۃతʹൃ৴͠Α͏ʂ
  3. [1-­‐4-­‐2]  ४උͷ࡞ۀ   sudo  apt-­‐get  update     sudo  apt-­‐get

     install  python-­‐pip     sudo  apt-­‐get  install  sqlite3     sudo  pip  install  django     django-­‐admin.py  startproject  suzume     cd  suzume     python  manage.py  runserver   端末の中に次のコマンドを入力します。  
  4. [2-­‐1-­‐2]  suzume/seJngs.py  ① 12.  DATABASES  =  {   13.   

         'default':  {   14.                 'ENGINE':  'django.db.backends.sqlite3',     15.                 'NAME':  'quiz.sqlite3',     16.                 'USER':  '',   17.                 'PASSWORD':  '',   18.                 'HOST':  '',     19.                 'PORT':  '’,   20.         }   21.  }   …..   32.  TIME_ZONE  =  'Asia/Tokyo’   …..   36.  LANGUAGE_CODE  =  'ja-­‐jp'
  5. [2-­‐1-­‐3]  suzume/seJngs.py  ② 116.  INSTALLED_APPS  =  (   117.   

         'django.contrib.auth',   118.         'django.contrib.contenkypes',   119.         'django.contrib.sessions',   120.         'django.contrib.sites',   121.         'django.contrib.messages',   122.         'django.contrib.stalcfiles',   123.         #  Uncomment  the  next  line  to  enable  the  admin:   124.         'django.contrib.admin',   125.         #  Uncomment  the  next  line  to  enable  admin  documentalon:   126.         #  'django.contrib.admindocs',   127.         'quiz’,   128.  )
  6. [2-­‐1-­‐4]  quiz/models.py 1.  #  -­‐*-­‐  coding:  uD-­‐8  -­‐*-­‐   2. 

    from  django.db  import  models   3.      4.  class  Quiz(models.Model):   5.    quesNon  =  models.CharField('໰୊',  max_length=255)   6.    answer01  =  models.CharField('౴͑01',  max_length=255)   7.    answer02  =  models.CharField('౴͑02',  max_length=255)   8.    answer03  =  models.CharField('౴͑03',  max_length=255)   9.    answer  =  models.IntegerField('ਖ਼ղ൪߸')
  7. [2-­‐2-­‐1]  quiz/admin.py 1.  from  quiz.models  import  Quiz   2.  from

     django.contrib  import  admin   3.  ɹ   4.  admin.site.register(Quiz)   
  8. [2-­‐2-­‐2]  suzume/urls.py 1.  from  django.conf.urls  import  pakerns,  include,  url  

    2.        3.  #  Uncomment  the  next  two  lines  to  enable  the  admin:   4.  from  django.contrib  import  admin   5.  admin.autodiscover()   6.      7.  urlpakerns  =  pakerns('',   …   15.         #  Uncomment  the  next  line  to  enable  the  admin:   16.         url(r'^admin/',  include(admin.site.urls)),   17.  )   
  9. [3-­‐1-­‐1]৽͍͠ϓϩδΣΫτͷ࡞੒ $  django-­‐admin.py  startproject  hato   $  cd  hato  

    $  python  manage.py  startapp  nameform   $  mkdir  templates    「hato」という名前の新しいプロジェクトを作成して、作 業場所をhatoディレクトリにします。   次に名前を入力する機能「nameform」を作成します。   さらに画面を入れるフォルダ「templates」を作成します。  
  10. [3-­‐1-­‐2]  ϑΥϧμߏ੒ hato ・・・ プロジェクトの設定をするフォルダ     __init__.py ・・・ 無視してOK     seJngs.py ・・・ プログラム全体の設定     urls.py ・・・ プログラムの名前を書く  

      wsgi.py ・・・ 無視してOK   manage.py ・・・ 管理コマンド   nameform ・・・ 機能を入れるフォルダ     __init__.py ・・・ 無視してOK     models.py ・・・ データベースの設定     test.py ・・・ 無視してOK     views.py ・・・ データベースと画面の処理   templates ・・・ 画面のファイルを入れる    
  11. [3-­‐1-­‐3]  suzume/seJngs.py  ① 2.  import  os   …..   32. 

    TIME_ZONE  =  'Asia/Tokyo’   …..   36.  LANGUAGE_CODE  =  'ja-­‐jp’   …..   111.  TEMPLATE_DIRS  =  (   112.         #  Put  strings  here,  like  "/home/html/django_templates"  or  "C:/www/django/ templates".   113.         #  Always  use  forward  slashes,  even  on  Windows.   114.         #  Don't  forget  to  use  absolute  paths,  not  relalve  paths.   115.         os.path.join(os.path.dirname(__file__),  '..',  'templates').replace('\\','/'),   116.  )
  12. [3-­‐1-­‐4]  suzume/seJngs.py  ② 116.  INSTALLED_APPS  =  (   117.   

         'django.contrib.auth',   118.         'django.contrib.contenkypes',   119.         'django.contrib.sessions',   120.         'django.contrib.sites',   121.         'django.contrib.messages',   122.         'django.contrib.stalcfiles',   123.         #  Uncomment  the  next  line  to  enable  the  admin:   124.         #  'django.contrib.admin',   125.         #  Uncomment  the  next  line  to  enable  admin  documentalon:   126.         #  'django.contrib.admindocs',   127.         ’nameform’,   128.  )
  13. [3-­‐1-­‐5]  hato/urls.py 1.  from  django.conf.urls  import  pakerns,  include,  url  

    2.        3.  urlpakerns  =  pakerns('',   4.    url(r'^$',  'nameform.views.index'),   5.  )
  14. [3-­‐2-­‐2]  nameform/forms.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django  import  forms   3.        4.  class  MynameForm(forms.Form):   5.    myname  =  forms.CharField(   6.            label='あなたの名前は',     7.            max_length=20,     8.            help_text='(最大文字数20文字)'   9.    )
  15. [3-­‐2-­‐3]  nameform/views.py 1.  #  -­‐*-­‐  coding:  uD-­‐8  -­‐*-­‐   2. 

    from  django.shortcuts  import  render_to_response   3.  from  django.template  import  RequestContext   4.  from  django.hap  import  HapResponse   5.  from  nameform.forms  import  MynameForm   6.      7.  def  index(request):   8.      9.    if  request.method  ==  'POST':   10.      myname  =  request.POST.get('myname')   11.      massage  =  u'͜Μʹͪ͸ɺ'  +  myname  +  u'͞Μ'   12.      return  HapResponse(massage)   13.    else:   14.      form  =  MynameForm()   15.      return  render_to_response('form.html',  {   16.        'from':  form,   17.      },  context_instance=RequestContext(request))
  16. [3-­‐2-­‐4]  templates/form.html 1.  <form  aclon=""  method="POST">{%  csrf_token  %}   2. 

    {{  from.as_p  }}   3.  <input  type="submit"  value="࣮ߦ͢Δ">   4.  </form>
  17. [4-­‐2-­‐3]  ॲཧ 式と判断と繰り返しを組分せせたものを処 理と呼びます。  #  登録ブログのクロールを開始する for  blog  in  blogs:

               logging.info('-­‐['  +  str(blog.id)  +  ']'  +  blog.blog_ltle)              now  =  datelme.datelme.now()      blog.last_crawled  =  pytz.lmezone(seJngs.TIME_ZONE).localize(now)      blog.save()              #  各エントリーの取得 feeds  =  feedparser.parse(blog.blog_rss)      for  feed  in  feeds.entries:                  logging.info('-­‐-­‐-­‐'  +  feed.ltle)          #  日付のタイムゾーンを設定情報に合わせる dt  =  feed.updated_parsed        update_date_local  =  datelme.datelme(dt[0],  dt[1],  dt[2],  dt[3],  dt[4])        update_date  =  pytz.lmezone(seJngs.TIME_ZONE).localize(update_date_local)  
  18. [5-­‐3-­‐1]  quiz/models.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django.db  import  models   3.      4.  class  Quiz(models.Model):   5.      6.         queslon  =  models.CharField(‘΋Μ͍ͩ',  max_length=255   7.         answer01  =  models.CharField(’ͨ͑͜01',  max_length=255)   8.         answer02  =  models.CharField(‘΋Μ͍ͩ02',  max_length=255)   9.         answer03  =  models.CharField(‘΋Μ͍ͩ03',  max_length=255)   10.         answer  =  models.IntegerField(‘͍͔͍ͤ͹Μ͝͏')   11.      12.         def  __unicode__(self):   13.                 return  self.queslon
  19. [5-­‐3-­‐2]  washi/urls.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django.conf.urls  import  pakerns,  include,  url   3.      4.  #  Uncomment  the  next  two  lines  to  enable  the  admin:   5.  from  django.contrib  import  admin   6.  admin.autodiscover()   7.      8.  urlpakerns  =  pakerns('’,   9.         url(r'^$',  'quiz.views.home’),   10.                 url(r'^start/',  'quiz.views.start'),   11.         url(r'^admin/',  include(admin.site.urls)),   12.  )   
  20. [5-­‐3-­‐3]  templates/home.html 1.  <h1>ϝχϡʔ</h1>   2.  <p>   3.  <a

     href="/start/">࢝ΊΔ</a>   4.  </p>   5.  <p>   6.  <a  href="/ranking/">ϥϯΩϯάΛݟΔ</a>   7.  </p>   
  21. [5-­‐3-­‐4]  templates/home.html 1.  <form  acNon="/quesNon/"  method="POST">{%  csrf_token  %}   2. 

    <p>͋ͳͨͷ໊લΛೖྗ͍ͯͩ͘͠͞</p>   3.  <input  type="text"    name="challenger"  size="20"  maxlength="20"  />   4.  <input  type="hidden"    name="quesNon_count"  value="0"  />   5.  <input  type="hidden"    name="correct_count"  value="0"  />   6.  <input  type="submit"  value="ΫΠζΛ࢝ΊΔ"  />   7.  </form>
  22. [5-­‐4-­‐1]  quiz/views.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django.shortcuts  import  render_to_response   3.  from  django.hkp  import  HkpResponse   4.  from  django.template  import  RequestContext   5.  from  quiz.models  import  Quiz   6.  import  random   7.      8.  def  home(request):     9.         return  render_to_response('home.html',  {   10.         },  context_instance=RequestContext(request))   11.      12.  def  start(request):   13.         return  render_to_response('start.html',  {   14.         },  context_instance=RequestContext(request))  
  23. [5-­‐4-­‐2]  washi/urls.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django.conf.urls  import  pakerns,  include,  url   3.      4.  #  Uncomment  the  next  two  lines  to  enable  the  admin:   5.  from  django.contrib  import  admin   6.  admin.autodiscover()   7.      8.  urlpakerns  =  pakerns('’,   9.         url(r'^$',  'quiz.views.home’),   10.         url(r'^start/',  'quiz.views.start'),   11.         url(r'^quesNon/',  'quiz.views.quesNon'),   12.         url(r'^admin/',  include(admin.site.urls)),   13.  )
  24. [5-­‐6-­‐1]  templates/queslon.html 1.  <form  aclon="/check/"  method="POST">{%  csrf_token  %}   2. 

    <input  type="hidden"  name="queslon_id"  value="{{  queslon_id  }}"  />   3.  <input  type="hidden"  name="challenger"  value="{{  challenger  }}"  />   4.  <input  type="hidden"  name="queslon_count"  value="{{  queslon_count  }}"  />   5.  <input  type="hidden"  name="correct_count"  value="{{  correct_count  }}"  />   6.  <p>{{  challenger  }}͞Μɺ͕Μ͹Εʂ</p>   7.  <p>{{  queslon_count  }}໰໨ 8.  <h1>໰୊ɿ{{  queslon  }}</h1>   9.  ղ౴ 10.  <ol>   11.    <li>{{  answer1  }}</li>   12.    <li>{{  answer2  }}</li>   13.    <li>{{  answer3  }}</li>   14.  </ol>  
  25. [5-­‐6-­‐3]  quiz/views.py 1.  def  queslon(request):   2.       

     if  request.method  ==  'POST':   3.                 #  ໰୊ͷऔಘ   4.                 record_count  =  Quiz.objects.count()   5.                 get_id  =  random.randint(1,  record_count)   6.                 record  =  Quiz.objects.get(pk=get_id)   7.                 #  -­‐-­‐  ௅ઓऀ໊ 8.  challenger  =  request.POST.get('challenger',  '')   9.                 #  -­‐-­‐  Կ໰໨͔   10.                 queslon_count  =  int(request.POST.get('queslon_count',  ''))   11.                 queslon_count  =  queslon_count  +  1   12.                 #  -­‐-­‐  ਖ਼ղ਺   13.                 correct_count  =  request.POST.get('correct_count',  '’)  
  26. [5-­‐6-­‐4]  quiz/views.py 1.               

     #  -­‐-­‐  ໰୊ͷID   2.                 queslon_id  =  record.id   3.                 #  -­‐-­‐  දࣔ͢Δ໰୊ͱղ౴܈   4.                 queslon  =  record.queslon   5.                 answer01  =  record.answer01   6.                 answer02  =  record.answer02   7.                 answer03  =  record.answer03   8.      9.                   return  render_to_response('queslon.html',  {   10.                         #  -­‐-­‐  νϟϨϯδͷ࠷ޙ·Ͱอ༗͢Δ஋   11.                         'queslon_count':  queslon_count,   12.                         'correct_count':  correct_count,   13.                         'challenger':  challenger,  
  27. [5-­‐6-­‐5]  quiz/views.py 1.               

               #  -­‐-­‐  ਖ਼ղͷνΣοΫ·Ͱอ༗͢Δ஋   2.                           'queslon_id':  queslon_id,   3.                           #  -­‐-­‐  ͦͷଞͷ஋ 4.                         'queslon':  queslon,   5.                         'answer1':  answer01,   6.                         'answer2':  answer02,   7.                         'answer3':  answer03,   8.                 },  context_instance=RequestContext(request))   9.      10.         else:   11.                 massage  =  u'௅ઓऀ໊͕ઃఆ͞Ε͍ͯ·ͤΜɻ’   12.                 return  HkpResponse(massage)
  28. [5-­‐6-­‐6]  templates/check.html 1.  <form  aclon="/queslon/"  method="POST">{%  csrf_token  %}   2. 

    <input  type="hidden"  name="challenger"  value="{{  challenger  }}"  />   3.  <input  type="hidden"  name="queslon_count"  value="{{  queslon_count  }}"  />   4.  <input  type="hidden"  name="correct_count"  value="{{  correct_count  }}"  />   5.  <input  type="submit"  value="࣍ͷ໰୊ʹਐΉ"  />   6.  </form>
  29. [5-­‐6-­‐7]  quiz/views.py 1.  def  check(request):   2.       

     if  request.method  ==  'POST':   3.                 #  -­‐-­‐  ௅ઓऀ໊ 4.  challenger  =  request.POST.get('challenger',  '')   5.                 #  -­‐-­‐  Կ໰໨͔ 6.  queslon_count  =  int(request.POST.get('queslon_count',  ''))   7.                 #  -­‐-­‐  ਖ਼ղ਺ 8.  correct_count  =  int(request.POST.get('correct_count',  ''))   9.                 #  -­‐-­‐  ೖྗ͞Εͨ౴ 10.  input_answer  =  int(request.POST.get('input_answer',  ''))   11.                 #  -­‐-­‐  ໰୊ͷਖ਼ղͷऔಘ 12.  queslon_id  =  request.POST.get('queslon_id',  '')   13.                 record  =  Quiz.objects.get(pk=queslon_id)   14.                 answer  =  int(record.answer)
  30. [5-­‐6-­‐8]  quiz/views.py 1.               

     #  -­‐-­‐  ਖ਼ղͷνΣοΫ 2.  if  input_answer  ==  answer:   3.                         correct_count  =  correct_count  +  1   4.                         message  =  u'͍͢͝ʂਖ਼ղͰ͢ʂʂ'   5.                 else:   6.                         message  =  u'͟ΜͶΜʂෆਖ਼ղͰ͢ʂʂ'   7.      8.                 if  queslon_count  ==  5:   9.                         #  -­‐-­‐  ϥϯΩϯάσʔλͷอଘ 10.  histry  =  Histry(name=challenger,  points=correct_count)   11.                         histry.save()   12.                         return  render_to_response('finish.html',  {   13.                                 #  -­‐-­‐  νϟϨϯδͷ࠷ޙ·Ͱอ༗͢Δ஋ 14.  'correct_count':  correct_count,   15.                                 'challenger':  challenger,   16.                         },  context_instance=RequestContext(request))
  31. [5-­‐6-­‐9]  quiz/views.py 1.               

     else:   2.                         return  render_to_response('check.html',  {   3.                                 #  -­‐-­‐  νϟϨϯδͷ࠷ޙ·Ͱอ༗͢Δ஋ 4.                                 'queslon_count':  queslon_count,   5.                                 'correct_count':  correct_count,   6.                                 'challenger':  challenger,   7.                                 #  -­‐-­‐  ਖ਼ղɺෆਖ਼ղͷϝοηʔδ 8.  'message':  message,   9.                         },  context_instance=RequestContext(request))   10.      11.         else:   12.                 massage  =  u'௅ઓऀ໊͕ઃఆ͞Ε͍ͯ·ͤΜɻ'   13.                 return  HkpResponse(massage)
  32. [5-­‐6-­‐10]  washi/urls.py 1.  #  -­‐*-­‐  coding:  uu-­‐8  -­‐*-­‐   2. 

    from  django.conf.urls  import  pakerns,  include,  url   3.      4.  #  Uncomment  the  next  two  lines  to  enable  the  admin:   5.  from  django.contrib  import  admin   6.  admin.autodiscover()   7.      8.  urlpakerns  =  pakerns('’,   9.         url(r'^$',  'quiz.views.home'),   10.         url(r'^start/',  'quiz.views.start'),   11.         url(r'^queslon/',  'quiz.views.queslon'),   12.         url(r'^check/',  'quiz.views.check'),   13.         url(r'^ranking/',  'quiz.views.ranking'),   14.         url(r'^admin/',  include(admin.site.urls)),   15.  )   
  33. [5-­‐7-­‐1]  quiz/views.py 1.  def  ranking(request):   2.    persons  =

     Histry.objects.all().order_by('-­‐points')[:10]   3.    return  render_to_response('ranking.html',  {   4.      'persons':persons,   5.    },  context_instance=RequestContext(request))
  34. [5-­‐7-­‐2]  templates/ranking.html 1.  <h1>ಘ఺ϥϯΩϯά</h1>   2.  <ol>   3.  {%

     for  person  in  persons  %}   4.         <li>{{  person.name  }}͞Μ-­‐  {{  person.points  }}఺</li>   5.  {%  endfor  %}   6.  </ol>
  35. ༻ޠ •  σʔλϕʔε   –  σʔλΛอ؅͢ΔͨΊͷϓϩάϥϜɻσʔλϕʔεอ؅͢Δͱσʔλͷ औΓग़͕͠؆୯ʹͳΔɻ   •  Python

      –  ϓϩάϥϛϯάݴޠɻϓϩάϥϜΛ࡞੒͢ΔͨΊͷݴ༿ɻPythonͷଞʹ ΋PHP΍JAVA౳͕͋Δɻ   •  Django   –  ϓϩάϥϛϯάϑϨʔϜϫʔΫɻϓϩάϥϜΛ؆୯ʹͭΔͨ͘Ίͷಓ۩ɻ   •  ΤσΟλ   –  จࣈΛೖྗ͢ΔͨΊͷϓϩάϥϜɻ   •  Terminalʢ୺຤ʣ   –  ίϯϐϡʔλʹ໋ྩΛೖྗ͢ΔͨΊͷϓϩάϥϜɻ   •  WEBϒϥ΢β   –  ΠϯλʔωοτΛݟΔͨΊͷϓϩάϥϜɻ  
  36. ͜ͷڭࣨͰར༻ͨ͠ιϑτ •  OS  –  Ubuntu  Desktoo  ೔ຊޠRemix  12.04    

    •  σʔλϕʔε  –  sqlite3  3.7.9   •  ϓϩάϥϜݴޠ–  Python  2.7.4   •  ϑϨʔϜϫʔΫ–  Django  1.5.1