From a58a237bc16e0a0ff78d22f7833fbf154f8bb44b Mon Sep 17 00:00:00 2001 From: Pelle Nilsson Date: Tue, 4 Jun 2013 22:44:50 +0200 Subject: [PATCH] Better API for tags in sections. --- formatgamebook.py | 2 +- sections.py | 7 +++++-- test_sections.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/formatgamebook.py b/formatgamebook.py index cb689a9..681416e 100755 --- a/formatgamebook.py +++ b/formatgamebook.py @@ -107,7 +107,7 @@ def parse_file_to_book(inputfile, book): def add_section_to_book(book, name, text, number=None, tags=None): section = sections.Section(name, text) if tags: - section.set_tags(tags) + section.add_tags(tags) book.add(section) if number: book.force_section_nr(name, number) diff --git a/sections.py b/sections.py index ebc0c09..f6844ce 100644 --- a/sections.py +++ b/sections.py @@ -7,8 +7,11 @@ class Section: self.text = text self.tags = set() - def set_tags(self, tags): - self.tags = set(tags) + def add_tags(self, tags): + self.tags.update(set(tags)) + + def hastag(self, tag): + return tag in self.tags def __repr__(self): return "Section(%s, %s, %s)" % (repr(self.name), repr(self.text), diff --git a/test_sections.py b/test_sections.py index 202f0e6..0fe231c 100755 --- a/test_sections.py +++ b/test_sections.py @@ -14,6 +14,17 @@ class TestSection(TestCase): self.assertEqual(sec.name, "nnn") self.assertEqual(sec.text, "text") + def test_add_tags(self): + sec = sections.Section("nnn", "text") + sec.add_tags(['a', 'b']) + self.assertTrue(sec.hastag('a')) + self.assertTrue(sec.hastag('b')) + sec.add_tags(['c', 'd']) + self.assertTrue(sec.hastag('a')) + self.assertTrue(sec.hastag('b')) + self.assertTrue(sec.hastag('c')) + self.assertTrue(sec.hastag('d')) + class TestBook(TestCase): def setUp(self): pass