<jupyter_start><jupyter_text>Enums and DataclassesOften we would like to be able to refer to a fixed set of values as constants.Consider, for example, a program that works with the points of the compass. We could refer to them as string values, but this would leave us exposed to spelling errors, differences of convention (should `North West` be hyphenated? Could it be referred to as `NW`?).Instead let us define the points of the compass and see how we might use them when writing some classes.<jupyter_code>from enum import Enum
from dataclasses import dataclass, field
from typing import List


class Direction(Enum):
    NORTH = 1
    NORTH_WEST = 2
    NORTH_EAST = 3
    SOUTH = 4
    SOUTH_WEST = 5
    SOUTH_EAST = 6
    EAST = 7
    WEST = 8
    

@dataclass
class NavigationInstruction:
    direction: Direction
    distance: int
        
    def print_instruction(self):
        print(f'Move {self.distance}m {self.direction.name}')


@dataclass
class Path:
    instructions: List[NavigationInstruction] = field(default_factory=list)
        
    def print_all(self):
        for instruction in self.instructions:
            instruction.print_instruction()
path = Path(instructions=[NavigationInstruction(Direction.NORTH, 50),
                          NavigationInstruction(Direction.EAST, 100),
                          NavigationInstruction(Direction.NORTH_WEST, 75)])
path.print_all()<jupyter_output>Move 50m NORTH
Move 100m EAST
Move 75m NORTH_WEST
{'fraction_non_alphanumeric': 0.06403269754768393, 'fraction_numerical': 0.014986376021798364, 'mean_word_length': 3.3720238095238093, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '504671', 'license': 'CC0-1.0', 'n_tokens_mistral': 459, 'n_tokens_neox': 420, 'n_words': 153, 'path': 'notebooks/05_Enums_and_Dataclasses.ipynb', 'repo_name': 'patrickdinneen/python101', 'size': '1468'}