星期四, 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方面的技術了....

星期日, 9月 21, 2008

[WPF] .net 3.5 SP1 feature - Splash Screen Support


WPF的Startup一直為人所垢病

在3.5 SP1的版本中 有做了一個很特別的Feature 可以十分容易在WPF Project隨便放一張圖變成Splash Screen

有效降低Users對Long Cold Startup Time的痛苦.... 至少... 他們知道程式還在run...

細節可以看這一篇

WPF Performance : What’s new in WPF 3.5 SP1: Splash Screen to improve perceived startup perf

[WPF] Cross Threading Binding Support of ObservableCollection



這幾天又重新在思考ObservableCollection的設計與用法

雖然Notifiy的機制已經十分方便 但是還是有效率上的考量

尤其是大量的CollectionChanged 會讓UI不是十分順....

所以在思考怎麼改善.... 其實最簡單的想法 就是開一個Thread來做

但其實是沒辦法的 因為Binding到的UI Object通常都有Thread Affinity (簡言之 就是只認自己的Thread可以用)

找到一篇解決了這個問題...........

WPF Data Binding ObservableCollection (cross thread binding support) « C# Live

星期五, 6月 13, 2008

[C#] Using Extension Methods in .net framework 2.0



在.net framework 2.0 or 3.0 沒辦法Compile Extension Method怎麼辦?

很簡單 在幾行code就可以了

namespace System.Runtime.CompilerServices
{
public class ExtensionAttribute : Attribute { }
}

方法是從下面的blog來的


The Moth: Using Extension methods in Fx 2.0 projects

星期日, 6月 01, 2008

[WPF] Initialized v.s. Loaded


常常在寫WPF的時候 搞不懂何時要用Loaded,何時要用Initialized

這一篇有十分詳盡的解說.........

In Summary

Initialized

--> Initialized
比Loaded早很多,他的時機點是所有的Property都setup好了

-->
會被raised是因為呼叫了ISupportInitialize.EndInit.... FrameworkElemtn有實現這個interface(在load XAML的時候,是BAML Loader call的)

-->
如果是自己寫code去生成,而且沒呼叫EndInit,為什麼還是有呢? 因為在串Visual Tree的時候,任何Uninitialized的children串到Parent的時候,都會先被Initialized (根據 IsInitialized的property),那如果root怎麼呢? 唔...root最後會被串到PresentationSource


--> Initialized
的順序是bottom-up,即children會先被叫起來,反之Loaded是從Top-down, 即從上到下....

Loaded

-->
被raised的時候,意味已經準備可以render了,所以ActualWidth, Layout Measurement, data-bound property都已經ready了

-->
 順序是Top-Bottom

另外文章中提到....Proprety引發Event的關係,意即

IsInitialized --> Initialized Event

IsLoaded --> Loaded Event

舉個例子 如果XAML是如此寫的話

Page裡有一個Button的話

順序會是..

  • Button.IsInitialized goes true
  • Button.Initialized event is raised
  • Page.IsInitialized goes true
  • Page.Initialized event is raised
  • Page IsLoaded goes to true
  • Button IsLoaded goes to true
  • Page.Loaded is raised
  • Button.Loaded is raised


原始文章在

Mike Hillberg's Blog on Wpf (.Net and Silverlight) : The Loaded event and the Initialized event

星期三, 5月 28, 2008

[C#] Convert a string to enum

好久沒開始C and P了 (Copy and Paste) 來記Blog....

現在開始吧.......

from Mark Wagner's .NET C# Cogitation
Convert a string to an enum

private enum Aircraft
{
Beech,
Cessna,
Piper
}


You can easily convert the string to an enum value like this:




Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);



Ideally you should wrap a try-catch around the Enum.Parse statement.

rated (enum) value

星期三, 3月 19, 2008

[Concept] Difference between Composition and Aggregation


最近在trace很大量的source code 所以開始認真想要畫一些圖記錄

以免會忘記....... 所以得學一些UML的基本概念...

一直來搞混的Aggregation跟Composition 今天終於查了google搞清楚了....

最清楚的說明 可以看這裡



結論就是....Aggregation跟Composition 都是一種Association, 一種owner ship的概念

只是Composition是更強烈的Ownership 主要是生命週期是一起的..... 而Aggregation就不是

for example

public class classA --> Composition
{
classB B = new ClassB()

~classA()
{
delete B;
}
}


public class classA -->Aggregation
{
classB *m_pB;

void SetB(classB *pB)
{
m_pB = pB; // lifecycle is not the same with parent!!
}
}






星期二, 2月 19, 2008

[Programming Concept] Coroutine

 

最近在研究C#與Script的結合,想到之前買的書「遊戲之旅 我的程式設計感悟」裡有一章在在講腳本(Script) 大概就是如何寫C++與Lua之間的整合.... 那時候才了解一個新的名詞叫Coroutine

 

本來以為這是我想要的東西,但是survey的結果似乎不是 不過也蠻好玩的,主要翻成「協同式多工」。

概念很簡單,就是如何讓兩個要run很久的function能交錯地run...好像是兩個thread在一起run,但事實是是兩個func之間作了些手腳,他們互讓的結果。

 

有興趣,可以參考以下的link..都有sample code可以試

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

http://blog.linux.org.tw/~jserv/archives/001848.html

http://www.wretch.cc/blog/LungZeno&article_id=19936457

星期一, 2月 18, 2008

[.net] How to ConfigureVisual Studio to Debug .net framework source code


找到這個網頁 十分詳盡介紹怎麼設定要debug .net framework source 的方法

不過還沒有時間試... 就先記吧

許多.net dll已經有 full source/symbols,可以直接一步步trace...

以後會比較少用reflector.net 一點 :-D


Shawn Burke's Blog : Configuring Visual Studio to Debug .NET Framework Source Code



Powered by ScribeFire.

星期二, 1月 22, 2008

[WPF]How can I get a ListBoxItem from a data bound ListBox?

天呀~~ 我長久以來的問題 居然在2005年就有人Blog回答這個問題

我卻渾然未知 一直在navigate visual tree來解決

Data會藉由Data Template展開始Visual Tree

這兩邊的Binding要如何互相串連呢? 這是很重要的問題

從Visual Tree串連到Data 我已經找到方法 就是直接看任何一個UIElement裡的DataContext

就會指到Data

但是如果從Data 想要得到展開Visual Tree的item怎麼辦呢?

原來就是下面這一行

GreekGod greekGod = (GreekGod)(listBox.Items[0]);

ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));

