Skip to main content
edited tags; deleted 122 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

After my last successful submission for review, I present another snippet of Python for review. Included are simpleHere is a lazy dictionary with test cases which are also applicable for review.

After my last successful submission for review, I present another snippet of Python for review. Included are simple test cases which are also applicable for review.

Here is a lazy dictionary with test cases.

deleted 163 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

As usual a huge thanks to people who take the time to look at this code, I learnt a huge amount from the previous posting and truly appreciate all input given.

As usual a huge thanks to people who take the time to look at this code, I learnt a huge amount from the previous posting and truly appreciate all input given.

Source Link
Justin Fay
  • 307
  • 2
  • 10

Python lazy dictionary

After my last successful submission for review, I present another snippet of Python for review. Included are simple test cases which are also applicable for review.

As usual a huge thanks to people who take the time to look at this code, I learnt a huge amount from the previous posting and truly appreciate all input given.

"""
A dictionary whose values are only evaluated on access.
"""
from functools import partial

from UserDict import IterableUserDict


class LazyDict(IterableUserDict, object):
    """
    A lazy dictionary implementation which will try
    to evaluate all values on access and cache the
    result for later access.
    """

    def set_lazy(self, key, item, *args, **kwargs):
        """
        Allow the setting of a callable and arguments
        as value of dictionary.
        """
        if callable(item):
            item = partial(item, *args, **kwargs)
        super(LazyDict, self).__setitem__(key, item)

    def __getitem__(self, key):
        item = super(LazyDict, self).__getitem__(key)
        try:
            self[key] = item = item()
        except TypeError:
            pass
        return item


def test_lazy_dict():
    """
    Simple test cases for `LazyDict`.
    """

    lazy_dict = LazyDict({1: 1, 2: lambda: 2})
    assert lazy_dict[2] == 2
    lazy_dict[3] = 3
    assert lazy_dict[3] == 3

    def joiner(*args, **kwargs):
        sep = kwargs.pop('sep', ' ')
        kwargs = [
            '%s=%s' % (k, v)
            for k, v in sorted(kwargs.iteritems())]
        return sep.join(list(args) + kwargs)

    lazy_dict.set_lazy(
        4, joiner, 'foo', 'bar', name='test', other='muah', sep=' ')
    assert lazy_dict[4] == 'foo bar name=test other=muah'

    assert lazy_dict.get('5') is None

    # Test caching functionality.
    def call_at_max(count):
        counter = [0]

        def inner():
            counter[0] += 1
            if counter[0] > count:
                raise AssertionError('Called more than once')
            return 'happy'
        return inner

    call_once = call_at_max(1)
    lazy_dict[5] = call_once
    assert lazy_dict[5] == 'happy'
    assert lazy_dict[5] == 'happy'

    # Test for helper function.
    try:
        call_once()
    except AssertionError:
        assert True