您的位置:首页 > 编程语言 > Python开发

ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点

2013-07-05 08:59 531 查看



在做WPFMVVM中经常会遇到一些Model、ViewModel的属性添加添加私有字段和更改通知方法来支持Binding。
比如把:public class Test{ public string TestData1{get; set;} public string TestData2{get;set;}}
变为:public class Test : INotifyPropertyChanged{ private string _testData1; public string TestData1 { get{return _testData1;} set { _testData1 = value; OnPropertyChanged("TestData1"); } }
private string _testData2; public string TestData2 { get{return _testData2;} set { _testData2 = value; OnPropertyChanged("TestData2"); } }}
上述工作如果在遇到很多属性时, 会太累,而且容易出错。
因此, 我写了一个python的小工具,专门处理添加私有字段和更改方法通知的添加。
由于是第一次用python,中间有些知识点记下来。
1)用#注释代码, like:#version:12)用import导入其他库, like:import re3) re是正则表达式库,shutil是copy或者备份文件的库4)class的定义, like:class YourClassName:5)构造方法的定义, like:def __init__(selft, yourParameter):6) 方法的定义, like:def YourMethodName(self, yourParameter):7) self类似c、c++中的this指针8)python中的正则表达式字符串可以用?P<your_key>来标记一个符合的值, like:pattern = "((?P<space>\s*)public\s+"testData = " public "m = re.match(pattern, testData)print(m.group('space')
如果一个测试数据的开头有很多空格等符合\s*条件的, 我们可以用space作为关键字来索引到具体空格。此示例中为4个空格9)字符串的格式化可以用“%(your_key)s”%{'your_key':your_value}, like:self.classPattern = "(\s*public\s+%(class)s\s+%(derived)s\s+%(colon)s+\s*%(base)s)"%{'class':"class", 'derived':"\w+", 'colon':":", 'base':self.baseClassName}10)如果字符串跨行, 请在字符串行尾加上\11)re.match()返回值若不匹配则为None12)可以将正则表达式的匹配结果转为字典, like:m = re.match(pattern, testData)yourDict = m.groupdict()13)可以用raise Exception("Your exception information")来抛出异常14)用lower()来将字符串转为小写15)用replace(old, new, length)来替换字符串, 并且可以指定替换几个16)用input()可以接受控制台输入17)if,else,for语句的定义:if methodName == "": methodName = "OnPropertyChanged"for line in fileBackup: print("Line is: %s"%line)18)文件操作有open,write,close等方法, 其中open接受打开的方式, 比如“r+"代表为可读写, "w+"代表先清空源文件(若存在)再读写19)可以用cxfreeze来打包py为exe文件,安装cxfreeze后,打开命令行进入python路径:C:\Python33\Scripts, 使用cxfreeze PropertyChanged.py --install-dir=D:/OnPropertyChanged
运行成功后,OnPropertyChanged目录下会有exe,python33.dll以及相关依赖的pyd文件
download:http://sourceforge.net/projects/cx-freeze/?source=dlp20)另外发现TortoiseGit和Sublime Text很好用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: