There are four orchards, namely A, B, C & D with fruits in them as Apple, Litchi, Mango, Apple respectively. The qualities of the fruits in the orchards are good, good, good & bad respectively.
# install rich (https://github.com/textualize/rich)
$ python3 -m pip install rich
# run the code
$ python3 main.py
from enum import Enum
from rich.tree import Tree
from rich import print
# Available fruit qualities.
class Quality(Enum):
GOOD: int = 0
BAD: int = 1
# An orchard model to bind name of orchard, fruit in that orchard & its quality together.
class Orchard:
name: str
fruit: str
quality: Quality
# Initialize object attributes from constructor.
def __init__(self, name, fruit, quality):
self.name = name
self.fruit = fruit
self.quality = quality
# Actual logic implementation class
class OrchardProvider:
# Available orchards to us.
orchards: list[Orchard] = [
Orchard("A", "Apple", Quality.GOOD),
Orchard("B", "Litchi", Quality.GOOD),
Orchard("C", "Mango", Quality.GOOD),
Orchard("D", "Apple", Quality.BAD),
]
# Constructor. Printing available orchards upon object creation.
def __init__(self):
print("[green]Available Orchards:")
self.print_available()
# Prints available orchards in a tree view.
def print_available(self):
tree = Tree("[yellow]Orchards")
for orchard in self.orchards:
node = tree.add("[yellow]" + orchard.name)
node.add("[yellow]" + orchard.fruit)
node.add("[yellow]" + orchard.quality.name)
print(tree)
# Searches for a given fruit name in available orchards & prints the tree view.
def search(self, fruit_name: str):
flag = False
tree = Tree("[yellow]Results")
# Search for fruit name in available orchards.
for orchard in self.orchards:
if fruit_name == orchard.fruit:
flag = True
node = tree.add("[yellow]" + orchard.name)
node.add("[yellow]" + orchard.fruit)
node.add("[yellow]" + orchard.quality.name)
if flag:
print(tree)
else:
print("[red]No results found.")
# Entry point of the application.
if __name__ == "__main__":
# Create new object instance.
orchard_provider = OrchardProvider()
while True:
# Search with the help of object created above.
fruit_name = input("Enter fruit to search:")
orchard_provider.search(fruit_name)
right-click & select “Open image in new tab” for larger image.