Skip to content

Commit

Permalink
refactor: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Jan 23, 2025
1 parent d80faca commit 0c5dd5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 12 additions & 8 deletions openedx/core/lib/derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Derived:
TODO doc
TODO typing
"""
def __init__(self, calculate_value: t.Callable)
def __init__(self, calculate_value: t.Callable):
self.calculate_value = calculate_value


Expand All @@ -62,20 +62,24 @@ def _derive_dict_items(settings, the_dict: dict):
for key, child in the_dict.items():
if isinstance(child, Derived):
the_dict[key] = child.calculate_value(settings)
elif isinstance(child, Sequence):
elif isinstance(child, Sequence) and not isinstance(child, str):
the_dict[key] = _derive_sequence_items(settings, child)
_derive_sequence_items(settings, child)
elif isinstance(child, dict):
_derive_dict_items(settings, child)


def _derive_sequence_items(settings, sequence: Sequence):
def _derive_sequence_items(settings, the_seq: Sequence):
"""
TODO doc
"""
for ix, child in enumerate(sequence):
result = []
for ix, child in enumerate(the_seq):
if isinstance(child, Derived):
the_dict[ix] = child.calculate_value(settings)
elif isinstance(value, Sequence):
_derive_sequence_items(settings, child)
elif isinstance(value, dict):
result.append(child.calculate_value(settings))
elif isinstance(child, Sequence) and not isinstance(child, str):
result.append(_derive_sequence_items(settings, child))
elif isinstance(child, dict):
_derive_dict_items(settings, child)
result.append(child)
return type(the_seq)(result)
3 changes: 1 addition & 2 deletions openedx/core/lib/tests/test_derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sys
from unittest import TestCase
from openedx.core.lib.derived import Derived
from openedx.core.lib.derived import Derived, derive_settings


class TestDerivedSettings(TestCase):
Expand All @@ -14,7 +14,6 @@ class TestDerivedSettings(TestCase):
"""
def setUp(self):
super().setUp()
clear_for_tests()
self.module = sys.modules[__name__]
self.module.SIMPLE_VALUE = 'paneer'
self.module.DERIVED_VALUE = Derived(lambda settings: 'mutter ' + settings.SIMPLE_VALUE)
Expand Down

0 comments on commit 0c5dd5e

Please sign in to comment.