summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG3
-rw-r--r--beautifulsoup/element.py42
-rw-r--r--tests/test_tree.py84
3 files changed, 67 insertions, 62 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 0bfe72a..50502cf 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,8 @@
= 4.0 =
+Methods have been renamed for comply with PEP 8. findAll is now
+find_all, and so on. Old names still work.
+
tag.string now operates recursively. If tag A contains a single tag B
and nothing else, then A.string is the same as B.string. So:
diff --git a/beautifulsoup/element.py b/beautifulsoup/element.py
index b4981a7..869be4d 100644
--- a/beautifulsoup/element.py
+++ b/beautifulsoup/element.py
@@ -164,13 +164,13 @@ class PageElement:
def findNext(self, name=None, attrs={}, text=None, **kwargs):
"""Returns the first item that matches the given criteria and
appears after this Tag in the document."""
- return self._findOne(self.findAllNext, name, attrs, text, **kwargs)
+ return self._findOne(self.find_allNext, name, attrs, text, **kwargs)
- def findAllNext(self, name=None, attrs={}, text=None, limit=None,
+ def find_allNext(self, name=None, attrs={}, text=None, limit=None,
**kwargs):
"""Returns all items that match the given criteria and appear
after this Tag in the document."""
- return self._findAll(name, attrs, text, limit, self.nextGenerator,
+ return self._find_all(name, attrs, text, limit, self.nextGenerator,
**kwargs)
def findNextSibling(self, name=None, attrs={}, text=None, **kwargs):
@@ -183,22 +183,22 @@ class PageElement:
**kwargs):
"""Returns the siblings of this Tag that match the given
criteria and appear after this Tag in the document."""
- return self._findAll(name, attrs, text, limit,
+ return self._find_all(name, attrs, text, limit,
self.nextSiblingGenerator, **kwargs)
fetchNextSiblings = findNextSiblings # Compatibility with pre-3.x
def findPrevious(self, name=None, attrs={}, text=None, **kwargs):
"""Returns the first item that matches the given criteria and
appears before this Tag in the document."""
- return self._findOne(self.findAllPrevious, name, attrs, text, **kwargs)
+ return self._findOne(self.find_allPrevious, name, attrs, text, **kwargs)
- def findAllPrevious(self, name=None, attrs={}, text=None, limit=None,
+ def find_allPrevious(self, name=None, attrs={}, text=None, limit=None,
**kwargs):
"""Returns all items that match the given criteria and appear
before this Tag in the document."""
- return self._findAll(name, attrs, text, limit, self.previousGenerator,
+ return self._find_all(name, attrs, text, limit, self.previousGenerator,
**kwargs)
- fetchPrevious = findAllPrevious # Compatibility with pre-3.x
+ fetchPrevious = find_allPrevious # Compatibility with pre-3.x
def findPreviousSibling(self, name=None, attrs={}, text=None, **kwargs):
"""Returns the closest sibling to this Tag that matches the
@@ -210,7 +210,7 @@ class PageElement:
limit=None, **kwargs):
"""Returns the siblings of this Tag that match the given
criteria and appear before this Tag in the document."""
- return self._findAll(name, attrs, text, limit,
+ return self._find_all(name, attrs, text, limit,
self.previousSiblingGenerator, **kwargs)
fetchPreviousSiblings = findPreviousSiblings # Compatibility with pre-3.x
@@ -229,7 +229,7 @@ class PageElement:
"""Returns the parents of this Tag that match the given
criteria."""
- return self._findAll(name, attrs, None, limit, self.parentGenerator,
+ return self._find_all(name, attrs, None, limit, self.parentGenerator,
**kwargs)
fetchParents = findParents # Compatibility with pre-3.x
@@ -242,7 +242,7 @@ class PageElement:
r = l[0]
return r
- def _findAll(self, name, attrs, text, limit, generator, **kwargs):
+ def _find_all(self, name, attrs, text, limit, generator, **kwargs):
"Iterates over a generator looking for things that match."
if isinstance(name, SoupStrainer):
@@ -394,7 +394,7 @@ class Tag(PageElement, Entities):
"Basic constructor."
# We don't actually store the parser object: that lets extracted
- # chunks be garbage-collected
+ # chunks be garbage-collected.
self.parserClass = parser.__class__
self.name = name
self.isSelfClosing = builder.isSelfClosingTag(name)
@@ -407,7 +407,6 @@ class Tag(PageElement, Entities):
self.setup(parent, previous)
self.hidden = False
self.containsSubstitutions = False
- self.escapeUnrecognizedEntities = parser.escapeUnrecognizedEntities
if isinstance(attrs, types.DictType):
self.attrs = [kv for kv in attrs.items()]
@@ -487,9 +486,9 @@ class Tag(PageElement, Entities):
def __call__(self, *args, **kwargs):
"""Calling a tag like a function is the same as calling its
- findAll() method. Eg. tag('a') returns a list of all the A tags
+ find_all() method. Eg. tag('a') returns a list of all the A tags
found within this tag."""
- return apply(self.findAll, args, kwargs)
+ return apply(self.find_all, args, kwargs)
def __getattr__(self, tag):
#print "Getattr %s.%s" % (self.__class__, tag)
@@ -670,14 +669,14 @@ class Tag(PageElement, Entities):
"""Return only the first child of this Tag matching the given
criteria."""
r = None
- l = self.findAll(name, attrs, recursive, text, 1, **kwargs)
+ l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
if l:
r = l[0]
return r
findChild = find
- def findAll(self, name=None, attrs={}, recursive=True, text=None,
- limit=None, **kwargs):
+ def find_all(self, name=None, attrs={}, recursive=True, text=None,
+ limit=None, **kwargs):
"""Extracts a list of Tag objects that match the given
criteria. You can specify the name of the Tag and any
attributes you want the Tag to have.
@@ -690,8 +689,11 @@ class Tag(PageElement, Entities):
generator = self.recursiveChildGenerator
if not recursive:
generator = self.childGenerator
- return self._findAll(name, attrs, text, limit, generator, **kwargs)
- findChildren = findAll
+ return self._find_all(name, attrs, text, limit, generator, **kwargs)
+
+ # Old names for backwards compatibility.
+ find_all = find_all
+ findChildren = find_all
#Private methods
diff --git a/tests/test_tree.py b/tests/test_tree.py
index 367489e..87bd154 100644
--- a/tests/test_tree.py
+++ b/tests/test_tree.py
@@ -40,7 +40,7 @@ class TreeTest(SoupTest):
class TestFind(TreeTest):
"""Basic tests of the find() method.
- find() just calls findAll() with limit=1, so it's not tested all
+ find() just calls find_all() with limit=1, so it's not tested all
that thouroughly here.
"""
@@ -54,34 +54,34 @@ class TestFind(TreeTest):
class TestFindAll(TreeTest):
- """Basic tests of the findAll() method."""
+ """Basic tests of the find_all() method."""
def test_find_all_text_nodes(self):
"""You can search the tree for text nodes."""
soup = self.soup("<html>Foo<b>bar</b>\xbb</html>")
# Exact match.
- self.assertEqual(soup.findAll(text="bar"), [u"bar"])
+ self.assertEqual(soup.find_all(text="bar"), [u"bar"])
# Match any of a number of strings.
self.assertEqual(
- soup.findAll(text=["Foo", "bar"]), [u"Foo", u"bar"])
+ soup.find_all(text=["Foo", "bar"]), [u"Foo", u"bar"])
# Match a regular expression.
- self.assertEqual(soup.findAll(text=re.compile('.*')),
+ self.assertEqual(soup.find_all(text=re.compile('.*')),
[u"Foo", u"bar", u'\xbb'])
# Match anything.
- self.assertEqual(soup.findAll(text=True),
+ self.assertEqual(soup.find_all(text=True),
[u"Foo", u"bar", u'\xbb'])
def test_find_all_limit(self):
- """You can limit the number of items returned by findAll."""
+ """You can limit the number of items returned by find_all."""
soup = self.soup("<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a>")
- self.assertSelects(soup.findAll('a', limit=3), ["1", "2", "3"])
- self.assertSelects(soup.findAll('a', limit=1), ["1"])
+ self.assertSelects(soup.find_all('a', limit=3), ["1", "2", "3"])
+ self.assertSelects(soup.find_all('a', limit=1), ["1"])
self.assertSelects(
- soup.findAll('a', limit=10), ["1", "2", "3", "4", "5"])
+ soup.find_all('a', limit=10), ["1", "2", "3", "4", "5"])
# A limit of 0 means no limit.
self.assertSelects(
- soup.findAll('a', limit=0), ["1", "2", "3", "4", "5"])
+ soup.find_all('a', limit=0), ["1", "2", "3", "4", "5"])
class TestFindAllByName(TreeTest):
"""Test ways of finding tags by tag name."""
@@ -95,33 +95,33 @@ class TestFindAllByName(TreeTest):
def test_find_all_by_tag_name(self):
# Find all the <a> tags.
self.assertSelects(
- self.tree.findAll('a'), ['First tag.', 'Nested tag.'])
+ self.tree.find_all('a'), ['First tag.', 'Nested tag.'])
def test_find_all_on_non_root_element(self):
# You can call find_all on any node, not just the root.
- self.assertSelects(self.tree.c.findAll('a'), ['Nested tag.'])
+ self.assertSelects(self.tree.c.find_all('a'), ['Nested tag.'])
def test_calling_element_invokes_find_all(self):
self.assertSelects(self.tree('a'), ['First tag.', 'Nested tag.'])
def test_find_all_by_tag_strainer(self):
self.assertSelects(
- self.tree.findAll(SoupStrainer('a')),
+ self.tree.find_all(SoupStrainer('a')),
['First tag.', 'Nested tag.'])
def test_find_all_by_tag_names(self):
self.assertSelects(
- self.tree.findAll(['a', 'b']),
+ self.tree.find_all(['a', 'b']),
['First tag.', 'Second tag.', 'Nested tag.'])
def test_find_all_by_tag_dict(self):
self.assertSelects(
- self.tree.findAll({'a' : True, 'b' : True}),
+ self.tree.find_all({'a' : True, 'b' : True}),
['First tag.', 'Second tag.', 'Nested tag.'])
def test_find_all_by_tag_re(self):
self.assertSelects(
- self.tree.findAll(re.compile('^[ab]$')),
+ self.tree.find_all(re.compile('^[ab]$')),
['First tag.', 'Second tag.', 'Nested tag.'])
def test_find_all_with_tags_matching_method(self):
@@ -135,26 +135,26 @@ class TestFindAllByName(TreeTest):
<b id="b">Match 2.</a>""")
self.assertSelects(
- tree.findAll(id_matches_name), ["Match 1.", "Match 2."])
+ tree.find_all(id_matches_name), ["Match 1.", "Match 2."])
class TestFindAllByAttribute(TreeTest):
def test_find_all_by_attribute_name(self):
- # You can pass in keyword arguments to findAll to search by
+ # You can pass in keyword arguments to find_all to search by
# attribute.
tree = self.soup("""
<a id="first">Matching a.</a>
<a id="second">
Non-matching <b id="first">Matching b.</b>a.
</a>""")
- self.assertSelects(tree.findAll(id='first'),
+ self.assertSelects(tree.find_all(id='first'),
["Matching a.", "Matching b."])
def test_find_all_by_attribute_dict(self):
# You can pass in a dictionary as the argument 'attrs'. This
# lets you search for attributes like 'name' (a fixed argument
- # to findAll) and 'class' (a reserved word in Python.)
+ # to find_all) and 'class' (a reserved word in Python.)
tree = self.soup("""
<a name="name1" class="class1">Name match.</a>
<a name="name2" class="class2">Class match.</a>
@@ -163,14 +163,14 @@ class TestFindAllByAttribute(TreeTest):
""")
# This doesn't do what you want.
- self.assertSelects(tree.findAll(name='name1'),
+ self.assertSelects(tree.find_all(name='name1'),
["A tag called 'name1'."])
# This does what you want.
- self.assertSelects(tree.findAll(attrs={'name' : 'name1'}),
+ self.assertSelects(tree.find_all(attrs={'name' : 'name1'}),
["Name match."])
# Passing class='class2' would cause a syntax error.
- self.assertSelects(tree.findAll(attrs={'class' : 'class2'}),
+ self.assertSelects(tree.find_all(attrs={'class' : 'class2'}),
["Class match."])
def test_find_all_by_class(self):
@@ -180,8 +180,8 @@ class TestFindAllByAttribute(TreeTest):
<a class="2">Class 2.</a>
<b class="1">Class 1.</a>
""")
- self.assertSelects(tree.findAll('a', '1'), ['Class 1.'])
- self.assertSelects(tree.findAll(attrs='1'), ['Class 1.', 'Class 1.'])
+ self.assertSelects(tree.find_all('a', '1'), ['Class 1.'])
+ self.assertSelects(tree.find_all(attrs='1'), ['Class 1.', 'Class 1.'])
def test_find_all_by_attribute_soupstrainer(self):
tree = self.soup("""
@@ -189,24 +189,24 @@ class TestFindAllByAttribute(TreeTest):
<a id="second">Non-match.</a>""")
strainer = SoupStrainer(attrs={'id' : 'first'})
- self.assertSelects(tree.findAll(strainer), ['Match.'])
+ self.assertSelects(tree.find_all(strainer), ['Match.'])
def test_find_all_with_missing_atribute(self):
- # You can pass in None as the value of an attribute to findAll.
+ # You can pass in None as the value of an attribute to find_all.
# This will match tags that do not have that attribute set.
tree = self.soup("""<a id="1">ID present.</a>
<a>No ID present.</a>
<a id="">ID is empty.</a>""")
- self.assertSelects(tree.findAll('a', id=None), ["No ID present."])
+ self.assertSelects(tree.find_all('a', id=None), ["No ID present."])
def test_find_all_with_defined_attribute(self):
- # You can pass in None as the value of an attribute to findAll.
+ # You can pass in None as the value of an attribute to find_all.
# This will match tags that have that attribute set to any value.
tree = self.soup("""<a id="1">ID present.</a>
<a>No ID present.</a>
<a id="">ID is empty.</a>""")
self.assertSelects(
- tree.findAll(id=True), ["ID present.", "ID is empty."])
+ tree.find_all(id=True), ["ID present.", "ID is empty."])
def test_find_all_with_numeric_attribute(self):
# If you search for a number, it's treated as a string.
@@ -214,8 +214,8 @@ class TestFindAllByAttribute(TreeTest):
<a id="1">Quoted attribute.</a>""")
expected = ["Unquoted attribute.", "Quoted attribute."]
- self.assertSelects(tree.findAll(id=1), expected)
- self.assertSelects(tree.findAll(id="1"), expected)
+ self.assertSelects(tree.find_all(id=1), expected)
+ self.assertSelects(tree.find_all(id="1"), expected)
def test_find_all_with_list_attribute_values(self):
# You can pass a list of attribute values instead of just one,
@@ -224,7 +224,7 @@ class TestFindAllByAttribute(TreeTest):
<a id="2">2</a>
<a id="3">3</a>
<a>No ID.</a>""")
- self.assertSelects(tree.findAll(id=["1", "3", "4"]),
+ self.assertSelects(tree.find_all(id=["1", "3", "4"]),
["1", "3"])
def test_find_all_with_regular_expression_attribute_value(self):
@@ -237,7 +237,7 @@ class TestFindAllByAttribute(TreeTest):
<a id="b">One b.</a>
<a>No ID.</a>""")
- self.assertSelects(tree.findAll(id=re.compile("^a+$")),
+ self.assertSelects(tree.find_all(id=re.compile("^a+$")),
["One a.", "Two as."])
@@ -318,8 +318,8 @@ class TestNextOperations(ProximityTest):
self.assertEquals(self.tree.next, None)
def test_find_all_next(self):
- self.assertSelects(self.start.findAllNext('b'), ["Two", "Three"])
- self.assertSelects(self.start.findAllNext(id=3), ["Three"])
+ self.assertSelects(self.start.find_allNext('b'), ["Two", "Three"])
+ self.assertSelects(self.start.find_allNext(id=3), ["Three"])
def test_find_next(self):
self.assertEquals(self.start.findNext('b')['id'], '2')
@@ -328,7 +328,7 @@ class TestNextOperations(ProximityTest):
def test_find_next_for_text_element(self):
text = self.tree.find(text="One")
self.assertEquals(text.findNext("b").string, "Two")
- self.assertSelects(text.findAllNext("b"), ["Two", "Three"])
+ self.assertSelects(text.find_allNext("b"), ["Two", "Three"])
def test_next_generator(self):
start = self.tree.find(text="Two")
@@ -369,8 +369,8 @@ class TestPreviousOperations(ProximityTest):
# of the "Three" node itself, which is why "Three" shows up
# here.
self.assertSelects(
- self.end.findAllPrevious('b'), ["Three", "Two", "One"])
- self.assertSelects(self.end.findAllPrevious(id=1), ["One"])
+ self.end.find_allPrevious('b'), ["Three", "Two", "One"])
+ self.assertSelects(self.end.find_allPrevious(id=1), ["One"])
def test_find_previous(self):
self.assertEquals(self.end.findPrevious('b')['id'], '3')
@@ -380,7 +380,7 @@ class TestPreviousOperations(ProximityTest):
text = self.tree.find(text="Three")
self.assertEquals(text.findPrevious("b").string, "Three")
self.assertSelects(
- text.findAllPrevious("b"), ["Three", "Two", "One"])
+ text.find_allPrevious("b"), ["Three", "Two", "One"])
def test_previous_generator(self):
start = self.tree.find(text="One")
@@ -623,7 +623,7 @@ class TestTreeModification(SoupTest):
def test_replace_with(self):
soup = self.soup(
"<p>There's <b>no</b> business like <b>show</b> business</p>")
- no, show = soup.findAll('b')
+ no, show = soup.find_all('b')
show.replaceWith(no)
self.assertEquals(
soup.decode(),