diff options
-rw-r--r-- | CHANGELOG | 7 | ||||
-rw-r--r-- | bs4/element.py | 6 | ||||
-rw-r--r-- | tests/test_tree.py | 18 |
3 files changed, 24 insertions, 7 deletions
@@ -35,6 +35,13 @@ work. Here are the renames: * findPreviousSibling -> find_previous_sibling * findPreviousSiblings -> find_previous_siblings +Methods have been renamed for compatibility with Python 3. + + * Tag.has_key() -> Tag.has_attr() + + (This was misleading, anyway, because has_key() looked at + a tag's attributes and __in__ looked at a tag's contents.) + Some attributes have also been renamed: * Tag.isSelfClosing -> Tag.is_empty_element diff --git a/bs4/element.py b/bs4/element.py index 10377e9..a9814e1 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -463,7 +463,7 @@ class Tag(PageElement): attribute.""" return self.attrs.get(key, default) - def has_key(self, key): + def has_attr(self, key): return key in self.attrs def __getitem__(self, key): @@ -707,6 +707,10 @@ class Tag(PageElement): def recursiveChildGenerator(self): return self.recursive_children + # This was kind of misleading because has_key() (attributes) was + # different from __in__ (contents). has_key() is gone in Python 3, + # anyway. + has_key = has_attr # Next, a couple classes to represent queries and their results. class SoupStrainer(object): diff --git a/tests/test_tree.py b/tests/test_tree.py index 87a7e3a..cf14d0c 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -10,7 +10,7 @@ methods tested here. """ import copy -import cPickle as pickle +import pickle import re from bs4 import BeautifulSoup from bs4.builder import builder_registry @@ -288,7 +288,7 @@ class TestParentOperations(TreeTest): def test_parent_generator(self): parents = [parent['id'] for parent in self.start.parents - if parent is not None and parent.has_key('id')] + if parent is not None and 'id' in parent.attrs] self.assertEquals(parents, ['bottom', 'middle', 'top']) @@ -735,11 +735,17 @@ class TestElementObjects(SoupTest): self.assertEqual(soup.a, None) self.assertEqual(soup.aTag, None) - def test_has_key(self): - """has_key() checks for the presence of an attribute.""" + def test_has_attr(self): + """has_attr() checks for the presence of an attribute. + + Please note note: has_attr() is different from + __in__. has_attr() checks the tag's attributes and __in__ + checks the tag's chidlren. + """ soup = self.soup("<foo attr='bar'>") - self.assertTrue(soup.foo.has_key('attr')) - self.assertFalse(soup.foo.has_key('attr2')) + self.assertTrue(soup.foo.has_attr('attr')) + self.assertFalse(soup.foo.has_attr('attr2')) + def test_attributes_come_out_in_alphabetical_order(self): markup = '<b a="1" z="5" m="3" f="2" y="4"></b>' |