package.md 6.0 KB
Newer Older
F
feilong 已提交
1 2
# Python 打包

M
Mars Liu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Linux 系统的 shell 命令 tree,可以显示目录树结构,例如:

```shell
~/jobs/csdn-nlp/skill_tree_parser (master) » tree src                                                                                    mars@millydeMacBook-Pro
src
├── __init__.py
├── __pycache__
│   └── __init__.cpython-39.pyc
├── skill_tree
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   └── __init__.cpython-39.pyc
│   ├── doc.py
│   ├── exercises
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── __init__.cpython-39.pyc
│   │   │   ├── markdown.cpython-38.pyc
│   │   │   └── markdown.cpython-39.pyc
│   │   ├── init_exercises.py
│   │   ├── markdown.py
│   │   └── marked_math.py
│   ├── img.py
│   ├── init.py
│   ├── template
│   │   ├── __init__.py
│   │   └── main.py
│   └── tree.py
└── test
    ├── __init__.py
    ├── __pycache__
    │   └── __init__.cpython-39.pyc
    ├── exercises
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-39.pyc
    │   │   └── markdown_test.cpython-39.pyc
    │   └── markdown_test.py
    └── skill_tree
        └── __init__.py

11 directories, 26 files
```

F
feilong 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
打印出 Python 打包目录层次结构,类似命令行 tree 的效果,本题难度微提,请仔细阅读下面的注释,思考后做题。

```python
# -*- coding: UTF-8 -*-
def dump_node(node, space, last):
    space = ''.join(space)
    if last:
        print(f"{space}└── {node}")
    else:
        print(f"{space}├── {node}")

def tree_project(node, depth, space):

    count = len(node)
    i = 0

    # 遍历一层下的节点
    while i < count:
        sub_node = node[i]

        # 获取子目录数据
        sub_dir = None
        if i+1 < len(node):
            if type(node[i+1]) == type([]):
                sub_dir = node[i+1]
                i += 1

        # 判断是否是本级别最后一个节点
        last = i == len(node) - 1

        # 打印当前节点
        dump_node(sub_node, space, last)

        # 如果含有子目录,则递归打印子目录结构
        if sub_dir is not None:
            # TODO(You): 请在此实现计算当前层缩进空格的代码

            tree_project(sub_dir, depth+1, next_space)

        i += 1


def test():
    project_files = [
        "packaging_tutorial",
        [
            "LICENSE",
            "pyproject.toml",
            "README.md",
            "setup.cfg",
            "src",
            [
                "example_package_1",
                [
                    "__init__.py",
                    "example_1.py"
                ],
                "example_package_2",
                [
                    "__init__.py",
                    "example_2.py"
                ],
            ],
            "tests/"
        ]
    ]

    print()
    print("# 介绍一下 Python 打包")
    print("* Python 打包教程:https://packaging.python.org/tutorials/packaging-projects/")
    print("* 一个典型 Python 包的目录结构如下:")
    print()
    tree_project(project_files, 0, [''])
    print()

if __name__ == '__main__':
    test()
```

请选出下列能**正确**实现`计算当前层缩进空格数`的代码是。

## template

```python

def dump_node(node, space, last):
    space = ''.join(space)
    if last:
        print(f"{space}└── {node}")
    else:
        print(f"{space}├── {node}")


def tree_project(node, depth, space):

    count = len(node)
    i = 0
    while i < count:
        sub_node = node[i]

        sub_dir = None
        if i+1 < len(node):
            if type(node[i+1]) == type([]):
                sub_dir = node[i+1]
                i += 1

        last = i == len(node) - 1
        dump_node(sub_node, space, last)

        if sub_dir is not None:
            next_space = None
            if depth == 0:
                next_space = ['']
            else:
                next_space = [*space]
                if not last:
                    next_space.append('|   ')
                else:
                    next_space.append('    ')
            tree_project(sub_dir, depth+1, next_space)

        i += 1


def test():
    project_files = [
        "packaging_tutorial",
        [
            "LICENSE",
            "pyproject.toml",
            "README.md",
            "setup.cfg",
            "src",
            [
                "example_package_1",
                [
                    "__init__.py",
                    "example_1.py"
                ],
                "example_package_2",
                [
                    "__init__.py",
                    "example_2.py"
                ],
            ],
            "tests/"
        ]
    ]

    print()
    print("# 介绍一下 Python 打包")
    print("* Python 打包教程:https://packaging.python.org/tutorials/packaging-projects/")
    print("* 一个典型 Python 包的目录结构如下:")
    print()
    tree_project(project_files, 0, [''])
    print()

if __name__ == '__main__':
    test()
```

## 答案

```python
if depth == 0:
    next_space = ['']
else:
    next_space = [*space]
    if not last:
        next_space.append('|   ')
    else:
        next_space.append('    ')
```

## 选项

### A

```python
next_space = [*space]
if not last:
    next_space.append('|   ')
else:
    next_space.append('    ')
```

### B

```python
if depth == 0:
    next_space = ['    ']
else:
    next_space = [*space]
    if not last:
        next_space.append('|   ')
    else:
        next_space.append('    ')
```

### C

```python
if depth == 0:
    next_space = ['']
else:
    next_space = [*space]
    if last:
        next_space.append('    ')
    else:
        next_space.append('|   ')
```