国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python實(shí)現(xiàn)目錄樹(shù)生成示例

2020-02-23 05:17:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import optparse

LOCATION_NONE     = 'NONE'
LOCATION_MID      = 'MID'
LOCATION_MID_GAP  = 'MID_GAP'
LOCATION_TAIL     = 'TAIL'
LOCATION_TAIL_GAP = 'TAIL_GAP'

Notations = {
    LOCATION_NONE: '',
    LOCATION_MID: '├─',
    LOCATION_MID_GAP: '│  ',
    LOCATION_TAIL: '└─',
    LOCATION_TAIL_GAP: '    '
}

class Node(object):
    def __init__(self, name, depth, parent=None, location=LOCATION_NONE):
        self.name = name
        self.depth = depth
        self.parent = parent
        self.location = location
        self.children = []

    def __str__(self):
        sections = [self.name]
        parent = self.has_parent()
        if parent:
            if self.is_tail():
                sections.insert(0, Notations[LOCATION_TAIL])
            else:
                sections.insert(0, Notations[LOCATION_MID])
            self.__insert_gaps(self, sections)
        return ''.join(sections)

    def __insert_gaps(self, node, sections):
        parent = node.has_parent()
        # parent exists and parent's parent is not the root node
        if parent and parent.has_parent():
            if parent.is_tail():
                sections.insert(0, Notations[LOCATION_TAIL_GAP])
            else:
                sections.insert(0, Notations[LOCATION_MID_GAP])
            self.__insert_gaps(parent, sections)

    def has_parent(self):
        return self.parent

    def has_children(self):
        return self.children

    def add_child(self, node):

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大港区| 麦盖提县| 牡丹江市| 嘉义县| 祁阳县| 南汇区| 柘城县| 轮台县| 泽普县| 灵寿县| 宝坻区| 黄冈市| 怀来县| 赤水市| 徐州市| 溧水县| 正安县| 大同市| 金昌市| 长沙市| 垦利县| 湾仔区| 基隆市| 浦县| 布尔津县| 崇义县| 金堂县| 南开区| 塔城市| 南木林县| 襄樊市| 谢通门县| 万年县| 东至县| 云和县| 和静县| 延安市| 大埔县| 开平市| 凉城县| 望城县|