2013年11月29日 星期五

Mac OS X 安裝及使用  Homebrew

ubuntu apt 用習慣的朋友,應該也能很快適應 Homebrew for Mac OS X的。

安裝步驟參考官網及實際經驗筆記如下:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
> brew update

接著就能安裝自己想要的 UNIX Packages,如:wget, curl, lynx ...
> brew install wget
> brew install lynx

查詢經安裝的套件列表 
> brew list

或是想要移除已經安裝的套件
> brew uninstall wget

自動更新已經安裝的套件
> brew upgrade wget

其他功能可以藉由Help看指令參數及用法 
> brew help


延伸閱讀: 

#

2013年11月18日 星期一

Ubuntu precise amd64 安裝筆記


# apt-get update & 
sudo apt-get -y update
sudo apt-get dist-upgrade
sudo apt-get --no-install-recommends -y install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev ncurses-dev automake libtool bison subversion pkg-config libffi-dev vim

# install python
sudo apt-get install build-essential
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
cd /tmp
wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tgz
tar -xvf Python-2.7.6.tgz
cd Python-2.7.6
./configure
make
sudo make altinstall

# install easy_install for python 2.7
cd ..
sudo apt-get install python-setuptools

# install Flask for python
sudo easy_install Flask

# prepare to install uwsgi, nginx
sudo useradd -r -g www-data -s /bin/false uwsgi
sudo mkdir -p /var/www/myapp
sudo chown uwsgi:www-data /var/www/myapp -R
sudo useradd -G www-data myuser
sudo usermod -a -G www-data myuser

# install uwsgi
sudo pip install uwsgi
sudo mkdir -p /var/log/uwsgi
sudo touch /var/log/uwsgi/myapp.log
cd /var/www/myapp
sudo vi uwsgi.xml
sudo /usr/local/bin/uwsgi -x uwsgi.xml

# install nginx
sudo apt-get install nginx
sudo service nginx start
cd /etc/nginx/
cd sites-enabled/
sudo vi default


uwsgi.xml

<?xml version="1.0"?>
<uwsgi id="myapp">
  <module>application:app</module>
  <socket>/tmp/uwsgi.sock</socket>
  <socket>127.0.0.1:8000</socket>
  <processes>4</processes>
  <chdir>/var/www/myapp</chdir>
  <pidfile>/tmp/myapp.pid</pidfile>
  <daemonize>/var/log/uwsgi/myapp.log</daemonize>
  <stats>/tmp/myapp.stats</stats>
  <log-format>``%(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) `` "%(referer)" "%(uagent)"</log-format>
  <uid>uwsgi</uid>
  <gid>www-data</gid>
  <chmod-socket>775</chmod-socket>
</uwsgi>

/etc/nginx/sites-enabled/default

server {
        listen 8080;
        root /var/www/myapp;
        server_name myapp;

        try_files $uri @uwsgi;
        location @uwsgi {
                include uwsgi_params;
                uwsgi_pass unix:/tmp/myapp.sock;
        }
}


2013年11月14日 星期四

安裝 elasticsearch 與 python client for elasticsearch

安裝 elasticsearch

官網下載 http://www.elasticsearch.org/download/

解開壓縮檔後,至bin目錄下即可執行啟動 elasticsearch

> elasticsearch.bat 或 > elasticsearch.sh


安裝 urllib3 package

> easy_install urllib3 或 > pip install urllib3

或至官網下載https://pypi.python.org/pypi/elasticsearch

接著利用下列程式測試一下...
# -*- coding: utf-8 -*-

import urllib3
http = urllib3.proxy_from_url('http://proxy.hinet.net/')
r = http.request('GET', 'http://tekibrain.blogspot.com/')

print r.status # 正常的話應該會印出 '200',網站傳回 HTTP Return code '200'
print r.data # 印出網頁原始碼資料



安裝 python client for elasticsearch package

> easy_install elasticsearch
或者
> pip install elasticsearch

或至官網下載 https://pypi.python.org/pypi/urllib3

接著利用下列程式 estest.py 測試一下...
# -*- coding: utf-8 -*-

from datetime import datetime
from elasticsearch import Elasticsearch

# 預設連線至 ElasticSearch Server Port 9200,  localhost:9200
es = Elasticsearch()

# 加入資料進行索引, 自己設定  id = 1
# http://localhost:9200/social/tweet/1
setdata = es.index(index="social", doc_type="tweet", id=1, body={"content": "大家好","user":{"name":"老王","id":670085},"tags":["demo","test"], "timestamp": datetime.now()})
print 'set data => ' 
print setdata
setdata = es.index(index="social", doc_type="tweet", id=2, body={"content": "大家好2","user":{"name":"小王","id":670086},"tags":["demo","test2"], "timestamp": datetime.now()})
print 'set data => ' 
print setdata


#  取得 id=1 資料
getdata = es.get(index="social", doc_type="tweet", id=1)['_source']
print '-------------------------'
print 'get data <= ' 

es.indices.refresh(index="social")
# Search:
qdoc = {
 "query": {
  "match" : {
   "tags" : "demo"
  }
 }
}
getdata = es.search(index="social", body=qdoc)
print 'get data <= '
#print type(getdata)
print("Got %d Hits:" % getdata['hits']['total'])
for hit in getdata['hits']['hits']:
    print("%(content)s %(user)s: %(tags)s" % hit["_source"])


測試成功結果 (timestamp依照系統時間而不同)
> python estest.py
set data =>
{u'_type': u'tweet', u'_id': u'1', u'ok': True, u'_version': 44, u'_index': u'social'}
set data =>
{u'_type': u'tweet', u'_id': u'2', u'ok': True, u'_version': 42, u'_index': u'social'}
-------------------------
get data <=
get data <=
Got 2 Hits:
大家好2 {u'name': u'\u5c0f\u738b', u'id': 670086}: [u'demo', u'test2']
大家好 {u'name': u'\u8001\u738b', u'id': 670085}: [u'demo', u'test']

#

2013年11月4日 星期一

Python 2.7 進行 datetime 加減(timedalta)及設定輸出格式(strftime)


  1. 設定 someday = datetime.date(2013,10,28)
  2. datetime.timedelta(days = 1) 用於計算 datetime 時間增減(本例:以1天為單位)
  3. 可利用 strftime("%Y%m%d) datetime 控制輸出格式

vi test.py
import time
import datetime

someday = datetime.date(2014,9,20)

while (someday <= datetime.date.today()) :
    # print out with date format : YYYYMMDD, example : 20140920
    print str(someday.strftime("%Y%m%d"))
    someday += datetime.timedelta(days = 1)

print "================="
    
someday = datetime.date.today()

while (someday >= datetime.date(2014,9,15)) :
    # print out with date format : YYYYMMDD, example : 2014-09-20
    print str(someday.strftime("%Y-%m-%d"))
    # minus, timedelta(days = 1)
    someday -= datetime.timedelta(days = 1)

print "================="

someday = datetime.date.today()

while (someday >= datetime.date(2014,9,15)) :
    # print out with date format : YYYYMMDD, example : 2014/09/20
    print str(someday.strftime("%Y/%m/%d"))
    # add, timedelta(days = -1)
    someday += datetime.timedelta(days = -1)

執行結果:
$ python test.py
20140920
20140921
20140922
=================
2014-09-22
2014-09-21
2014-09-20
2014-09-19
2014-09-18
2014-09-17
2014-09-16
2014-09-15
=================
2014/09/22
2014/09/21
2014/09/20
2014/09/19
2014/09/18
2014/09/17
2014/09/16
2014/09/15

延伸閱讀:datetime - Basic date and time types (python.org)

#