zoukankan      html  css  js  c++  java
  • 总结的小知识点(更新)

    ConfigParser模块:

    #!/sur/bin/env python
    # -*- coding:utf-8 -*-
    __author__ = 'ganzl'
    import ConfigParser
    config = ConfigParser.ConfigParser()
    config.read('i.cfg')
    
    secs = config.sections()
    print 'sections:',secs
    options = config.options('group2')
    print 'options:',options
    
    item_list = config.items('group2')
    print 'item:',item_list
    for k,v in item_list:
        print k,v
    
    i.cfg:
    [section1]
    k1 = v1
    k2:v2
    [section2]
    host = 1.0.0.1
    [group2]
    gan : 123
    host1 = 2.0.0.2
    host2 = 3.0.0.3
    [111]
    a : b
    
    作用:对同一模块的服务器进行批量命令操作:
    python python_cmd.py group2 'ifconfig'
    View Code

    递归小demo:

    #!/sur/bin/env python
    # -*- coding:utf-8 -*-
    __author__ = 'ganzl'
    def func(arg1,arg2,times):
        times += 1
        count = arg1+arg2
        if count>1000:
            return  count
        ret = func(arg2,count,times)
        #return func(arg2,count,times)
        print ret,times-1
    
    ret = func(0,1,1)
    print ret
    View Code
    #!/sur/bin/env python
    # -*- coding:utf-8 -*-
    __author__ = 'ganzl'
    def func(arg1,arg2,times):
        times += 1
        count = arg1+arg2
        if count>1000:
            return  count
        #ret = func(arg2,count,times)
        return func(arg2,count,times)
        #print ret,times-1
    
    ret = func(0,1,1)
    print ret
    
    比较下这两个小demo。类似这个递归,其他源码经常看到
    View Code

     re:

    字符:

      . 匹配除换行符以外的任意字符
      w 匹配字母或数字或下划线或汉字
      s 匹配任意的空白符
      d 匹配数字
       匹配单词的开始或结束
      ^ 匹配字符串的开始
      $ 匹配字符串的结束

    次数:

      * 重复零次或更多次
      + 重复一次或更多次
      ? 重复零次或一次
      {n} 重复n次
      {n,} 重复n次或更多次
      {n,m} 重复n到m次

    IP:
    ^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$
    手机号:
    ^1[3|4|5|8][0-9]d{8}$
    View Code
    #!/sur/bin/env python
    # -*- coding:utf-8 -*-
    __author__ = 'ganzl'
    import re
    #以加减乘除或者单独除进行切割
    content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
    new_content1 = re.split('[+-*/]+', content)
    new_content2 = re.split('*', content, 1)
    print 'new_content1',new_content1
    print 'new_content2',new_content2
    #将空格去掉,并以(加减乘除数字重复一次的形式)切割
    inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
    inpp = re.sub('s*','',inpp)
    new_content3 = re.split('(([+-*/]?d+[+-*/]?d+){1})', inpp)
    print 'new_content3:',new_content3
    #截取格式:(加减乘除数字,带小数,重复两次以上的形式)切割
    inpp = '1 - 2 * ( (60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ) '
    inpp = re.sub('s*','',inpp)
    content = re.search('(([+-*/]*d+.*d*){2,})', inpp).group()
    before, ret, after = re.split('(([+-*/]*d+.*d*){2,})',inpp,1)
    print 'content',content
    print 'before:',before
    print 'ret:',ret
    print 'after:',after
    
    
    上面用到sub/split方法。
    还有其他方法(下次用到时再实践下就好)
    obj = re.match('d+', '123uuasf')
    obj = re.search('d+', 'u123uu888asf')
    re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()
    obj = re.findall('d+', 'fa123uu888asf')
    View Code

     看高人用正则跟递归写的计算器:

    http://www.cnblogs.com/wupeiqi/articles/4949995.html

  • 相关阅读:
    (2/24) 快速上手一个webpack的demo
    (1/24) 认识webpack
    module.exports 、exports、export、export default的区别
    Git同时提交到多个远程仓库
    @codeforces
    @loj
    @bzoj
    @loj
    @bzoj
    @bzoj
  • 原文地址:https://www.cnblogs.com/shoug/p/5151659.html
Copyright © 2011-2022 走看看