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

2012年5月12日 星期六

Random String in Python


'''
Created on 2012-5-13

@author: cnp
'''

import string
import random


def random_string(size=6, chars=string.ascii_letters + string.digits):
    return ''.join(random.choice(chars) for x in xrange(size))


print random_string()



output:

bXwN6t


2012年5月10日 星期四

Hashing in Python



hashlib 裡面有md5 和sha可以使用
以下為範例

'''
Created on 2012-5-11

@author: cnp
'''

import hashlib

import hmac

x= hashlib.md5("MACA")

print x.hexdigest()

x= hashlib.sha256("udacity")

print x.hexdigest()

x= hmac.new("Secret", "HAHA").hexdigest()

print x







output:

19e165f571d62cbd06b97cc1413c86e2
6b63aae7ade6895dd8cd5b2621dccd546e278398
0224daa3a5efbaaa2414745183e20646



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;   "