トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

LN-python

PIP インストール

> curl -kL https://bootstrap.pypa.io/get-pip.py | python2.7
> curl -kL https://bootstrap.pypa.io/get-pip.py | python3.3
### curl -x http://192.168.0.252:8080 -kL https://bootstrap.pypa.io/get-pip.py | python2 - --proxy=192.168.0.252:8080
### curl -x http://127.0.0.1:18080 -kL https://bootstrap.pypa.io/get-pip.py | python3 - --proxy=127.0.0.1:18080
> pip2.7 install virtualenv
> pip3.3 install virtualenv 
> pip3.6 install --upgrade --user virtualenv
## PROXY 
> wget https://bootstrap.pypa.io/get-pip.py 
> python ./get-pip.py --proxy=user@proxy.hoge.jp:port
> pip install pycrypto --proxy=user@proxy.hoge.jp:port
## UPdate の 一覧
> pip2  list --o
> pip2 install FOOBAR -U
## バージョン指定
> pip install six==1.8.0
# 一括アップデート 
$ pip list -o | awk '{print $1}' | xargs pip install -U
$ pip3 list --outdated --format=legacy | awk '{print $1}' | xargs pip3 install -U pip

module を local へインストール

python setup.py install --home=<dir>
or
python setup.py install --home=

python setup.py install --prefix=$HOME/lib
http://docs.python.jp/2.5/inst/alt-install-windows.html
PIP の場合
pip install <module-name> --user

pipでインストール

pip install モジュール名

python で ODBC

http://code.google.com/p/pyodbc/
参照

python debbugger

pip install pudb
( q で終了 )
http://momijiame.tumblr.com/post/79011616659/python-%E3%81%AE-cui-%E3%83%87%E3%83%90%E3%83%83%E3%82%AC-pudb-%E3%81%8C%E4%BE%BF%E5%88%A9%E3%81%99%E3%81%8E%E3%81%9F%E4%BB%B6

文字列操作でハマった encode と decode

UNICODE型 <<== decode ==== STR型
      u = unicode( s,encoding='utf-8' )

UNICODE型 <<== unicode === STR型
      u = s.decode('utf-8')

UNICODE型 ==== encode ==>> STR型
      s = u.encode('utf_8')
Ex.
retdata=unicode(r,'utf-8',errors='replace')
retdata=unicode(r,'utf-8',errors='ignore')
retdata=unicode(r,'utf-8').encode('utf-8','backslashreplace')
STR ==> BYTE ( PYTHON3 )
encmoji = 'ストリング'.encode('utf-8')
BYTE ==> STR ( PYTHON3 )
encstring = encmoji.decode('utf-8')

PIL で動かない

OpenSUSE 13.2 になったら今まで動いていたPILがうまく動かない。。
img.save(filename)
が動かない。。(拡張子が不正とかいってエラーになる。。)
# import Image 
を
from PIL import Image 
とモジュール名を正しく設定しよう。。
ネタ元
     http://stackoverflow.com/questions/28503419/unknown-extension-in-save-function-of-pil-due-to-empty-extension-array

alsa をつかう

# pip install pyalsaaudio
Collecting pyalsaaudio
 Downloading pyalsaaudio-0.8.2.tar.gz (212kB)
   100% |????????????????????????????????| 215kB 1.9MB/s
Building wheels for collected packages: pyalsaaudio
 Running setup.py bdist_wheel for pyalsaaudio ... done
 Stored in directory:   /root/.cache/pip/wheels/54/16/2c/54170e41eb51c51a930fad4bf40d4a55a733ac40aaaddc80bc
Successfully built pyalsaaudio
Installing collected packages: pyalsaaudio
Successfully installed pyalsaaudio-0.8.2
# sync

Python LDAP/SSL

# yum install python-devel openldap-devel
# yum install libxslt-devel libxml2-devel libffi-devel
# pip install python-ldap --proxy=127.0.0.1:18080 --user

Python をデーモン化

SupervisorでPythonのスクリプトをデーモンプロセスとして動かす

Spider を作る

https://qiita.com/checkpoint/items/9d95288affb4b33c601a#_reference-c4d551f320d40c79628b

pip install scrapy

Sample-CODE

# -*- coding:utf-8 -*-

from scrapy import Spider
from scrapy.http import Request

class GetCSVSpider(Spider):
   name = 'get_csv_spider'
   allowed_domains = ['seanlahman.com']

   custom_settings = {
       'DOWNLOAD_DELAY': 1.5,
   }

   # CSVファイルを保存する任意のディレクトリ
   DIR_NAME = '/tmp/csv/'

   # エンドポイント(クローリングを開始するURLを記載する)
   start_urls = ['http://seanlahman.com/baseball-archive/statistics/']

   # URLの抽出処理を記載
   def parse(self, response):
       for href in response.css('.entry-content a[href*=csv]::attr(href)'):
           full_url = response.urljoin(href.extract())

           # 抽出したURLを元にRequestを作成し、ダウンロードする
           yield Request(full_url, callback=self.parse_item)

   # ダウンロードしたページを元に、内容を抽出し保存する
   def parse_item(self, response):

       file_name = '{0}{1}'.format(self.DIR_NAME, response.url.split('/')[-1])

       # ファイルの保存
       f = open(file_name, 'w')
       f.write(response.body)
       f.close()

使い方
scrapy runspider get_csv_spider

Scrapy