thread: Create a generic Data class
This class is desigend to make it easier to pass data to and from a running Thread. This was inspired by the types.SimpleNamespace object so we can set generic values and use them as class members. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
c4e827bc5a
commit
0d100ec752
|
@ -0,0 +1,23 @@
|
|||
# Copyright 2024 (c) Anna Schumaker.
|
||||
"""A Thread class designed to easily sync up with the main thread."""
|
||||
|
||||
|
||||
class Data:
|
||||
"""A class for holding generic fields inspired by SimpleNamespace."""
|
||||
|
||||
def __init__(self, values_dict: dict = {}, **kwargs):
|
||||
"""Initialize our Data class."""
|
||||
self.__dict__.update(values_dict | kwargs)
|
||||
|
||||
def __eq__(self, rhs: any) -> bool:
|
||||
"""Compare two Data classes."""
|
||||
if isinstance(rhs, Data):
|
||||
return self.__dict__ == rhs.__dict__
|
||||
elif isinstance(rhs, dict):
|
||||
return self.__dict__ == rhs
|
||||
return False
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Get a string representation of the Data."""
|
||||
items = (f"{k}={v!r}" for k, v in self.__dict__.items())
|
||||
return f"{type(self).__name__}({', '.join(items)})"
|
|
@ -0,0 +1,40 @@
|
|||
# Copyright 2024 (c) Anna Schumaker.
|
||||
"""Tests our common Thread class."""
|
||||
import emmental.thread
|
||||
import unittest
|
||||
|
||||
|
||||
class TestData(unittest.TestCase):
|
||||
"""Tests our thread Data class."""
|
||||
|
||||
def test_init_kwargs(self):
|
||||
"""Tests initializing the data class with keyword args."""
|
||||
data = emmental.thread.Data(a=1, b=2)
|
||||
self.assertEqual(data.a, 1)
|
||||
self.assertEqual(data.b, 2)
|
||||
self.assertEqual(repr(data), "Data(a=1, b=2)")
|
||||
|
||||
def test_init_values_dict(self):
|
||||
"""Test initializing the data class with a dictionary of values."""
|
||||
data = emmental.thread.Data({"a": 1, "b": 2})
|
||||
self.assertEqual(data.a, 1)
|
||||
self.assertEqual(data.b, 2)
|
||||
self.assertEqual(repr(data), "Data(a=1, b=2)")
|
||||
|
||||
def test_init_both(self):
|
||||
"""Test initializing the data class with both."""
|
||||
data = emmental.thread.Data({"a": 1, "b": 2}, b=3, c='4')
|
||||
self.assertEqual(data.a, 1)
|
||||
self.assertEqual(data.b, 3)
|
||||
self.assertEqual(data.c, '4')
|
||||
self.assertEqual(repr(data), "Data(a=1, b=3, c='4')")
|
||||
|
||||
def test_compare(self):
|
||||
"""Test comparing two data classes."""
|
||||
data1 = emmental.thread.Data({"a": 1, "b": 2})
|
||||
data2 = emmental.thread.Data({"c": 3, "d": 4})
|
||||
self.assertTrue(data1 == data1)
|
||||
self.assertTrue(data1 == {"a": 1, "b": 2})
|
||||
self.assertFalse(data1 == data2)
|
||||
self.assertFalse(data1 == {"c": 2, "d": 4})
|
||||
self.assertFalse(data1 == 3)
|
Loading…
Reference in New Issue