zoukankan      html  css  js  c++  java
  • Head First Python 学习笔记(第二章:分享你的代码)

    共享你的代码

    Python提供了一组技术,可以很容易地实现共享,这包括模块和一些发布工具:

    • 模块允许你合力组织代码来实现最优共享。
    • 发布工具允许你向全世界共享你的模块。

    函数转换为模块

    1.把第一章中的代码存文件名为“nester.py”的文件,代码如下

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

    2.注释代码:"""三个引号就是注释"""

    3.在IDLE编辑窗口家在nester.py文件,然后按下F5运行这个模块代码:

     这时Python shell“会重启”,出现一个带有下面字符串的提示窗口

       =============RESTART==============

       然后运行代码

    movies=['The Holy Grail', 1975, "Terry Jones & Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]]
    print_lol(movies)

    现在我们开始准备发布

    1. 为模块创建一个文件夹名为“nester”,然后吧nseter.py文件复制到这个文件夹中
    2. 在新的文件夹中创建一个名为“setup.py”的文件(文中的一些内容可自己更改)
    from distutils.core import setup
    setup(
            name        ='nester',
            version     ='1.0.0',
            py_modules  =['nester'],
            author      ='Your name',
            author_email='Your mail',
            url         ='Your Website',
            descriptions='A simple printer of nested list',
        )

    3.构建一个发布文件:在nester文件夹中打开个终端(cd到nester文件夹下),键入

      $python3 setuo.py sdist

    4.将发布安装到你的Python本地副本中。

      $sudo python3 setup.py install

    发布已经就绪。

    发布预览

    安装后会在nester文件夹下新建build和dist两个文件夹,manifest和nester.pyc文件

    导入模块并使用

    要使用一个模块,只需把它倒入到你的程序中,或者倒入到IDLE shell:

        import nester

    import nester
    movies=['The Holy Grail', 1975, "Terry Jones & Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]]
    nester.print_lol(movies)

    注册PyPI

    打开PyPI网站:https://pypi.python.org/pypi

    点击右侧:Register

    然后在邮箱确定

    向PyPI上传代码

    在nester文件夹下打开终端,输入$python3 setup.py register

    输入username和password,保存登陆(可选)

    开始上传$python3 setup.py sdist upload(如果setup.py里的name是nester是上传不上的)

    欢迎来到PyPI社区

     完善nester,使它的显示更有层次

    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("	", end=' ')
                print(each_item)

     range() BIF可以提供你需要的控制来迭代指定的次数,而且可以用来生成一个从0直到(但不包含)某个数的数字列表以下是这个BIF的用法:

    for num in range(4):

      print(num)

    显示:

    0,1,2,3

    然后可以使用前面介绍的发布命令来更新你在PyPI上面的代码,但是一定要记得修改版本号哦

    使用可选参数

    修改def print_lol(the_list,level):→def print_lol(the_list,level=0):

    这样就可以在用户使用你的代码的时候不输入第二个参数一样可以正常运行你的代码

    然后可以使用前面介绍的发布命令来更新你在PyPI上面的代码,但是一定要记得修改版本号哦

    添加是否缩进的开关

    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("	", end=' ')
                print(each_item)
  • 相关阅读:
    1144 The Missing Number (20分)
    1145 Hashing
    1146 Topological Order (25分)
    1147 Heaps (30分)
    1148 Werewolf
    1149 Dangerous Goods Packaging (25分)
    TypeReference
    Supervisor安装与配置()二
    谷粒商城ES调用(十九)
    Found interface org.elasticsearch.common.bytes.BytesReference, but class was expected
  • 原文地址:https://www.cnblogs.com/kk8023/p/3187452.html
Copyright © 2011-2022 走看看