• 第二章:共享你的代码


    生成的python怎么发布

    准备发布

    为上节的函数建立一个放模板的文件夹

    将nester.py内容如下放入文件夹

    #!/usr/bin/python
    # -*- coding:utf-8 -*-

    def print_lol(the_list):
      for each_item in the_list:
        if isinstance(each_item,list):
          print_lol(each_item)
        else:
          print (each_item)

    在新文件夹建立一个setup.py的文件,内容如下

    from distutils.core import setup

    #python发布工具导入setup函数
    setup(
      name = 'nester',
      version = '1.0.0',
      py_modules = ['nester'],
      author = 'liuyueming',
      author_email = '274670459@qq.com',
      description = 'A simple printer of nested lists',
      url = 'http://www.liuyueming.com',
    )

    '''

    指定一些参数

    '''

    进入文件夹在当前文件夹终端端口执行命令

     python setup.py sdist

     

    生成文件MANIFEST.in文件夹dist并且提示需要创建一个README.txt文件

    执行命令安装函数

    python setup.py install

     

    新增文件夹build存放代码文件nester.py

    进入python导入模块并使用

     

    PS:需要使用命名空间才能调用函数

    假如想要嵌套列表缩进呢,修改源码,为了方便,直接做到一个脚本里面nester.py

    #!/usr/bin/python
    # -*- coding:utf-8 -*-

    movies = ["The Holy Grail",1975,"Terry Jones&Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleesl","Terry Gillam","Eric Idle","Terry Jonse"]]]def print_lol(the_list,level):

    for each_item in the_list:
      if isinstance(each_item,list):
        print_lol(each_item)
      else:
        for tab_stop in range(level):
          print(" ")
        print (each_item)
    print_lol(movies,0)

    执行报错

    可以看出是参数错误,函数在递归的时候没有指定两个参数

    修改源码在递归调用时候加上参数level

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    movies = ["The Holy Grail",1975,"Terry Jones&Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleesl","Terry Gillam","Eric Idle","Terry Jonse"]]]

    def print_lol(the_list,level):
      for each_item in the_list:
        if isinstance(each_item,list):
          print_lol(each_item,level+1)
        else:
          for tab_stop in range(level):
            print" ",
          print (each_item)
    print_lol(movies,0)

    执行输出

     

    对于一层的列表项定格显示,二层列表打印一个tab显示,以此类推

    执行过程对于一层的列表项由于level为所以不会执行print " "直接把值打印出来

    对于二层的Graham Chapman由于这是一个嵌套列表所以使level的数字加1会执行一次

    print " ",对于更加深一层的配角michael等人由于level由+1了 所以打印两次" "再打印

    对应的值

    PS:print" ",后面有,号,否则会输出回车,与题意不符 因为书里使用的是3.5版本,实际环境是2.7

    这个函数增加了新功能但是对于新用户只需要在调用函数的时候加一个参数0或者1即可,但是都有老用户呢

    可以使用默认参数定义函数def print_lol(the_list,level=0):这样对于老用户还是使用之前的命令即可,对于

    新用户如果需要缩进的层次更加深就可以调用函数的时候自己输入一个参数,例如print_lol(movies,1)

    尽管原来的用户可以用原来的形式调用这个函数,但是默认情况会打开嵌套.并不是所有人都需要,而且有人根本不希望这样

    在增加一个参数

    def print_lol(the_list,indent=False,level=0):
        for each_item in the_list:
            if isinstance(each_item,list):
                print_lol(each_item,indent,level+1)
            else:
                if indent:
                    for tab_stop in range(level):
                        print "	",
                    print (each_item)
    print_lol(movies,indent=True)
    

    默认indent参数是False不会执行打印 ,如果需要缩进的用户调用函数的时候需要手动把indent置为True

  • 相关阅读:
    四层架构设计实践
    看看node.js chat程序如何实现Ajax longpolling长链接刷新模式
    模仿igoogle【定制化、拖动排序,最大化、分屏】
    安装和配置Apache
    好书推荐《Pro ASP.NET MVC 3 Framework 3rd Edition》
    GAC和VS引用的程序集不一致?
    不要在 ASP.NET 4.5 Beta 的 Page 类事件上直接使用 async 与 await
    使用事务自动回滚来实现单元测试
    C# 如何异步查询数据库
    Linq + Jquery + Ajax 实现异步分页,批量删除,单个删除,全选,反选 ……
  • 原文地址:https://www.cnblogs.com/minseo/p/6736602.html
Copyright © 2020-2023  润新知