04 2012 档案

摘要:引用自:http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspxusing System;using System.Windows.Forms;using Microsoft.VisualBasic.ApplicationServices;namespace SuperSingleInstance{static class Program{[STAThread]static void Main(){Application.EnableVisua 阅读全文
posted @ 2012-04-25 22:17 谷安 阅读(366) 评论(0) 推荐(0)
摘要:通用模式# -*- coding: utf-8 -*-def mydecorator(function): def _mydecorator(*args, **kw): #在调用实际函数之前做些填充工作 res = function(*args, **kw) #做完填充后 return res #返回子函数 return _mydecoratordef mydecorator(arg1, arg2): def _mydecorator(function): def __mydecorator(*args, *kw): res = function(*args, **kw) return res 阅读全文
posted @ 2012-04-19 11:50 谷安 阅读(144) 评论(0) 推荐(0)
摘要:hosts = file('D:/Test.txt')try: for line in hosts: if line.startswith('#'): continue print linefinally: hosts.close()with:可不关闭,与C#中using 类似:with file('D:/Test.txt') as hosts: for line in hosts: if line.startswith('#'): continue print host要使用with,需要类实现 __enter__ 与 __ex 阅读全文
posted @ 2012-04-19 11:42 谷安 阅读(568) 评论(0) 推荐(0)
摘要:Example1:def fibonacci(): a, b = 0, 1 while True: print 'abc' yield b a, b = b, a+bfib = fibonacci()fib.next()[fib.next() for i in range(10)] #感觉这种写法很精妙Example 2: yield 作为表达式 Send 来填充 yield表达式,throw 抛出异常,close抛出GeneratorExit异常def psychologist(): print 'Please tell me your problems' w 阅读全文
posted @ 2012-04-09 16:24 谷安 阅读(166) 评论(0) 推荐(0)
摘要:class MyIterator(object): def __init__(self, step): self.step = step def next(self): if self.step == 0: raise StopIteration self.step -= 1 return self.step def __iter__(self): return self [el for el in MyIterator(4)] 阅读全文
posted @ 2012-04-09 11:23 谷安 阅读(314) 评论(0) 推荐(0)
摘要:seq = ['one', 'two', 'three']def FormatStr(pos, element): return '%d: %s' % (pos, element)[FormatStr(i, el) for i, el in enumerate(seq)] 阅读全文
posted @ 2012-04-09 10:30 谷安 阅读(184) 评论(0) 推荐(0)