from http://www.beacosta.com/blog/?p=7

[WPF] ItemsPanel, ItemsTemplate, DataTemplate, ControlTemplate? How to find ItemsPanel from code?

開始初學WPF對於Template其實是很容易一知半解 尤其是一大堆Template的名詞

與許多功能重複的關係 造成搞不清楚狀況

今天我跟同事們又解釋了一次 覺得其實我之前也花了許多時間搞懂 不如就寫下來

順便剛又找到一個問題的workaround解法 也寫下來記錄一下 .....

WPF主要是希望用XAML與Code 的方式 把User Interface與Logics分開

所以可以想到我們要寫一個Control的話......

所有的behavior, property是寫在Code裡

而ControlTemplate (also can be defined in Style) 就是幫你把整個Custom Control的程式展開成Visual Tree,就是一大堆小元件,主要是Control 的Presentation....

ItemsTemplate 對於ItemsControl而言 就是將他的Items如何展開成Visual Tree....所以可想而知...ItemsTemplate是一種DataTemplates

DataTemplate 就是描述如何將Data (.net class)展開成Visual Tree (所以也就是我們assign source可以輕鬆地給.net data class,data template會為我們展開)

ItemsPanel就是對於一個ItemsControl而言 就是要去描述要如何layout所有的items,例如可以用StackPanel or WrapPanel.....


