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 DateTime._try_to_convert_date by 803% #32

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 20, 2024

📄 803% (8.03x) speedup for DateTime._try_to_convert_date in src/click/types.py

⏱️ Runtime : 36.0 milliseconds 3.99 milliseconds (best of 26 runs)

📝 Explanation and details

Explanation of changes:

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 3039 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
from __future__ import annotations

import collections.abc as cabc
import typing as t
from datetime import datetime

# imports
import pytest  # used for our unit tests
from src.click.types import DateTime


# unit tests
@pytest.fixture
def datetime_instance():
    return DateTime()

def test_valid_date_standard_format(datetime_instance):
    # Test standard date format
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05", "%Y-%m-%d")

def test_valid_date_iso_format(datetime_instance):
    # Test ISO 8601 date-time format
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T14:30:00", "%Y-%m-%dT%H:%M:%S")

def test_valid_date_space_format(datetime_instance):
    # Test space-separated date-time format
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05 14:30:00", "%Y-%m-%d %H:%M:%S")

def test_invalid_date_format(datetime_instance):
    # Test incorrect date format
    codeflash_output = datetime_instance._try_to_convert_date("2023/10/05", "%Y-%m-%d")

def test_non_date_string(datetime_instance):
    # Test non-date string
    codeflash_output = datetime_instance._try_to_convert_date("Hello, World!", "%Y-%m-%d")

def test_empty_string(datetime_instance):
    # Test empty string
    codeflash_output = datetime_instance._try_to_convert_date("", "%Y-%m-%d")


def test_whitespace_string(datetime_instance):
    # Test whitespace string
    codeflash_output = datetime_instance._try_to_convert_date(" ", "%Y-%m-%d")

def test_boundary_date_earliest(datetime_instance):
    # Test earliest possible date
    codeflash_output = datetime_instance._try_to_convert_date("0001-01-01", "%Y-%m-%d")

def test_boundary_date_latest(datetime_instance):
    # Test latest possible date
    codeflash_output = datetime_instance._try_to_convert_date("9999-12-31", "%Y-%m-%d")

def test_long_date_string(datetime_instance):
    # Test long date string
    long_date_string = "2023-10-05T14:30:00" * 1000
    codeflash_output = datetime_instance._try_to_convert_date(long_date_string, "%Y-%m-%dT%H:%M:%S")

def test_mixed_valid_invalid_dates(datetime_instance):
    # Test mixed valid and invalid dates
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05 and 2023-10-06", "%Y-%m-%d")
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T14:30:00, 2023-10-06T14:30:00", "%Y-%m-%dT%H:%M:%S")

def test_invalid_date_components(datetime_instance):
    # Test invalid month and day
    codeflash_output = datetime_instance._try_to_convert_date("2023-13-01", "%Y-%m-%d")
    codeflash_output = datetime_instance._try_to_convert_date("2023-02-30", "%Y-%m-%d")

def test_invalid_time_components(datetime_instance):
    # Test invalid hour and minute
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T25:00:00", "%Y-%m-%dT%H:%M:%S")
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T14:60:00", "%Y-%m-%dT%H:%M:%S")

def test_leap_year_handling_valid(datetime_instance):
    # Test valid leap year date
    codeflash_output = datetime_instance._try_to_convert_date("2020-02-29", "%Y-%m-%d")

def test_leap_year_handling_invalid(datetime_instance):
    # Test invalid leap year date
    codeflash_output = datetime_instance._try_to_convert_date("2019-02-29", "%Y-%m-%d")

def test_time_zone_information(datetime_instance):
    # Test ISO 8601 with time zone
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T14:30:00Z", "%Y-%m-%dT%H:%M:%S")
    codeflash_output = datetime_instance._try_to_convert_date("2023-10-05T14:30:00+02:00", "%Y-%m-%dT%H:%M:%S")

def test_performance_large_number_of_conversions(datetime_instance):
    # Test performance with a large number of conversions
    for _ in range(1000):
        codeflash_output = datetime_instance._try_to_convert_date("2023-10-05", "%Y-%m-%d")
        codeflash_output = datetime_instance._try_to_convert_date("invalid-date", "%Y-%m-%d")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from __future__ import annotations

import collections.abc as cabc
import typing as t
from datetime import datetime

# imports
import pytest  # used for our unit tests
from src.click.types import DateTime

# unit tests

# Basic Test Cases
def test_valid_date_standard_format():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15", "%Y-%m-%d")

def test_valid_date_time_format_with_T():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15T14:30:00", "%Y-%m-%dT%H:%M:%S")

def test_valid_date_time_format_with_space():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15 14:30:00", "%Y-%m-%d %H:%M:%S")

# Edge Test Cases
def test_invalid_date_format():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("15-10-2023", "%Y-%m-%d")

def test_non_date_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("not-a-date", "%Y-%m-%d")

def test_empty_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("", "%Y-%m-%d")


def test_whitespace_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("   ", "%Y-%m-%d")

def test_boundary_date():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("0001-01-01", "%Y-%m-%d")
    codeflash_output = dt._try_to_convert_date("9999-12-31", "%Y-%m-%d")

# Large Scale Test Cases
def test_very_long_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15" * 1000, "%Y-%m-%d")

def test_high_volume_of_valid_dates():
    dt = DateTime()
    for i in range(1000):
        date_str = f"2023-10-{15 + i % 10:02d}"
        codeflash_output = dt._try_to_convert_date(date_str, "%Y-%m-%d")

# Special Characters in Strings
def test_special_characters_in_date_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15!@#$", "%Y-%m-%d")

# Different Locale Formats
def test_locale_specific_date_format():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("15/10/2023", "%d/%m/%Y")
    codeflash_output = dt._try_to_convert_date("31-12-1999", "%d-%m-%Y")

# Mixed-Type Inputs

def test_date_time_with_time_zone():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15T14:30:00Z", "%Y-%m-%dT%H:%M:%S%z")
    codeflash_output = dt._try_to_convert_date("2023-10-15T14:30:00+02:00", "%Y-%m-%dT%H:%M:%S%z")

# Leap Year Dates
def test_leap_day():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2020-02-29", "%Y-%m-%d")
    codeflash_output = dt._try_to_convert_date("2019-02-29", "%Y-%m-%d")

# Invalid Formats
def test_incorrect_format_string():
    dt = DateTime()
    codeflash_output = dt._try_to_convert_date("2023-10-15", "%d-%m-%Y")
    codeflash_output = dt._try_to_convert_date("2023-10-15T14:30:00", "%d/%m/%Y %H:%M:%S")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from src.click.types import DateTime
import pytest

def test_DateTime__try_to_convert_date():
    with pytest.raises(TypeError, match='strptime\\(\\)\\ argument\\ 1\\ must\\ be\\ str,\\ not\\ SymbolicObject'):
        DateTime._try_to_convert_date(DateTime(formats=None), 0, '')

📢 Feedback on this optimization? Discord

@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 20, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 20, 2024 03: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