From c8e379442ecc664abeb9fffe7a47803fa7cc2b3e Mon Sep 17 00:00:00 2001 From: Valder Gallo Date: Mon, 31 Oct 2016 15:24:51 -0200 Subject: [PATCH] create set attr value --- pyconst/const.py | 12 ++++++++---- tests/test_pycont.py | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyconst/const.py b/pyconst/const.py index 938ef27..714901b 100644 --- a/pyconst/const.py +++ b/pyconst/const.py @@ -6,8 +6,8 @@ class PyConstString(str): - def __new__(cls, attr, label): - obj = str.__new__(cls, s(attr)) + def __new__(cls, label, value): + obj = str.__new__(cls, s(value)) obj.label = label return obj @@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs): for label, attr in kwargs.items(): self.add(label, attr) - def add(self, label, attr=None): + def add(self, label, attr=None, value=None): "Set values in constant" if isinstance(label, tuple) or isinstance(label, list): @@ -35,7 +35,11 @@ def add(self, label, attr=None): if not attr: attr = label - self.__data += (PyConstString(attr, label),) + if not value: + value = attr + + self.__data += (PyConstString(label=label, value=value),) + # set attribute as slugfiy self.__dict__[s_attr(attr)] = self.__data[-1] def __getitem__(self, index): diff --git a/tests/test_pycont.py b/tests/test_pycont.py index d6e246d..b3964e7 100644 --- a/tests/test_pycont.py +++ b/tests/test_pycont.py @@ -85,6 +85,12 @@ def test_number_attr(self): self.assertEqual(const[0], ('1', u'First item')) self.assertEqual(const._1, '1') + def test_set_different_attribute_and_value(self): + const = Const() + const.add(label='First item', attr="my_item", value=1) + self.assertEqual(const[0], ('1', u'First item')) + self.assertEqual(const.my_item, '1') + if __name__ == '__main__': unittest.main()