Last active
January 31, 2024 18:25
-
-
Save SeanSyue/ff14082675d625b77d7cd5e1a8f66798 to your computer and use it in GitHub Desktop.
An extension of the SimpleNamespace type, where the members of whose IndexableNamespace instance can also be accessed and modified by indexing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase | |
from IndexableNamespace import IndexableNamespace | |
class TestLegelInstantiation(TestCase): | |
def run_assertion(self, target): | |
self.assertEqual(target.a, 1) | |
self.assertEqual(target.b, 2) | |
def test_unpack_dict(self): | |
ns = IndexableNamespace(**{"a": 1, "b": 2}) | |
with self.subTest(): | |
self.run_assertion(ns) | |
def test_new_member_instance_accessing(self): | |
ns = IndexableNamespace() | |
ns.a = 1 | |
ns.b = 2 | |
with self.subTest(): | |
self.run_assertion(ns) | |
class TestAccessingModification(TestCase): | |
def setUp(self) -> None: | |
self.ns = IndexableNamespace(a=1, b=2, c=3, d=3, e=5) | |
def test_indexing(self): | |
with self.subTest(): | |
self.assertEqual(self.ns[0], self.ns.a) | |
self.assertEqual(self.ns[1], self.ns.b) | |
def test_member_modification_instance_accessing(self): | |
self.ns.a = 3 | |
self.assertEqual(self.ns.a, 3) | |
def test_member_modification_indexing(self): | |
self.ns[0] = 3 | |
self.assertEqual(self.ns.a, 3) | |
def test_len(self): | |
self.assertEqual(len(self.ns), 5) | |
def test_deletion(self): | |
del self.ns.a | |
with self.assertRaises(AttributeError): | |
self.ns.a | |
def test_key_accessing(self): | |
self.assertEqual(IndexableNamespace(**self.ns.as_dict()), self.ns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from types import SimpleNamespace | |
from typing import Any | |
from collections.abc import MutableSequence | |
class IndexableNamespace(SimpleNamespace, MutableSequence): | |
""" | |
An extension of the SimpleNamespace type. | |
The members of an IndexableNamespace instance can also be | |
accessed and modified by indexing. | |
Example: | |
``` | |
>>> ns = IndexableNamespace(a=1, b=2) | |
>>> ns[0] | |
1 | |
>>> ns[2] = 3 | |
>>> ns.b | |
3 | |
``` | |
""" | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
def __getitem__(self, idx): | |
""" | |
Access a member by indexing. | |
Example: | |
``` | |
>>> ns = IndexableNamespace(a=1, b=2) | |
>>> ns[0] | |
1 | |
``` | |
""" | |
return tuple(self.__dict__.values())[idx] | |
def __setitem__(self, idx, value): | |
""" | |
Modify a member by indexing. | |
Example: | |
``` | |
>>> ns = IndexableNamespace(a=1, b=2) | |
>>> ns[2] = 3 | |
>>> ns.b | |
3 | |
``` | |
""" | |
self.__dict__[tuple(self.__dict__)[idx]] = value | |
def __delitem__(self, idx): | |
del self.__dict__[tuple(self.__dict__)[idx]] | |
def __len__(self): | |
return len(tuple(self.__dict__)) | |
def insert(self, idx, value): | |
self.__dict__[tuple(self.__dict__)[idx]] = value | |
def append(self, value: Any): | |
""" | |
Replace the last None-type item with the new item. | |
If there is not None-type item, replace the last item | |
with the new item | |
Args: | |
value (Any): _description_ | |
""" | |
# List of all the indexes of items with None-type | |
idx_none = [i for i, v in enumerate(self.__dict__.values()) if v is None] | |
# Also add the index of the last item in the list, so that in case | |
# no None-type item exist, the last item will be specified | |
idx_none.append(-1) | |
self[max(idx_none)] = value | |
def as_dict(self): | |
return self.__dict__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment