So, Let's solve the quiz.
As we start the project let's think about the usage of the functions. We want all related functions to reside in the tree package. The user of the package should be able to import the package and start using the dump and load functions
>>> import tree
>>> root = tree.load([0])
...
>>> root.right = Node(2,None,None)
...
>>> tree.dump(tree)
[0,None,2]
To do so follow the following steps :
First, let's create the "tree" under the project root directory
Now create the "tree/node.py" and define the Node class.
class Node:
def __init__(self,data : int ,left : 'Node' , right : 'Node'):
self.data = data
self.left = left
self.right = right
Now, create the "tree/actions.py" and create the load and dump stubs.
from typing import List,Union
from .node import Node
def load( data : List[Union[int,None]] ) -> Node :
return None
def dump( tree : Node ) -> List[Union[int,None]] :
return None
Now, let's convert the "tree" directory to a package, by creating "tree/__init__.py". This file will expose the the Node class and the actions to the outside world.
from .node import Node
from .actions import load
from .actions import dump
Let's see the directory structure so far:
project
├── tree
├── __init__.py
├── node.py
├── actions.py