Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up method AddContentToPage.process_node by 11% in src/backend/base/langflow/components/Notion/add_content_to_page.py #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 12, 2024

📄 AddContentToPage.process_node in src/backend/base/langflow/components/Notion/add_content_to_page.py

✨ Performance Summary:

  • Speed Increase: 📈 11% (0.11x faster)
  • Runtime Reduction: ⏱️ From 38.2 milliseconds down to 34.5 milliseconds (best of 23 runs)

📝 Explanation and details

Sure, here's an optimized version of your provided code. I have eliminated some repetitive code and optimized conditions checking.


Correctness verification

The new optimized code was tested for correctness. The results are listed below:

Test Status Details
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 25 Passed See below
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Coverage 95.5%

🌀 Generated Regression Tests Details

Click to view details
from typing import Any

# imports
import pytest  # used for our unit tests
from bs4 import BeautifulSoup  # used to create HTML nodes for testing
# function to test
from langflow.base.langchain_utilities.model import LCToolComponent
from langflow.components.Notion.add_content_to_page import AddContentToPage

# unit tests

@pytest.fixture
def content_processor():
    return AddContentToPage()



def test_nested_paragraphs(content_processor):
    # Test nested paragraphs
    html = "<div><p>First paragraph.</p><p>Second paragraph.</p></div>"
    node = BeautifulSoup(html, 'html.parser').div
    expected = [
        {'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'First paragraph.'}}]}},
        {'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Second paragraph.'}}]}}
    ]
    codeflash_output = content_processor.process_node(node)

def test_nested_headings_and_paragraphs(content_processor):
    # Test nested headings and paragraphs
    html = "<div><h1>Main Heading</h1><p>Introduction paragraph.</p></div>"
    node = BeautifulSoup(html, 'html.parser').div
    expected = [
        {'object': 'block', 'type': 'heading_1', 'heading_1': {'rich_text': [{'type': 'text', 'text': {'content': 'Main Heading'}}]}},
        {'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Introduction paragraph.'}}]}}
    ]
    codeflash_output = content_processor.process_node(node)

def test_unordered_list(content_processor):
    # Test unordered list
    html = "<ul><li>Item 1</li><li>Item 2</li></ul>"
    node = BeautifulSoup(html, 'html.parser').ul
    expected = [
        {'object': 'block', 'type': 'bulleted_list_item', 'bulleted_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'Item 1'}}]}},
        {'object': 'block', 'type': 'bulleted_list_item', 'bulleted_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'Item 2'}}]}}
    ]
    codeflash_output = content_processor.process_node(node)

def test_ordered_list(content_processor):
    # Test ordered list
    html = "<ol><li>First</li><li>Second</li></ol>"
    node = BeautifulSoup(html, 'html.parser').ol
    expected = [
        {'object': 'block', 'type': 'numbered_list_item', 'numbered_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'First'}}]}},
        {'object': 'block', 'type': 'numbered_list_item', 'numbered_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'Second'}}]}}
    ]
    codeflash_output = content_processor.process_node(node)

def test_code_block(content_processor):
    # Test code block
    html = "<p><code>print('Hello World')</code></p>"
    node = BeautifulSoup(html, 'html.parser').p
    expected = [{'object': 'block', 'type': 'code', 'code': {'rich_text': [{'type': 'text', 'text': {'content': "print('Hello World')"}}], 'language': 'plain text'}}]
    codeflash_output = content_processor.process_node(node)

def test_image(content_processor):
    # Test image
    html = '<img src="https://example.com/image.png" />'
    node = BeautifulSoup(html, 'html.parser').img
    expected = [{'object': 'block', 'type': 'image', 'image': {'type': 'external', 'external': {'url': 'https://example.com/image.png'}}}]
    codeflash_output = content_processor.process_node(node)

def test_bookmark(content_processor):
    # Test bookmark
    html = '<a href="https://example.com">Example</a>'
    node = BeautifulSoup(html, 'html.parser').a
    expected = [{'object': 'block', 'type': 'bookmark', 'bookmark': {'rich_text': [{'type': 'text', 'text': {'content': 'Example'}}], 'url': 'https://example.com'}}]
    codeflash_output = content_processor.process_node(node)

def test_mixed_content(content_processor):
    # Test mixed content
    html = "<div><h2>Section Heading</h2><p>Some introductory text.</p><ul><li>Point 1</li><li>Point 2</li></ul></div>"
    node = BeautifulSoup(html, 'html.parser').div
    expected = [
        {'object': 'block', 'type': 'heading_2', 'heading_2': {'rich_text': [{'type': 'text', 'text': {'content': 'Section Heading'}}]}},
        {'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Some introductory text.'}}]}},
        {'object': 'block', 'type': 'bulleted_list_item', 'bulleted_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'Point 1'}}]}},
        {'object': 'block', 'type': 'bulleted_list_item', 'bulleted_list_item': {'rich_text': [{'type': 'text', 'text': {'content': 'Point 2'}}]}}
    ]
    codeflash_output = content_processor.process_node(node)

def test_table_with_rows(content_processor):
    # Test table with rows
    html = "<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"
    node = BeautifulSoup(html, 'html.parser').table
    expected = [
        {'object': 'block', 'type': 'table_row', 'table_row': {'cells': [[{'type': 'text', 'text': {'content': 'Cell 1'}}, {'type': 'text', 'text': {'content': 'Cell 2'}}]]}}
    ]
    codeflash_output = content_processor.process_node(node)



def test_invalid_html(content_processor):
    # Test invalid HTML
    html = "<div><p>Unclosed paragraph"
    node = BeautifulSoup(html, 'html.parser').div
    expected = [{'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Unclosed paragraph'}}]}}]
    codeflash_output = content_processor.process_node(node)


def test_multiple_nested_elements(content_processor):
    # Test multiple nested elements
    html = "<div>" + "<div>" * 10 + "<p>Deep paragraph</p>" + "</div>" * 10 + "</div>"
    node = BeautifulSoup(html, 'html.parser').div
    expected = [{'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Deep paragraph'}}]}}]
    codeflash_output = content_processor.process_node(node)


def test_unsupported_node_type(content_processor):
    # Test unsupported node type
    html = "<custom>Unsupported content</custom>"
    node = BeautifulSoup(html, 'html.parser').custom
    expected = []
    codeflash_output = content_processor.process_node(node)

def test_malformed_node(content_processor):
    # Test malformed node
    html = "<p>Paragraph with <b>bold text</p>"
    node = BeautifulSoup(html, 'html.parser').p
    expected = [{'object': 'block', 'type': 'paragraph', 'paragraph': {'rich_text': [{'type': 'text', 'text': {'content': 'Paragraph with bold text'}}]}}]
    codeflash_output = content_processor.process_node(node)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from typing import Any

# imports
import pytest  # used for our unit tests
from bs4 import BeautifulSoup  # used to create HTML nodes for testing
# function to test
from langflow.base.langchain_utilities.model import LCToolComponent
from langflow.components.Notion.add_content_to_page import AddContentToPage


# unit tests
class TestProcessNode:
    @pytest.fixture
    def processor(self):
        return AddContentToPage()

📣 **Feedback**

If you have any feedback or need assistance, feel free to join our Discord community:

Discord

Sure, here's an optimized version of your provided code. I have eliminated some repetitive code and optimized conditions checking.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 12, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 12, 2024 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants