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

星期二, 10月 20, 2015

Django 設定建議

Django 設定好文章
reference:
http://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/
https://docs.djangoproject.com/en/1.8/intro/reusable-apps/


在Django中,樣板templates目錄及statics目錄可以有很多種設定方法:
Project based: 在setting.py中,設定

#設定靜態檔案的系統根目錄
STATIC_ROOT = os.path.join(BASE_DIR, '/static_root') 
STATIC_URL = '/static/'

*其中STATIC_URL是當Server看到/static/目錄的request, 則Server會到STATIC_ROOT下映射去找資料,如果找不到,則會到每個 App目錄下去找static子目錄查詢內容。
*在template中的用法 (helpers function)
static: To link to static files that are saved in STATIC_ROOT Django ships with a static template tag.
{% load static % }
src="{% static "images/hi.jpg" %}"

#設定樣版檔案的系統目錄
TEMPLATE_DIRS = (
    join(BASE_DIR,  'templates'),
)
Deprecated since version 1.8: Set the DIRS option of a DjangoTemplates backend instead. 1.8版要改設一定為DIRS 
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        join(BASE_DIR,  'templates'),
    ],....}

*當Python看到/template/目錄的request, 則Server會到TEMPLATE_DIRS 下映射去找資料,如果找不到,則會到每個 App目錄下去找/templates/子目錄查詢內容。
*在每個app的/template/目前下,建議再建立app名字同名子目錄,方便render函式辨視及搜尋不同app的樣版檔案。例如render(polls/index.html),則系統會依路徑去找,所以會依下列方式 去找
mysite/templates/polls/index.html ,如果找不到,就會找
mysite/polls/templates/polls/index.html


#設定上傳檔案的系統根目錄

MEDIA_ROOT = os.path.join(BASE_DIR, '/upload_root') 
MEDIA_URL = '/upload/'

*在FileField的upload_to會把檔案存在MEDIA_ROOT指定的路徑中。
*在template中的用法 (helpers function)
get_media_prefix: Similar to the get_static_prefix, get_media_prefix populates a template variable with the media prefix MEDIA_URL, e.g.:

{% load static %}
data-media-url="{% get_media_prefix %}">

#設定每個APP urls 路由方式
polls/
    __init__.py
    admin.py
    models.py
    tests.py
    urls.py
    views.py
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

其中^polls/結尾不能有$字號, Django 會移除己配對的字串,再把後續字串送到app目錄下的urls模組處理。
polls/urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

星期五, 7月 10, 2015

Python Flask Quick Guide

Django 功能強,但是不易學習,在設定上比較麻煩,Flask比較容易上手,可以快速架出restful 服務

Videos:

https://www.jetbrains.com/pycharm/documentation/

使用rest api及angular js的好影片
PyCharm Web Magic: Building a Pinterest Clone

Deploying Flask on Google App Engine

使用template方式架的影片
PyCharm - Flask Tutorial



Flask Authentication

http://blog.miguelgrinberg.com/post/restful-authentication-with-flask


星期二, 6月 23, 2015

星期日, 6月 21, 2015

Django Class Base View Examples

https://docs.djangoproject.com/en/1.8/ref/class-based-views/

Generic display views
Model Name as suffix name, e.g., author_xxx.html
DetailView
_detail.html
ListView
_list.html


Generic editing views

FormView

CreateView
_form.html
UpdateView
_form.html
DeleteView
_confirm_delete.html









Reference:

好投影片
Django class-based views: survival guide for novices
http://www.slideshare.net/giordanileonardo/django-cb-vssurvivalguidefornovicesv2

Best Practices for Class-Based Views
http://www.slideshare.net/starwilly/two-scoopsofdjangocbv


好範例,很小很容易理解

Django 1.8 Tutorial - 1. A Minimal Application Made Using Generic Class Based Views
http://riceball.com/d/content/django-18-minimal-application-using-generic-class-based-views

Notes:
練習中少了一些命令
settings.py -->'comment'
python manage.py makemigration
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Pycharm note:
把專案根目錄Make Directory as sources root 可以避免urls.py找不到相對import 檔提示問題
https://www.jetbrains.com/pycharm/help/configuring-folders-within-a-content-root.html


Getting started with Generic Class Based Views in Django
http://www.onespacemedia.com/news/2014/feb/5/getting-started-generic-class-based-views-django/

Django 中文習學資源


Django筆記
5 Stars, 描述很完整,技術細節說明很清楚的學習資源
http://dokelung-blog.logdown.com/posts/235592-django-notes-table-of-contents

