2012年4月30日 星期一

Python 筆記 namedtuple and sort with lambda expression



'''
Created on 2012-5-1

@author: Maca
'''

from collections import namedtuple

People = namedtuple("People",["Name","Age"])
Lists = [People("Maca","24"),People("Puma","18"),People("Lula","30")]

print Lists

Lists.sort(key=lambda args:args.Age)

print Lists

Lists.sort(key=lambda args:args.Name)

print Lists



Outputs:

[People(Name='Maca', Age='24'), People(Name='Puma', Age='18'), People(Name='Lula', Age='30')]
[People(Name='Puma', Age='18'), People(Name='Maca', Age='24'), People(Name='Lula', Age='30')]
[People(Name='Lula', Age='30'), People(Name='Maca', Age='24'), People(Name='Puma', Age='18')]


2012年4月27日 星期五

Python筆記 Set List Dictionary




集合 Set:

fruit = set(['apple','banana','tomato'])
vegetable = set (['carrot','tomato'])

print fruit | vegetable

print fruit - vegetable

print fruit & vegetable

print fruit ^ vegetable



set(['tomato', 'carrot', 'apple', 'banana'])
set(['apple', 'banana'])
set(['tomato'])
set(['carrot', 'apple', 'banana'])




List:

names = ['maca','Maca','Tony','Grace','Maca']

print names

print names.count('Maca')

names.sort()

print names

names.reverse()

print names

names.remove('Maca')

print names



['maca', 'Maca', 'Tony', 'Grace', 'Maca']
2
['Grace', 'Maca', 'Maca', 'Tony', 'maca']
['maca', 'Tony', 'Maca', 'Maca', 'Grace']
['maca', 'Tony', 'Maca', 'Grace']






Dictionary





di = {1:"HAHA",2:"LALA",3:"HOHO"}

print di[1]

di[1] = "MIMI"

print di[1]

di["A"] = "HEHE"

print di
print "-----------------------------------------------------"

print di.keys()

print di.values()

print "-----------------------------------------------------"
print di.pop("A")

print di

del di[2]

print di



HAHA
MIMI
{'A': 'HEHE', 1: 'MIMI', 2: 'LALA', 3: 'HOHO'}
-----------------------------------------------------
['A', 1, 2, 3]
['HEHE', 'MIMI', 'LALA', 'HOHO']
-----------------------------------------------------
HEHE
{1: 'MIMI', 2: 'LALA', 3: 'HOHO'}
{1: 'MIMI', 3: 'HOHO'}


Python 筆記 Regular Express in Python

參考:http://docs.python.org/library/re.html

判斷email格式

EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")

def valid_email(email):
    return EMAIL_RE.match(email)

沒找到的話
valid_email(xxx) ==None

2012年4月26日 星期四

Python 筆記- replace

'''
Created on 2012-5-1

@author: Maca
'''


str1 = "<<>>&&"
print str1.replace("<",">")


str2 = "abc"

for (a,b) in (("a","d"),("b","e"),("c","f")):
    str2 = str2.replace(a,b)
   
print str2

import cgi

str3 = ' <   >   &   " '

str3 = cgi.escape(str3,quote = True)

print str3

str3 = ' <   >   &   " '

str3 = cgi.escape(str3,quote = False)

print str3


>>>>&&
def
 &lt;   &gt;   &amp;   &quot; 
 &lt;   &gt;   &amp;   " 

2012年4月25日 星期三

本地定時提醒 Local Notification in Windows Phone 7

參考: http://msdn.microsoft.com/en-us/library/hh202965(v=VS.92).aspx 都在 Microsoft.Phone.Scheduler 這個namespace中 除了Alarm , Remider 還有 PeriodicTask ScheduledTask ResourceIntensiveTask 等可供調用

動態產生圖片 Create Bitmap Dynamically in Windows Phone 7

緣起:
為了更改ShellTile的格式, 觀察到只可能從一個地方著手,
就是StandardTileData ,
這時候觀察他是繼承父類ShellTileData而來的,
所以這時候想說是否可以繼承ShellTileData 建立一個自己的ShellTileData,
從而改變ShellTile的格式.
但是這個嘗試失敗了, 至於失敗的原因..用過就知道了, 但也很有可能是小弟功力不足所致

這時候想了想 StandardTileData 裡面有兩個參數
分別是 BackgroundImage 以及BackBackgroundImage
想說是否可以動態產生圖片 讓他們來使用?

這時查到了一個叫做WriteableBitmap的東西 貌似可以使用
所以著手寫了一個小Demo
付上源代碼:


            Canvas canvas = new Canvas ();
            canvas.Width = 173;
            canvas.Height = 173;

            ImageBrush brush = new ImageBrush ();
            BitmapImage image = new BitmapImage ();
            image.UriSource = new Uri ("/MyDemos;component/Resource/haha.png", UriKind.RelativeOrAbsolute);
            
            image.CreateOptions = BitmapCreateOptions.None;


            brush.ImageSource = image;
            canvas.Background = brush;



            TextBlock block = new TextBlock ();
            block.Width = 100;
            block.Height = 100;
            block.TextAlignment = TextAlignment.Center;

            block.FontSize = 50;

            block.Text = "Maca";


            canvas.Children.Add (block);

            WriteableBitmap backgroundImage = new WriteableBitmap (173, 173);
            backgroundImage.Render (canvas, null);
            backgroundImage.Invalidate ();


            image1.Source = backgroundImage;




效果圖:


至於如何變成圖檔可以參考
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.extensions.savejpeg(v=vs.92).aspx

2012年4月9日 星期一

ApplicationBar in Windows Phone 7 -1

創建ApplicationBar
Code-behind

Microsoft.Phone.Shell.ApplicationBar _ApplicationBar = new Microsoft.Phone.Shell.ApplicationBar (); Microsoft.Phone.Shell.ApplicationBarIconButton _Button = new Microsoft.Phone.Shell.ApplicationBarIconButton (new Uri ("/Icons/1.png", UriKind.Relative)); _Button.Text = "確定"; Microsoft.Phone.Shell.ApplicationBarIconButton _Button2 = new Microsoft.Phone.Shell.ApplicationBarIconButton (new Uri ("/Icons/2.png", UriKind.Relative)); _Button2.Text = "取消"; _ApplicationBar.Buttons.Add (_Button); _ApplicationBar.Buttons.Add (_Button2);

 this.ApplicationBar = _ApplicationBar;


Xaml


    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" x:Name="cApplicationBar" IsMenuEnabled="True">

            <shell:ApplicationBarIconButton x:Name="appbar_button4" IconUri="/Icons/1.png" Text=" 確定 " Click="appbar_button1_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Icons/2.png" Text=" 取消 " Click="appbar_button2_Click" />

        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>