哈哈 這樣有比較清楚了嗎?..... 還有一個叫ContentTemplate

ItemsTemplate 之於ItemsControl 就等同於 ContentTemplate 之於 ContentControl
也就是描述如何將Content展開....


ok~~ 接下來是另一個問題 是我個人的經驗.....

何時要用Data Template,何時要用Custom Control,何時要用User Control

彈性而言 DataTemplate >> CustomControl >> UserControl

但是大家也知道 寫UI就是最容易東改西改 而且Control常混雜的很嚴重 也有可能很複雜

有時候很特別的一個UI 並不會需要有reuse的功能 我們可以直接用UserControl解決

但是如果是十分Common的Control 我們可以用CustomControl解決

DataTemplate則是如果能用現有的Control Composite成一個的話 就不需要花時間寫Control

大概是這樣 :D


最後一個問題 當ItemsPanel展開之後 我們沒辦法用Template.FindName找個ItemsPanel裡的Panel怎麼嗎?

我從google找到一個討論串 得enumerate Visual Tree解決 請享用

from http://www.msdner.net/dev-archive/210/153-119-2109609.shtm

private T FindItemsPanel(Visual visual)

{

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)

{

Visual child = VisualTreeHelper.GetChild(visual, i) as Visual;

if (child != null)

{

if (child is T && VisualTreeHelper.GetParent(child) is ItemsPresenter)

{

object temp = child;

return (T)temp;

}

T panel = FindItemsPanel(child);

if (panel != null)

{

object temp = panel;

return (T)temp; // return the panel up the call stack

}

}

}

return default(T);

}

星期五, 1月 18, 2008

UI與Logics可[WPF]以完全分離嗎? TemplatePart可以解決不能分離的部分

一直以來 寫WPF Custom Control會往UI與Style (ControlTemplate)作分離的假設實作

但是發現這太難了~~ 如果想要加一下比較特別的功能 或是 Usability

UI跟Logics其實是有隱含的共識與假設的........ 這部分TemplatePart就可以用來解決這方面的問題

可以參考以下這二個link

http://www.cnblogs.com/zhouyinhui/archive/2007/12/01/979715.html
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!634.entry

怎麼寫呢? 例如 ( from 以上Link的Copy & Paste)

[TemplatePartAttribute(Name = "PART_EditableTextBox", Type = typeof(TextBox))]
[TemplatePartAttribute(Name
= "PART_Popup", Type = typeof(Popup))]
[LocalizabilityAttribute(LocalizationCategory.ComboBox)]
[StyleTypedPropertyAttribute(Property
= "ItemContainerStyle", StyleTargetType = typeof(ComboBoxItem))]
public class ComboBox : Selector


public override void OnApplyTemplate()
{
base.OnApplyTemplate();

Button mybtn
= base.GetTemplateChild("PART_BTN");

if (mybtn != null)
{
mybtn.Click
+= new RoutedEventHandler(mybtn_Click);
}

}

星期二, 1月 01, 2008

WPF Asynchronous DataBinding


一直以來 我想要寫一個Async DataBinding... 因為大量使用Data Binding 可能會影響整個UI的Performance

結果我找到這篇.... 唔... 是我誤會WPF... 其實是有Asynchronous Mode的...


Beatriz Costa » How can I control whether my Binding is synchronous or asynchronous?


另外還有一篇也很有意思 有關cross threading的操作.... 蠻值得參考的

Beatriz Costa » How can I propagate changes across threads?

[WPF] .netfx 3.5 IDataErrorInfo


.net 2.0的Databinding似乎有這個用法 但是.net 3.0沒有 所以特別在.net 3.5加入進來....

WPF 3.5 的IDataErrorInfo的用法 -- &gt;

WPF IDataErrorInfo and Databinding

Better WPF binding in .NET 3.5 « WPF Wonderland

Windows Presentation Foundation SDK : Data Validation in 3.5



Powered by ScribeFire.