星期五, 4月 24, 2015

如何在Windows下安裝SciPy, matplotlib

在Windows下安裝SciPy, matplotlib 實在是很麻煩的事,上網查了一下,決定試用

http://conda.pydata.org/miniconda.html


Miniconda是Anaconda下的工具,Anaconda把很多在Windows需要編譯的package都編好了,直接安裝就可以,可以省很多事....相較之前的痛苦,一整個輕鬆愉快

把舊的python砍掉重練. 下戴miniconda後重新安裝....

安裝packeage方式如下:

$ conda install numpy

也可以產生像virtualenv的虛擬環境,解決dependency的問題

參閱https://gist.github.com/ccwang002/449159cc2a05b1011467

> conda create -n ngs python=2.7 pip
使用它很簡單就 activate ngsdeactivate, 細節可以看 conda 的說明文件。總之在這邊
> activate ngs
Activating environment ngs ...
[ngs]> conda install numpy scipy
...
Proceed ([y]/n)? 



在pycharm重設 interpter的路徑就好了:

http://unlikenoise.com/setup-pycharm-anaconda-python-windows/



星期一, 4月 20, 2015

安裝Windows 版NumPy及SciPy

NumPy及SciPy在windows 安裝,都會有compile errors...

http://www.scipy.org/scipylib/download.html

要安裝的話,建議到sourceforge安裝預先compile的all-in-one版。

http://sourceforge.net/projects/numpy/files/

http://sourceforge.net/projects/scipy/files/

後記補充,由於這種方式安裝matplot等套件不方便,建議改用miniconda....

Python Text Classification using Naive Bayes and scikit-learn


Feature extraction (特徵擷取) [5]

CountVectorizer implements both tokenization (英文分詞) and occurrence counting(計算英文文字出現計數) in a single class:
>>>
>>> from sklearn.feature_extraction.text import CountVectorizer
This model has many parameters, however the default values are quite reasonable (please see the reference documentation for the details):
>>>
>>> vectorizer = CountVectorizer(min_df=1)
>>> vectorizer                     
CountVectorizer(analyzer=...'word', binary=False, decode_error=...'strict',
        dtype=<... 'numpy.int64'>, encoding=...'utf-8', input=...'content',
        lowercase=True, max_df=1.0, max_features=None, min_df=1,
        ngram_range=(1, 1), preprocessor=None, stop_words=None,
        strip_accents=None, token_pattern=...'(?u)\\b\\w\\w+\\b',
        tokenizer=None, vocabulary=None)
Let’s use it to tokenize and count the word occurrences of a minimalistic corpus of text documents:
說明: 
fit函式代表tokenize,加入到字典陣列vocabulary 
fit(raw_documents[, y])Learn a vocabulary dictionary of all tokens in the raw documents.
transform函式用來計計算英文文字出現計數
transform(raw_documents)Transform documents to document-term matrix.
fit_transform(raw_documents[, y])Learn the vocabulary dictionary and return term-document matrix.
>>>
>>> corpus = [
...     'This is the first document.',
...     'This is the second second document.',
...     'And the third one.',
...     'Is this the first document?',
... ]
>>> X = vectorizer.fit_transform(corpus)
>>> X                              
<4x9 matrix="" numpy.int64="" of="" sparse="" type="">'
    with 19 stored elements in Compressed Sparse ... format>
The default configuration tokenizes the string by extracting words of at least 2 letters. Each term found by the analyzer during the fit is assigned a unique integer index corresponding to a column in the resulting matrix. This interpretation of the columns can be retrieved as follows:
>>>
>>> vectorizer.get_feature_names() == (
...     ['and', 'document', 'first', 'is', 'one',
...      'second', 'the', 'third', 'this'])
True

>>> X.toarray()           
array([[0, 1, 1, 1, 0, 0, 1, 0, 1],
       [0, 1, 0, 1, 0, 2, 1, 0, 1],
       [1, 0, 0, 0, 1, 0, 1, 1, 0],
       [0, 1, 1, 1, 0, 0, 1, 0, 1]]...)

說明: 
經過特徵擷取後,可以利用get_feature_names()取得特徵索引字串字典,接著對映到結果計數陣列。
呼叫X.toarray()可以看出,每個文件,例[0, 1, 1, 1, 0, 0, 1, 0, 1]所對映到的英文字計數....
'This is the first document.' ->['and', 'document', 'first', 'is', 'one','second', 'the', 'third', 'this']


get_feature_names()Array mapping from feature integer indices to feature name



References:

