# Path: lantern/structures/dynamicdict.py
# class DynamicDict:
#     """Dictionary which builds values when they are accessed for the first time.
# 
#     Example:
#         >>> ngrams = DynamicDict({
#         ...     'trigrams': lambda: load_ngrams('trigrams'),
#         ...     'quadgrams': lambda: load_ngrams('quadgrams')
#         ... })
# 
#     Since trigrams and quadgrams are large files, its expensive to load them in
#     if theyre not needed. Using the DynamicDict ensures they are only loaded when
#     they are accessed for the first time.
#     """
# 
#     def __init__(self, builders={}):
#         """Instantiate dict with mapping of keys to builders.
# 
#         Args:
#             builders (dict): key to function mapping
#         """
#         self.builders = builders
# 
#     def __getattr__(self, name):
#         """Attempt to build values that are not already created."""
#         try:
#             attribute = self.builders[name]()
#         except KeyError:
#             raise AttributeError("'DynamicDict' object has no attribute '{}'".format(name))
#         else:
#             setattr(self, name, attribute)
#             return attribute


import pytest
from lantern.structures import DynamicDict
"""Tests for DynamicDict"""




def test_dynamicdict():
    """Testing entries are built and returned correctly"""
    container = DynamicDict({'example': lambda: 9})
    assert container.example == 9


def test_dynamicdict_operations():
    """Testing various operations on the DynamicDict"""
    container = DynamicDict()

    container.invalid = "hello"
    assert container.invalid == "hello"


def test_dynamicdict_invalid_access():
    """Testing invalid accesses raise exceptions"""

# Path: lantern/structures/dynamicdict.py
# class DynamicDict:
#     """Dictionary which builds values when they are accessed for the first time.
# 
#     Example:
#         >>> ngrams = DynamicDict({
#         ...     'trigrams': lambda: load_ngrams('trigrams'),
#         ...     'quadgrams': lambda: load_ngrams('quadgrams')
#         ... })
# 
#     Since trigrams and quadgrams are large files, its expensive to load them in
#     if theyre not needed. Using the DynamicDict ensures they are only loaded when
#     they are accessed for the first time.
#     """
# 
#     def __init__(self, builders={}):
#         """Instantiate dict with mapping of keys to builders.
# 
#         Args:
#             builders (dict): key to function mapping
#         """
#         self.builders = builders
# 
#     def __getattr__(self, name):
#         """Attempt to build values that are not already created."""
#         try:
#             attribute = self.builders[name]()
#         except KeyError:
#             raise AttributeError("'DynamicDict' object has no attribute '{}'".format(name))
#         else:
#             setattr(self, name, attribute)
#             return attribute


# Path: tests/structures/test_dynamicdict.py
import pytest
from lantern.structures import DynamicDict

"""Tests for DynamicDict"""




def test_dynamicdict():
    """Testing entries are built and returned correctly"""
    container = DynamicDict({'example': lambda: 9})
    assert container.example == 9


def test_dynamicdict_operations():
    """Testing various operations on the DynamicDict"""
    container = DynamicDict()

    container.invalid = "hello"
    assert container.invalid == "hello"


def test_dynamicdict_invalid_access():
    """Testing invalid accesses raise exceptions"""

    container = DynamicDict({'example': lambda: 9})