I hacked a way to print a christmas tree, to use it in the usual seasons greetings email, you know the drill.
Next Step is to integrate a multi lines of text in the border left side of the tree. For this i like to use an offset to the TREE['center']
.
""" Seasons Greeting mail - ascii Christmas Tree """
import pprint
import shutil
import sys
# DEFAULT
TREE = {
'minimalHeight': 10,
'minimalWidth': 12,
'height': 10,
'width': 12,
'center': 6,
'calc_width': lambda x: x + 2,
'calc_center': lambda x: int(x / 2),
'calc_amount': lambda x: int(x / 2),
}
(TREE['maximalWidth'], TREE['maximalHeight']) = shutil.get_terminal_size()
# FUNKTIONS
def line_generic_branch(imageline, amount, left):
"""
generates a branch line
"""
line_default(imageline)
if left:
start_pos = TREE['center'] - TREE['calc_amount'](imageline)
for num in range(amount):
tree[imageline][start_pos + num] = '_'
tree[imageline][start_pos] = '('
else:
end_pos = TREE['center'] + TREE['calc_amount'](imageline)
for num in range(amount):
tree[imageline][end_pos - num] = '_'
tree[imageline][end_pos] = ')'
def line_default(imageline):
tree[imageline][0] = '|'
tree[imageline][-1] = '|'
def line_0():
for pos in range(1, TREE['width'] - 1):
tree[0][pos] = '_'
def line_1():
line_default(1)
def line_2():
line_default(2)
tree[2][TREE['center']] = ')'
def line_branch(imageline, left):
line_generic_branch(imageline, imageline, left)
def line_last_branch(imageline, left):
line_generic_branch(imageline, TREE['calc_amount'](imageline), left)
def line_first_trunk(imageline):
line_default(imageline)
if imageline % 2:
tree[imageline][TREE['center']] = '('
elif not imageline % 2:
tree[imageline][TREE['center']] = ')'
def line_bottom_trunk(imageline):
line_default(imageline)
if imageline % 2:
tree[imageline][TREE['center'] - 1] = '('
tree[imageline][TREE['center']] = '.'
elif not imageline % 2:
tree[imageline][TREE['center'] + 1] = ')'
tree[imageline][TREE['center']] = '.'
def line_last(imageline):
line_default(imageline)
for pos in range(1, TREE['width'] - 1):
tree[imageline][pos] = '_'
# INIT
tree = []
imageline = 0
if len(sys.argv) > 1:
try:
TREE['height'] = int(sys.argv[1])
except TypeError:
raise TypeError("Only Integers are allowed not {}".format(sys.argv[1]))
if TREE['height'] < TREE['minimalHeight']:
raise ValueError("given Height is to less. MIN {}".format(
TREE['minimalHeight']))
if TREE['height'] > TREE['maximalHeight']:
raise ValueError("given Height is to much. MAX {}".format(
TREE['maximalHeight']))
TREE['width'] = TREE['calc_width'](TREE['height'])
TREE['center'] = TREE['calc_center'](TREE['width'])
# MAIN
tree = [[' ' for x in range(TREE['width'])] for y in range(TREE['height'])]
"""lets Draw the lines"""
while imageline <= TREE['height'] - 4:
try:
globals()['line_' + str(imageline)]()
except KeyError:
left = False
if imageline % 2:
left = True
if imageline < TREE['height'] - 4:
line_branch(imageline, left)
elif imageline == TREE['height'] - 4:
line_last_branch(imageline, left)
imageline += 1
## add footer
line_first_trunk(TREE['height'] - 3)
line_bottom_trunk(TREE['height'] - 2)
line_last(TREE['height'] - 1)
for nr, line in enumerate(tree):
# print("{0:2d}".format(nr), ''.join(line))
print(''.join(line))
https://github.com/mbenecke/christmas_tree
It workes fine, so far. Here an Example:
Di Nov 05 11:08:36 (master)> python christmastree.py 15
_______________
| |
| ) |
| (__ |
| ___) |
| (____ |
| _____) |
| (______ |
| _______) |
| (________ |
| _________) |
| (____ |
| ) |
| (. |
|_______________|
But i am not a skilled coder, so i need someone who give me feedback to my Style. What I want reviewed:
- Am I reinventing the wheel? I searched thoroughly but couldn't find anything similar.
- Code style.
- Is it pythonic?
- Is it readable?
- Hopefully not over engineered, but slim enough to be proud of it.