mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
cleaning up
This commit is contained in:
parent
d24d24c25c
commit
82f4138539
5 changed files with 75 additions and 125 deletions
|
|
@ -1,15 +1,11 @@
|
|||
from code_to_optimize.bubble_sort_dep1_helper import dep1_comparer
|
||||
from code_to_optimize.bubble_sort_dep2_swap import dep2_swap
|
||||
|
||||
|
||||
def sorter_deps(arr):
|
||||
n = len(arr)
|
||||
for i in range(n):
|
||||
# We use a flag to check if the array is already sorted
|
||||
swapped = False
|
||||
# Reduce the range of j, since the last i elements are already sorted
|
||||
for j in range(n - 1 - i):
|
||||
if arr[j] > arr[j + 1]:
|
||||
# Swap without a helper function
|
||||
arr[j], arr[j + 1] = arr[j + 1], arr[j]
|
||||
swapped = True
|
||||
# If no elements were swapped in the inner loop, break
|
||||
if not swapped:
|
||||
break
|
||||
for i in range(len(arr)):
|
||||
for j in range(len(arr) - 1):
|
||||
if dep1_comparer(arr, j):
|
||||
dep2_swap(arr, j)
|
||||
return arr
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
from code_to_optimize.bubble_sort_in_nested_class import WrapperClass
|
||||
from line_profiler import profile as codeflash_line_profile
|
||||
|
||||
|
||||
@codeflash_line_profile
|
||||
def sort_classmethod(x):
|
||||
y = WrapperClass.BubbleSortClass()
|
||||
return y.sorter(x)
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
from code_to_optimize.bubble_sort_deps import sorter_deps
|
||||
|
||||
|
||||
def test_sort():
|
||||
input = [5, 4, 3, 2, 1, 0]
|
||||
output = sorter_deps(input)
|
||||
assert output == [0, 1, 2, 3, 4, 5]
|
||||
|
||||
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
|
||||
output = sorter_deps(input)
|
||||
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
|
||||
|
||||
input = list(reversed(range(5000)))
|
||||
output = sorter_deps(input)
|
||||
assert output == list(range(5000))
|
||||
|
|
@ -1,33 +1,4 @@
|
|||
"""
|
||||
This software is OSI Certified Open Source Software.
|
||||
OSI Certified is a certification mark of the Open Source Initiative.
|
||||
|
||||
Copyright (c) 2008, Enthought, Inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of Enthought, Inc. nor the names of its contributors may
|
||||
be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
"""Adapted from line_profiler (https://github.com/pyutils/line_profiler) written by Enthought, Inc. (BSD License)"""
|
||||
import linecache
|
||||
import inspect
|
||||
from codeflash.code_utils.tabulate import tabulate
|
||||
|
|
|
|||
|
|
@ -108,69 +108,69 @@ def sorter(arr):
|
|||
arr.sort()
|
||||
return arr
|
||||
|
||||
def test_pytest_line_profile_runner():
|
||||
def get_fto_cc(file_path):
|
||||
function_to_optimize = FunctionToOptimize(
|
||||
function_name="sort_classmethod", file_path=file_path, parents=[], starting_line=None, ending_line=None
|
||||
)
|
||||
original_helper_code: dict[Path, str] = {}
|
||||
test_config = TestConfig(
|
||||
tests_root=file_path.parent / "tests",
|
||||
tests_project_rootdir=file_path.parent.resolve(),
|
||||
project_root_path=file_path.parent.parent.resolve(),
|
||||
test_framework="pytest",
|
||||
pytest_cmd="pytest",
|
||||
)
|
||||
func_optimizer = FunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
|
||||
ctx_result = func_optimizer.get_code_optimization_context()
|
||||
if not is_successful(ctx_result):
|
||||
pytest.fail()
|
||||
code_context = ctx_result.unwrap()
|
||||
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
|
||||
for helper_function_path in helper_function_paths:
|
||||
with helper_function_path.open(encoding="utf8") as f:
|
||||
helper_code = f.read()
|
||||
original_helper_code[helper_function_path] = helper_code
|
||||
return func_optimizer, code_context, original_helper_code
|
||||
file_path = (Path(__file__) / ".." / ".." / "code_to_optimize" / "bubble_sort_classmethod.py").resolve()
|
||||
func_optimizer, code_context, original_helper_code = get_fto_cc(file_path)
|
||||
#check if decorators are added properly or not
|
||||
file_path_to_helper_classes = defaultdict(set)
|
||||
for function_source in code_context.helper_functions:
|
||||
if (
|
||||
function_source.qualified_name != func_optimizer.function_to_optimize.qualified_name
|
||||
and "." in function_source.qualified_name
|
||||
):
|
||||
file_path_to_helper_classes[function_source.file_path].add(function_source.qualified_name.split(".")[0])
|
||||
try:
|
||||
line_profiler_output_file = add_decorator_imports(func_optimizer.function_to_optimize, code_context)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
finally:
|
||||
func_optimizer.write_code_and_helpers(func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path)
|
||||
#now check if lp runs properly or not
|
||||
# test_env = os.environ.copy()
|
||||
# test_env["CODEFLASH_TEST_ITERATION"] = "0"
|
||||
# test_env["CODEFLASH_TRACER_DISABLE"] = "1"
|
||||
# if "PYTHONPATH" not in test_env:
|
||||
# test_env["PYTHONPATH"] = str(config.project_root_path)
|
||||
# else:
|
||||
# test_env["PYTHONPATH"] += os.pathsep + str(config.project_root_path)
|
||||
#
|
||||
# with tempfile.NamedTemporaryFile(prefix="test_xx", suffix=".py", dir=cur_dir_path) as fp:
|
||||
# test_files = TestFiles(
|
||||
# test_files=[TestFile(instrumented_behavior_file_path=Path(fp.name), test_type=TestType.EXISTING_UNIT_TEST)]
|
||||
# )
|
||||
# fp.write(code.encode("utf-8"))
|
||||
# fp.flush()
|
||||
# result_file, process = run_line_profile_tests(
|
||||
# test_files,
|
||||
# cwd=cur_dir_path,
|
||||
# test_env=test_env,
|
||||
# test_framework="pytest",
|
||||
# line_profiler_output_file=line_profiler_output_file,
|
||||
# pytest_cmd="pytest",
|
||||
# )
|
||||
# print(process.stdout)
|
||||
# result_file.unlink(missing_ok=True)
|
||||
# def test_pytest_line_profile_runner():
|
||||
# def get_fto_cc(file_path):
|
||||
# function_to_optimize = FunctionToOptimize(
|
||||
# function_name="sort_classmethod", file_path=file_path, parents=[], starting_line=None, ending_line=None
|
||||
# )
|
||||
# original_helper_code: dict[Path, str] = {}
|
||||
# test_config = TestConfig(
|
||||
# tests_root=file_path.parent / "tests",
|
||||
# tests_project_rootdir=file_path.parent.resolve(),
|
||||
# project_root_path=file_path.parent.parent.resolve(),
|
||||
# test_framework="pytest",
|
||||
# pytest_cmd="pytest",
|
||||
# )
|
||||
# func_optimizer = FunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
|
||||
# ctx_result = func_optimizer.get_code_optimization_context()
|
||||
# if not is_successful(ctx_result):
|
||||
# pytest.fail()
|
||||
# code_context = ctx_result.unwrap()
|
||||
# helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
|
||||
# for helper_function_path in helper_function_paths:
|
||||
# with helper_function_path.open(encoding="utf8") as f:
|
||||
# helper_code = f.read()
|
||||
# original_helper_code[helper_function_path] = helper_code
|
||||
# return func_optimizer, code_context, original_helper_code
|
||||
# file_path = (Path(__file__) / ".." / ".." / "code_to_optimize" / "bubble_sort_classmethod.py").resolve()
|
||||
# func_optimizer, code_context, original_helper_code = get_fto_cc(file_path)
|
||||
# #check if decorators are added properly or not
|
||||
# file_path_to_helper_classes = defaultdict(set)
|
||||
# for function_source in code_context.helper_functions:
|
||||
# if (
|
||||
# function_source.qualified_name != func_optimizer.function_to_optimize.qualified_name
|
||||
# and "." in function_source.qualified_name
|
||||
# ):
|
||||
# file_path_to_helper_classes[function_source.file_path].add(function_source.qualified_name.split(".")[0])
|
||||
# try:
|
||||
# line_profiler_output_file = add_decorator_imports(func_optimizer.function_to_optimize, code_context)
|
||||
# except Exception as e:
|
||||
# print(e)
|
||||
# finally:
|
||||
# func_optimizer.write_code_and_helpers(func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path)
|
||||
# #now check if lp runs properly or not
|
||||
# # test_env = os.environ.copy()
|
||||
# # test_env["CODEFLASH_TEST_ITERATION"] = "0"
|
||||
# # test_env["CODEFLASH_TRACER_DISABLE"] = "1"
|
||||
# # if "PYTHONPATH" not in test_env:
|
||||
# # test_env["PYTHONPATH"] = str(config.project_root_path)
|
||||
# # else:
|
||||
# # test_env["PYTHONPATH"] += os.pathsep + str(config.project_root_path)
|
||||
# #
|
||||
# # with tempfile.NamedTemporaryFile(prefix="test_xx", suffix=".py", dir=cur_dir_path) as fp:
|
||||
# # test_files = TestFiles(
|
||||
# # test_files=[TestFile(instrumented_behavior_file_path=Path(fp.name), test_type=TestType.EXISTING_UNIT_TEST)]
|
||||
# # )
|
||||
# # fp.write(code.encode("utf-8"))
|
||||
# # fp.flush()
|
||||
# # result_file, process = run_line_profile_tests(
|
||||
# # test_files,
|
||||
# # cwd=cur_dir_path,
|
||||
# # test_env=test_env,
|
||||
# # test_framework="pytest",
|
||||
# # line_profiler_output_file=line_profiler_output_file,
|
||||
# # pytest_cmd="pytest",
|
||||
# # )
|
||||
# # print(process.stdout)
|
||||
# # result_file.unlink(missing_ok=True)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue