forked from daleathan/ProgrammingInPython3-MarkSummerfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.py
More file actions
50 lines (39 loc) · 1.59 KB
/
Block.py
File metadata and controls
50 lines (39 loc) · 1.59 KB
1
2
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
49
50
#!/usr/bin/env python3
# Copyright (c) 2011 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
__all__ = ["Block", "get_root_block", "get_empty_block",
"get_new_row", "is_new_row"]
class Block:
def __init__(self, name, color="white"):
self.name = name
self.color = color
self.children = []
def has_children(self):
return bool(self.children)
def __str__(self):
blocks = []
if self.name is not None:
color = "{0}: ".format(self.color) if self.color else ""
block = "[{0}{1}".format(color, self.name)
blocks.append(block)
if self.children:
blocks.append("\n")
for block in self.children:
if is_new_row(block):
blocks.append("/\n")
else:
blocks.append(str(block))
if self.name is not None:
blocks[-1] += "]\n"
return "".join(blocks)
get_root_block = lambda: Block(None, None)
get_empty_block = lambda: Block("")
get_new_row = lambda: None
is_new_row = lambda x: x is None