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 Photo.api_get by 8% in facebook_business/adobjects/photo.py #6

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 Nov 29, 2024

📄 Photo.api_get() in facebook_business/adobjects/photo.py

📈 Performance improved by 8% (0.08x faster)

⏱️ Runtime went down from 1.27 millisecond to 1.17 millisecond (best of 14 runs)

Explanation and details

Key Optimizations.

  1. Set Usage: Changed list to set for _accepted_fields to improve in operation speed.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 2 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
import copy
import importlib
import os
import warnings
from unittest.mock import MagicMock, patch

# unit tests
# imports
import pytest  # used for our unit tests
import six
from facebook_business import apiconfig
from facebook_business.adobjects.abstractcrudobject import AbstractCrudObject
from facebook_business.adobjects.objectparser import ObjectParser
from facebook_business.adobjects.photo import Photo
from facebook_business.api import Cursor, FacebookAdsApi, FacebookRequest
from facebook_business.exceptions import (FacebookBadObjectError,
                                          FacebookBadParameterError,
                                          FacebookBadParameterTypeException)
from facebook_business.typechecker import TypeChecker
from facebook_business.utils import api_utils


# Mocking necessary components
@pytest.fixture
def mock_api():
    api = MagicMock()
    api.call.return_value = MagicMock()
    return api

@pytest.fixture
def photo(mock_api):
    return Photo(fbid='12345', api=mock_api)

# Basic Functionality Test

















def test_api_get_invalid_node_id():
    photo = Photo(fbid='invalid_id')
    with pytest.raises(Exception):
        photo.api_get()


def test_api_get_api_call_failure(photo):
    photo._api.call.side_effect = Exception("API call failed")
    with pytest.raises(Exception):
        photo.api_get()

# Type Checking Tests



🔘 (none found) − ⏪ Replay Tests

### Key Optimizations.

2. **Set Usage**: Changed list to `set` for `_accepted_fields` to improve `in` operation speed.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Nov 29, 2024
@codeflash-ai codeflash-ai bot requested a review from Saga4 November 29, 2024 23:44
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