32
おまえらこのライブラリ使ってないの? m9 ふるかわとおる @torufurukawa

おまえらこのライブラリ使ってないの? m9(2013-07)

Embed Size (px)

Citation preview

Page 1: おまえらこのライブラリ使ってないの? m9(2013-07)

おまえらこのライブラリ使ってないの?  m9

ふるかわとおる  @torufurukawa  

Page 2: おまえらこのライブラリ使ってないの? m9(2013-07)

お前だれよ?

•  ふるかわとおる  •  @torufurukawa  •  #bucho  •  株式会社バスキュール  – TV連動サービス作ってますした  

Page 3: おまえらこのライブラリ使ってないの? m9(2013-07)

アンケート結果より...

•  仮想環境  •  シェル拡張  •  コーディングスタイル  •  テスト  

•  追加は  #pyfes  #m9  で、ツイートしてください。  h8ps://twi8er.com/search?q=%23pyfes%20AND%20%23m9&src=typd  

Page 4: おまえらこのライブラリ使ってないの? m9(2013-07)

仮想環境でインストールし放題

•  複数のプロジェクト  •  ライブラリの競合を避けたい

Page 5: おまえらこのライブラリ使ってないの? m9(2013-07)

virtualenv

$  virtualenv  .  $  ls  bin          include  lib  $  source  bin/activate  インストールし放題!

Page 6: おまえらこのライブラリ使ってないの? m9(2013-07)

venv  (3.x)

$  python3.3  -­‐m  venv  .  $  source  bin/activate  インストールし放題!

Page 7: おまえらこのライブラリ使ってないの? m9(2013-07)

virtualenvwrapper

$  mkvirtualenv  myenv  $  workon  myenv  インストールし放題!

Page 8: おまえらこのライブラリ使ってないの? m9(2013-07)

対話シェル拡張

•  対話シェルだけでは...  •  Python  の対話シェルでもっといろいろやる

Page 9: おまえらこのライブラリ使ってないの? m9(2013-07)

ipython $  ipython  Python  2.7  (r27:82500,  Nov    7  2010,  14:17:28)    Type  "copyright",  "credits"  or  "license"  for  more  informaXon.    IPython  0.13.2  -­‐-­‐  An  enhanced  InteracXve  Python.  ?                  -­‐>  IntroducXon  and  overview  of  IPython's  features.  %quickref  -­‐>  Quick  reference.  help            -­‐>  Python's  own  help  system.  object?      -­‐>  Details  about  'object',  use  'object??'  for  extra  details.    In  [1]:  

Page 10: おまえらこのライブラリ使ってないの? m9(2013-07)

In  [1]:  import  random    In  [2]:  random.choice?  Type:              instancemethod  String  Form:<bound  method  Random.choice  of  <random.Random  object  at  0x10183f820>>  File:              /usr/local/Cellar/python/2.7/lib/python2.7/random.py  Definition:  random.choice(self,  seq)  Docstring:    Choose  a  random  element  from  a  non-­‐empty  sequence.

Page 11: おまえらこのライブラリ使ってないの? m9(2013-07)

In  [3]:  random.choice??  Type:              instancemethod  String  Form:<bound  method  Random.choice  of  <random.Random  object  at  0x10183f820>>  File:              /usr/local/Cellar/python/2.7/lib/python2.7/random.py  Definition:  random.choice(self,  seq)  Source:          def  choice(self,  seq):                  """Choose  a  random  element  from  a  non-­‐empty  sequence."""                  return  seq[int(self.random()  *  len(seq))]    #  raises...  

Page 12: おまえらこのライブラリ使ってないの? m9(2013-07)

ls  とか  cd  とか

In  [4]:  ls  bin/    lib/    share/  include/  man/    src/    In  [5]:  cd  src  /Users/torufurukawa/works/pyfes201307/src

Page 13: おまえらこのライブラリ使ってないの? m9(2013-07)

コマンド実行

In  [6]:  !ps        PID  TTY                      TIME  CMD    2739  8ys000        0:00.84  -­‐zsh    6059  8ys000        0:01.13  /Users/torufurukawa/works/pyfes201307/bin/python2.7  /Us....  

Page 14: おまえらこのライブラリ使ってないの? m9(2013-07)

In  [7]:  !redis-­‐server  [6080]  27  Jul  12:59:29  #  Warning:  no  config  file  specified,  using  the  default  config.  In  order  to  specify  a  config  file  use  'redis-­‐server  /path/to/redis.conf'  [6080]  27  Jul  12:59:29  *  Server  started,  Redis  version  2.4.10  [6080]  27  Jul  12:59:29  *  The  server  is  now  ready  to  accept  connecXons  on  port  6379  

Page 15: おまえらこのライブラリ使ってないの? m9(2013-07)

bpython

誰か書いて

Page 16: おまえらこのライブラリ使ってないの? m9(2013-07)

コーディングスタイル

•  汚いコードを見つけてくれる  •  自分でチェックしなくていい!

