星期五, 6月 25, 2010

[Django] Django Web Framework Introduction


88頁 十分言簡意該的投影片.....

不過對於完全沒學過的人而言 可能不是那麼好懂

有點知道的話 可以pick-up 十分快

有幾個slides 我也才學會一些基本的語法

還蠻值得參考 可以掃過一遍複習一下....

The Django Web Application Framework

星期四, 6月 24, 2010

[Python] Debugging Python by PDB


最近寫Python寫到快瘋了..... 被Visual Studio寵壞的我

其實十分不習慣Eclipse....用vi也是不是很順

Debugging更痛苦........ 所以下定決定好好研究一下python debugging 的技巧.........

以下這一篇大概是比較step by step很好簡易的介紹...

the Python debugger � Python Conquers The Universe

[Summary]

1.

在code任何地方 可以下中斷點 by inserting the line below
pdb.set_trace # 但記得要import pdb

2. 然後就可以直接執行程式了 python your_program.py

3. 就會進入 (Pdb) 這時候就可以開始用console來debugging了

重點的指令如下

n : next statement

[ENTER] : repeat last debugging command

q: quit the debugging

p : print..... (example: p a,b,c)

c : continue

l : list the current line of program

s : step into subroutines

r : return the current subroutines

![command python scripts] : execute some python scripts (example: !b = 'BBB')









星期四, 6月 17, 2010

[Python] Python Profilers in Default

26.4. The Python Profilers — Python v2.6.5 documentation

python Default附三套
1. cProfile

2. Profile

3. Hotspot

簡而言之, 選cProfile就對了.... 又快又新又完整.....

based on cProfile 開始Survey Django相關的Profiler by using cProfile

可以找到的是

Django Middleware using cProfile --> Django snippets: Profiling middleware using cProfile

Profiling each request at Apache/MOD_WSGI level: ProfilingDjango: profiler-cprofile.py - Django - Trac


不過稍微google一下才發現....cProfile的資源比較少,hotspot的比較多一點

所以可能要refer 到這篇Profiling Django (based on Hotspot).

這篇可以做function level的profining (用decorator的方式),middleware level或是整個server level的 (MOD_PYTHON)


另外django-profiling 還有django-logging似乎都已經不再maintain了 所以指向的library與是 django debug toolbar

看起來django debug toolbar support features set是蠻吸引人的 等我試用完 再跟大家報告心得吧... 不過這裡有一篇看起來很棒的簡介.....

  • Detailed SQL queries
  • Request timer
  • Common HTTP headers
  • Cache statistics
  • HTTP variables
  • Settings variables
  • Profile module
  • Templates rendered
  • Logging message output


[Django] Middle Concept and Introduction

Django | Middleware | Django documentation

這篇在講解Django的Middleware的細節..... 終於有空開始看這些東西了 讀完之後 Summary如下.....

[Summary]

主要是Middleware會overwrite 幾個重要的funcitons...


1. process_request(self, request)

每一個http request都會進來, 還沒分派到view之前......... 是top-down的order來呼叫 (即第一個到最後一個)

可以選擇return None or return HttpResponse

如果是return None --> 繼續call 下一個middleware

如果return Reponse --> 停止call下一個......

2. process_view(self, request, view_func, view_args, view_kwargs)

分派好view之後,馬上call這個function........也是top-down order

可以選擇return None or return HttpResponse (同上 process_request, i.e. 不會續call if return Response)

3. process_response(self, request, response)

已經有HTTP Response......注意,這裡是bottom-up order

must return HttpResponse (alter or create new one)

Always會call下一個middleware....可以所以預期所有的middleware都會被call到這個function

4. process_exception(self, request, exception)

Process requets的時候在view throw exception......注意,這裡是bottom-up order

可以選擇return None or return HttpResponse (同上 process_request, i.e. 不會續call if return Response)


基於以上的論點 參考下圖可以馬上了解整個流程


Middleware application order.


--> __init__ of middleware

__init__不可以有參數,而且只會被call 一次 after web server starts up.

--> Runtime switch Django Middleware

可以在Middlewared的__init__ method raise django.core.exceptions.MiddlewareNotUsed exception
不過要restart web server.

這點我可以用到可以runtime configure setting, such as 開啟一個ProfilerMiddler, ...etc
在Web 界面可以更改到/etc/django.settings 然後再restart apache or web server
然後就可以configure 成另一個mode without code modification.

(雖然也可以直接改 django_project.settings.... 不過的確比較麻煩一點點....呵)


--> Django Default Support 的Middleware List

像是Cache middleware, GZIP middleware, Conditional GET middleware, Locale middleware, Message middleware, Session middleware, Authentication middleware, CSRF protection middleware, Transaction middlware.



星期二, 6月 15, 2010

[Django] Django Deployment with Fabric

Django Deployment with Fabric

在Clouding的Deployment我覺得是一個蠻複雜的事情

於是我也玩過一下Fabric 也希望最終於的deployement能全靠Fabric解決....

這一篇Slide 有點相見恨晚 因為很多東西我已經走過一遭 不過看完之後 也能印證很多想法大家看法一致 .....

這也是很好的一個Fabric的Introduction..... :D

[update 6/18]

在網路上有找到一篇 跟我類似想法的人 也做了跟fabric有關的deployement 可參考


[Django] Very Good Introduction to Django

Python & Django: Fast and easy web application development

這一篇雖然十分短 但是把Django最重要的架構跟精神在幾個slide揭露無遺...

MTV的架構用圖表畫出 十分清楚.....

Model --> Model
Template --> View
View --> Controller

對應到我們熟悉的MVC架構...

[Django] Django Con High Performance Django

Django Con High Performance Django

針對Django High Performance提供了許多有用的建議

1. Template Engine可以換更快的 像是換成Jinja

2. 作Caching....object cache and browser cache, using such as Squid, Vamish or Nginx

3. Profiling your code and database queries
Slide裡也有Example的Screenshot可以參考....


[Django] Debugging Django Slide


這個slide (Debugging Django) 大概簡介了怎麼Debuggin Django的一些方法與細節

十分有參考價值...

Summary 如下

基本的就直接用Python的logging 可以logging to console, file, ..etc

db-error-log: 這個module可以幫忙把exception記錄在database裡

ProfilerMiddleware: 可以作Profiling 直接在Browser上打Url 可以看到Profiling與hotspot的結果

DebuggerFooter: add a SQL footer into every HTML files




重新開始.....

歷經兩年 決定再開始寫blog.....作一些技術上的記錄...

之後Survey的方向會有很大的出入.....

再之前都是集中在WPF/Silverlight 微軟的.net framework

接下來就是Clouding/Web Services方面的技術了....