Skip to content

Commit

Permalink
create set attr value
Browse files Browse the repository at this point in the history
  • Loading branch information
valdergallo committed Oct 31, 2016
1 parent bd90587 commit c8e3794
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pyconst/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pycont.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit c8e3794

Please sign in to comment.