顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

2014年9月18日 星期四

Install Flask on CentOS 7.0

1. Install pip on CentOS 7.0

## First install the EPEL repo as per your Operating system version and architecture.
rpm -ivh http://mirror01.idc.hinet.net/EPEL/7/x86_64/e/epel-release-7-1.noarch.rpm
yum install -y python-pip

2. Install Flask

pip install Flask

## Test
vi hello.py

from flask import Flaskapp = Flask(__name__)
@app.route("/")def hello():    return "Hello World!"
if __name__ == "__main__":    app.run()

## Verify
python hello.py

* Running on http://0.0.0.0:80/

2014年7月17日 星期四

Python: ConfigParser

[core]
cmd = /usr/bin/vim

[mod]
safe_edit = yes
time_interval = 20

python 程式如下:
#!/usr/bin/python
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('test.conf')

#傳回字串 /usr/bin/vim
parser.get('core', 'cmd')

#傳回 boolean 值 True
parser.getboolean('mod', 'safe_edit')

#傳回整數值 20
parser.getint('mod', 'time_interval')


references:
http://williewu.blogspot.tw/2007/10/python-configparser.html
https://wiki.python.org/moin/ConfigParserExamples
https://docs.python.org/2.6/library/configparser.html

How to find script's directory

import os
print os.path.dirname(os.path.realpath(__file__))

reference:
http://stackoverflow.com/questions/4934806/python-how-to-find-scripts-directory

2012年5月28日 星期一

安裝 wxPython


[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1


yum install wxPython.x86_64 wxPython-devel.x86_64

Dependencies Resolved

==================================================================================================================================================
 Package                                Arch                           Version                                  Repository                   Size
==================================================================================================================================================
Installing:
 wxPython                               x86_64                         2.8.9.1-1.el5.rf                         dag                          22 M
 wxPython-devel                         x86_64                         2.8.9.1-1.el5.rf                         dag                         479 k
Installing for dependencies:
 wxGTK                                  x86_64                         2.8.10-1.el5.rf                          dag                          27 M
 wxGTK-devel                            x86_64                         2.8.10-1.el5.rf                          dag                         1.1 M


2011年9月22日 星期四

wxPython

## Install wxPython in CentOS 5.5

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
yum install wxPython.x86_64 wxPython-devel.x86_64

[references]
wxPython API

wxPyWiki
http://wiki.wxpython.org/FrontPage
http://wiki.wxpython.org/AnotherTutorial

LDS株式會社 blog
The wxPython tutorial
宅學習 python blog
Image Processing in Python
wxPython自製Control : 用Matplotlib畫圖表的FigurePanel
wxPython Programming Tutorial

http://wiki.pastwind.org/home/python-cheng-shi-yu-yan
http://garywlee.blogspot.com/2007/10/wxpython.html

2011年9月21日 星期三

Using Parallelism in Python

1. Cython (C-Extensions for Python)
http://cython.org/
wget http://cython.org/release/Cython-0.15.tar.gz
tar zxf Cython-0.15.tar.gz
cd Cython-0.15
python setup.py install

2. Multi-threading (built-in)
import threading

3. Parallel Python
http://www.parallelpython.com/content/view/17/31/

4. python-multiprocessing
Python 2.5/2.4 back port of the multiprocessing package
http://code.google.com/p/python-multiprocessing/

[references]
Does python support multiprocessor/multicore programming?
Practical threaded programming with Python
Basic Threading in Python
Understanding Threading in Python
Simple Python: a job queue with threading

http://hungic.blogspot.com/2008/08/pythonthreading.html
http://www.cppblog.com/riverbird/archive/2007/12/26/39704.html

2011年5月4日 星期三

python 處理參數時的做法

sys.argv
getopt

http://www.faqs.org/docs/diveintopython/kgp_commandline.html

global variable in python

在Python 中修改到全域變數時要宣告global


globvar = 0
def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1
def print_globvar():
    print globvar     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

2011年4月28日 星期四

upgrade python 2.4.3 to 2.5.5 in Centos 5.5 or 5.6

http://www.python.org/ftp/python/2.5.5/Python-2.5.5.tar.bz2

tar jxf Python-2.5.5.tar.bz2
cd Python-2.5.5
./configure
make
make test
make install

2011年4月22日 星期五

"SyntaxError: invalid syntax" with python 2.4

在python 2.4 使用try-except-finally 時出現(如下), 是因為要到python 2.5 時才完全支援try-except-finally


File "try-except.py", line 13
    finally:
          ^
SyntaxError: invalid syntax

in python 2.4 可以改用

try:
    try:
    except:
finally:

2011年1月25日 星期二

常用的 Python指令及其說明

常用的 Python指令及其說明 [轉載: 我是孩子王

sys模組:

argv: 命令列參數。
exit([arg]): 結束程式。
exitfunc: 若有指定此函式,程式結束前會先呼叫此函式。

getopt模組:

getopt(args, options[, long_options]): 解譯命令列參數選項的工具。

2008年10月28日 星期二

quote and unquote 應用


#!/bin/python
#coding: utf-8
# -*- coding: utf-8 -*-
import urllib

str = "測試utf8 編解碼"
str_en = urllib.quote(str).encode('utf-8')
print str_en
print urllib.unquote(str_en)
codeing 指定本文編碼

其他應用
from urllib import quote, unquote

input = u"http://test.net/~admin"
input = input.encode('utf8')
print quote(input) # http%3A//test.net/%7Eadmin
print quote(input, '') # http%3A%2F%2Ftest.net%2F%7Eadmin
print quote(input, '~/') # http%3A//test.net/~admin