1. Machine Learning Tutorial: The Naive Bayes Text Classifier

2. Naive Bayes

3. Working With Text Data — scikit-learn 0.16.1 documentation

4. Text Classification

5. Feature extraction

Videos:

高一下數學3-0引言01什麼是機率

高一下數學3-3A觀念01條件機率的概念


星期三, 2月 25, 2015

Django and Facebook API

Good Reference:
Signing Up and Signing In: Users in Django with Django-AllAuth
https://speakerdeck.com/tedtieken/signing-up-and-signing-in-users-in-django-with-django-allauth

使用allauth套件對新手最方便

DjangoCon 2013 - Rapid prototyping and communicating with clients
http://www.slideshare.net/katychuang/kat-rapid-prototyping
django網站規劃好文章


星期日, 1月 11, 2015

Unicode, UTF-8, UTF-16, Python 2.x, 3.x 中文編碼

Unicode, UTF-8, UTF-16, BOM的說明

Reference: http://dnowba.blogspot.tw/2012/07/ansiunicodeutf-8utf-16bom.html

在命令提示視窗(Command Prompt)顯示UTF-8內容


http://blog.darkthread.net/post-2011-08-11-command-prompt-codepage.aspx

摘要:

  • Unicode (=UTF-16), 固定2bytes來編碼,解決多國語系呈現問題。
  • Unicode轉換格式(Unicode Transformation Format,簡稱為UTF)
  • UTF-8可變動式編碼,英文數字用1 byte來編、中文通常用3 bytes編碼。設計目的在於英語系資料可只用1碼,大幅簡少原Unicode 2碼的空間浪費。
  • BOM (Unicode Byte Order Mark),在檔頭加上二個byte的空間,為了解決CPU的BE和LE的問題。(早期Mac 系統主要是 Big Endian(BE), PC 系統則是使用 Little Endian(LE)。)
  • Difference between Big Endian and little Endian Byte order, http://stackoverflow.com/questions/701624/difference-between-big-endian-and-little-endian-byte-order
  • UTF-8在Windows軟體(如記事本)存檔時,可能會加入EF BB BF這個BOM,因為UTF-8編碼可以透過演算法偵測,理論上是沒必要的。所以Linux及Mac上的文字編輯器都不會加,造成一些程式碼在Linux及Mac平台上處理會有問題。

Python 2.x及 3.x的編碼

Python 2.x’s support for Unicode, Reference: https://docs.python.org/2/howto/unicode.html
Python 3.x’s support for Unicode, Reference: https://docs.python.org/3/howto/unicode.html
python 的編碼, http://openhome.cc/Gossip/Encoding/Python.html

摘要:


  • 在Python 2.x,程式中所有字串,其實都是原始位元組集合。也就是傳統的ascii編碼。
  • 如果原始碼中寫了非ASCII字元串,必須在第一行放置編碼聲明(encoding declaration)。例如:# coding=Big5。在上面情況下,len('中文')函式結果會是以byte來計算=4
  • 一般建議,第一行編碼聲明為# coding=utf-8
  • Unicode不等於utf-8編碼,所以讀取及儲存Utf-8資料,需要進行轉碼,轉成Unicode再處理。
  • 為了支援Unicode,Python 2.x提供了u前置字來產生unicode物件。len(u'中文')函式結果會是以真正的"字數"來計算=2
  • 在2.x中,可以用 s = unicode('abcdef')語法建立unicode,unicode() 建構式的原形 unicode(string[, encoding, errors]),其中encoding預設的編碼是ASCII,建構式會把string 參數內容,根據由encoding參數所指定的編碼,如ASCII轉成unicode。如果沒有改變編碼,直接用預設方式建立中文字,如unicode('中文')會產生錯誤,如UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 6。必須指定原始編碼,unicode()函式才能安全的把原生編碼轉過去Unicode, e.g, unicode('中文', 'big-5')。
  • unicode的encode()方法則可指定實現編碼,將之轉為代表位元組實現的str實例。即把字串從unicode--> big5。
  • 假如text文字為unicode,呼叫b_str = text.encode('big5')則會回傳big5的文字回去b_str。
  • 原生字串str的decode()會將字串轉換為unicode物件。即把字串從big--> unicode。
  • 假如b_str文字為原生字串,b_str.decode('big5')則會回傳Unicode的文字回去。


  • 在Python 3.x中,預設.py檔案必須是UTF-8編碼。如果.py檔案想要是UTF-8以外的編碼,同樣必須在第一行放置編碼聲明。
  • Python 3.x中的字串都是Unicode。