Skip to content

Commit

Permalink
Merge pull request #40 from sergei-maertens/develop
Browse files Browse the repository at this point in the history
Fixed #33: access dict methods of Labels (IPython support)
  • Loading branch information
sergei-maertens committed Aug 28, 2016
2 parents 2447903 + 125d523 commit 6567cb8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion djchoices/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# Support Functionality (Not part of public API)

class Labels(dict):
def __getattribute__(self, name):

def __getattr__(self, name):
result = dict.get(self, name, None)
if result is not None:
return result
Expand Down
26 changes: 26 additions & 0 deletions djchoices/tests/test_private_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
try:
import unittest2 as unittest
except ImportError:
import unittest

from djchoices import DjangoChoices, C, ChoiceItem


class PrivateAPITests(unittest.TestCase):

def test_labels_dict(self):
"""
Ref #33: IPython reads the Labels.keys() for pretty printing.
"""
class Choices(DjangoChoices):
a = ChoiceItem('a', 'A')
b = ChoiceItem('b', 'B')

self.assertEqual(Choices.labels.a, 'A')
self.assertEqual(Choices.labels.b, 'B')
with self.assertRaises(AttributeError):
Choices.labels.c

keys = Choices.labels.keys()
self.assertEqual(set(keys), set(['a', 'b']))

0 comments on commit 6567cb8

Please sign in to comment.