的笔记

随时随地编辑

Python 开发记录

里个,这些文档是作为备忘录笔记,不是为了展示,主要目标读者是某个患有严重健忘症的程序猿,如果被你不小心读到并迷惑不解,要么可以忽略,要么可以留言提问。更希望能指出错误。

Pycharm 环境设置

  • 修改快捷键: File --> Settings --> Keymap : Visual Studio

  • PTVS 环境设置

    标识符颜色设置
  • function:166,226,46
  • param:253,151,31
  • key:102,217,239
  • operator:249,38,114

  • Visual Assit 环境设置

    允许VC识别其他扩展,这里是识别成C++文件了。。。
  • Allow C/C++ files with a non-standard extension
  • 其中Add your extension to:"Tools | Options | Text Editor | File Extension | Microsoft Visual C++"不要修改了,否则会将py文件当成C++,所有#注释都被VA识别成了宏定义,很悲剧...

  • Support for python (work with pytools)
  • 扯了些淡,2012年的,也折腾了下,不知道到底其作用了没(上一个一起设置了,所以不好区分)

    Outline不能用,Open File in Solution可以用,还行

    参数组(元组*、字典**)

    将参数放到一个元组或字典中,func(params[,params2], *tuple_grp_nonkw_args, **dict_grp_kw_args)
  • 形参、元组、字典的顺序不能变
  • 三者可以缺少其中某2个或两个
  • 调用时传的参数也必须对应顺序
  • fun = lambda *z : z 同样适用

  • 代码组

    缩减相同的一组语句构成代码组。如if、while、def、class等符合语句以关键字开始,以冒号':'结束,之后接代码组。
  • 代码组可以称为子句(clause)

  • 修饰器(decorate)

    def f_decorate(func):
        print 'begin f_decorate'

        def func_wrapper(name):
            print '<func_wrapper>'
            return "<p>{0}</p>".format(func(name))

        print 'end f_decorate'
        return func_wrapper

    @f_decorate
    def g_function(name):
        print '<g_function>'
        return "lorem ipsum, {0} dolor sit amet".format(name)


    ret = g_function("John")

    预处理期

    先组装新函数对象
    #g_function = f_decorate(g_function)
    其实执行的函数对象是f_decorate,g_function只是参数
    新编译的函数暂且叫g_function@

    执行期
    按部就班调用新函数g_function@,记得存在原始函数对象g_function在新的函数对象环境里;
    而在修饰器函数体里,func_wrapper代表了新函数功能,func(name)代表了原来的函数功能,
    查看 globals和locals均可查看到新生成的函数对象
    #print globals()
    #print locals()

    运行时行号、文件名等

    (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[0]
  • <占位项1>
  • <占位项2>

  • Pip

    Tool for installing Python packages
    https://pypi.python.org/pypi/pip
  • C:\Python27\Scripts 加入环境变量,pip.exe在这里
  • <占位项2>
  • SimpleXMLRPCServer— Basic XML-RPC server

    https://docs.python.org/2.6/library/simplexmlrpcserver.html
  • Server
  • from SimpleXMLRPCServer import SimpleXMLRPCServer
    server = SimpleXMLRPCServer(('localhost', 9000))
    def my_function(a, b):
    return a * b
    server.register_function(my_function, 'multiply args')
    try:
    print 'Use Control-C to exit'
    server.serve_forever()
    except KeyboardInterrupt:
    print 'Exiting'
    
  • Client
  • import xmlrpclib
    proxy = xmlrpclib.ServerProxy('http://localhost:9000')
    print getattr(proxy, 'multiply args')(5, 5)
    

    <占位标题>

    <占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
  • <占位项1>
  • <占位项2>

  • 扯个全局对象的蛋

    这个蛋有点扯,在当前作用域,你可以使用全局变量,但是这个时候如果你用等号赋值这个名字,这个时候这个变量其实是局部重新声明的一个变量,而且不管这句是在块的任何位置。
    Instance=None
    print '1 global instance id:',id(Instance)

    class BossAIComp():
        def __init__(self):
            #Instance = self
            print '2 instance id:',id(Instance)
  • '#Instance = self'这句如果被注释了,你使用的就是全局的Instance
  • 变量名字在当前域任何地方只要赋值,这个块里这个名字都是局部的。例如"Instance = self"如果放在“print '2”下面,也会直接将这个变量当成局部的,同时在使用的时候提示这个变量没有定义

  • 再扯个import的蛋

    用法的不同形式的蛋

     ################################################

    # case 1
    import xxx.yyy.BossAIComp
    xxx.yyy.BossAIComp.Instance.say()

    # case 2
    import xxx.yyy.BossAIComp as ai
    ai.Instance.say()

    # case 3
    from xxx.yyy.BossAIComp import Instance
    Instance.say()

    # case 4
    from xxx.yyy.BossAIComp import Instance as ist
    ist.say()

    <占位标题>

    <占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
  • <占位项1>
  • <占位项2>

  • <占位标题>

    <占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
  • <占位项1>
  • <占位项2>

  • <占位标题>

    <占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
  • <占位项1>
  • <占位项2>

  • 修改记录

  • 2014-07-15 创建
  • ---
  • posted on 2014-07-14 17:00 的笔记 阅读(426) 评论(0)  编辑 收藏 引用


    只有注册用户登录后才能发表评论。
    网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理