Page 17: おまえらこのライブラリ使ってないの? m9(2013-07)

from  urllib  import  urlopen    #  使ってない  def  add(a,  b  ):    #  カッコのまわりに不要スペース          addressess  =  None    #  スペルミス          return  a  +  b  

Page 18: おまえらこのライブラリ使ってないの? m9(2013-07)

pep8

$  pep8  spam.py  spam.py:2:1:  E302  expected  2  blank  lines,  found  0  spam.py:2:13:  E202  whitespace  before  ')'  

Page 19: おまえらこのライブラリ使ってないの? m9(2013-07)

pyflakes

$  pyflakes  spam.py  spam.py:1:  'urlopen'  imported  but  unused  spam.py:3:  local  variable  'addressess'  is  assigned  to  but  never  used

Page 20: おまえらこのライブラリ使ってないの? m9(2013-07)

flake8

$  flake8  spam.py  spam.py:1:1:  F401  'urlopen'  imported  but  unused  spam.py:2:1:  E302  expected  2  blank  lines,  found  0  spam.py:2:13:  E202  whitespace  before  ')'  spam.py:3:1:  F841  local  variable  'addressess'  is  assigned  to  but  never  used

Page 21: おまえらこのライブラリ使ってないの? m9(2013-07)

misspellings

$  misspellings  spam.py  spam.py:3:  addressess  -­‐>  "addresses"

Page 22: おまえらこのライブラリ使ってないの? m9(2013-07)

HTTP  リクエスト

•  urllib  だけだとちょっと...  •  リクエストを簡単に書きたい  •  レスポンスを簡単に取得したい

Page 23: おまえらこのライブラリ使ってないの? m9(2013-07)

requests >>>  import  requests  >>>  r  =  requests.get('h8p://wozozo.jp/')  >>>  r.status_code  200  >>>  r.content  '\n<!DOCTYPE  html>\n<!-­‐-­‐[if  IEMobile  7  ]><html  class="no-­‐js  iem7"><![endif]-­‐-­‐>\n<!-­‐-­‐[if  lt  IE  9]><html  class="no-­‐js  lte-­‐ie8"><![endif]-­‐-­‐>\n<!-­‐-­‐[if  (gt  IE  8)|(gt  IEMobile  7)|!(IEMobile)|!(IE)]><!-­‐-­‐><html  class="no-­‐js"  lang="en"><!-­‐-­‐<![endif]

Page 24: おまえらこのライブラリ使ってないの? m9(2013-07)

>>>  r.json()    #  辞書を返す  {'spam':  'ham',  'foo':  [1,2,3]}    

Page 25: おまえらこのライブラリ使ってないの? m9(2013-07)

requests.post(    'h8p://wozozo.jp/',  

         data={'name':  'xaxtsuxo'},    #  パラメータ            auth=('user',  'pass')    #  Basic  Auth  )

Page 26: おまえらこのライブラリ使ってないの? m9(2013-07)

テスト

•  テストを手早く書きたい  •  失敗したテスト結果は分かりやすくならん?

Page 27: おまえらこのライブラリ使ってないの? m9(2013-07)

py.test

•  テストランナー  +  ディスカバラー  •  エラーをきれいに見せてくれる  •  確認しやすい

Page 28: おまえらこのライブラリ使ってないの? m9(2013-07)

import  uni8est    class  MyTest(uni8est.TestCase):          def  test(self):                  self.assertEqual(True,  True)    def  test2():          x  =  {'foo':  'bar',  'hoge':  'fuga'}          y  =  {'foo':  'bar',  'hoge':  'wozozo'}          assert  x  ==  y  

Page 29: おまえらこのライブラリ使ってないの? m9(2013-07)

$  py.test  mytest.py  =============================  test  session  starts  ==============================  pla{orm  darwin  -­‐-­‐  Python  2.7.0  -­‐-­‐  pytest-­‐2.3.5  collected  3  items      mytest.py  .F  ...  

Page 30: おまえらこのライブラリ使ってないの? m9(2013-07)

...            def  test2():                  x  =  {'foo':  'bar',  'hoge':  'fuga'}                  y  =  {'foo':  'bar',  'hoge':  'wozozo'}  >              assert  x  ==  y  E              assert  {'foo':  'bar',  'hoge':  'fuga'}  ==  {'foo':  'bar',...ge':  'wozozo'}  E                  Hiding  1  idenXcal  items,  use  -­‐v  to  show  E                  Differing  items:  E                  {'hoge':  'fuga'}  !=  {'hoge':  'wozozo'}    

Page 31: おまえらこのライブラリ使ってないの? m9(2013-07)

watchdog

•  ファイル変更を監視して、コマンド実行 •  継続的テスト  

Page 32: おまえらこのライブラリ使ってないの? m9(2013-07)

watchdog

$  watchmedo  \          shell-­‐command  \          -­‐-­‐pa8erns="*.py"  \          -­‐-­‐command="py.test  \          mytest.py"