Skip to content

Commit 7a413f6

Browse files
committed
feedback, cleanup
1 parent 040a747 commit 7a413f6

17 files changed

+3310
-381
lines changed

.idea/runConfigurations/retriever_local.xml

Lines changed: 0 additions & 25 deletions
This file was deleted.

code_to_optimize/bubble_sort_deps.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
from code_to_optimize.bubble_sort_dep1_helper import dep1_comparer
2-
from code_to_optimize.bubble_sort_dep2_swap import dep2_swap
31

42

53
def sorter_deps(arr):
6-
for i in range(len(arr)):
7-
for j in range(len(arr) - 1):
8-
if dep1_comparer(arr, j):
9-
dep2_swap(arr, j)
4+
n = len(arr)
5+
for i in range(n):
6+
# Introduce a flag that will allow us to exit early if no swaps occur
7+
swapped = False
8+
# Only iterate to the unsorted portion of the list
9+
for j in range(n - 1 - i):
10+
# Inline the comparison function
11+
if arr[j] > arr[j + 1]:
12+
# Inline the swap
13+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
14+
swapped = True
15+
# If no swaps occurred in the last inner loop, the array is sorted
16+
if not swapped:
17+
break
1018
return arr
11-

code_to_optimize/retriever_bubble_sort_imported.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

code_to_optimize/retriever_bubble_sort_with_math.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

code_to_optimize/retriever_globals.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

code_to_optimize/retriever_main.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

code_to_optimize/retriever_transform_utils.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

code_to_optimize/retriever_utils.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

codeflash/code_utils/code_replacer.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from codeflash.cli_cmds.console import logger
1111
from codeflash.code_utils.code_extractor import add_needed_imports_from_module
1212
from codeflash.models.models import FunctionParent
13-
import isort
1413

1514
if TYPE_CHECKING:
1615
from pathlib import Path
@@ -336,87 +335,4 @@ def function_to_optimize_original_worktree_fqn(
336335
str(worktrees[0].name / function_to_optimize.file_path.relative_to(git_root).with_suffix("")).replace("/", ".")
337336
+ "."
338337
+ function_to_optimize.qualified_name
339-
)
340-
341-
342-
def add_decorator_cst(module_node, function_name, decorator_name):
343-
"""Adds a decorator to a function definition in a LibCST module node."""
344-
345-
class AddDecoratorTransformer(cst.CSTTransformer):
346-
def leave_FunctionDef(self, original_node, updated_node):
347-
if original_node.name.value == function_name:
348-
new_decorator = cst.Decorator(
349-
decorator=cst.Name(value=decorator_name)
350-
)
351-
352-
updated_decorators = list(updated_node.decorators)
353-
updated_decorators.insert(0, new_decorator)
354-
355-
return updated_node.with_changes(decorators=updated_decorators)
356-
return updated_node
357-
358-
transformer = AddDecoratorTransformer()
359-
updated_module = module_node.visit(transformer)
360-
return updated_module
361-
362-
def add_decorator_imports(file_paths, fn_list, db_file):
363-
"""Adds a decorator to a function in a Python file."""
364-
for file_path, fn_name in zip(file_paths, fn_list):
365-
#open file
366-
with open(file_path, "r", encoding="utf-8") as file:
367-
file_contents = file.read()
368-
369-
# parse to cst
370-
module_node = cst.parse_module(file_contents)
371-
# add decorator
372-
module_node = add_decorator_cst(module_node, fn_name, 'profile')
373-
# add imports
374-
# Create a transformer to add the import
375-
transformer = ImportAdder("from line_profiler import profile")
376-
377-
# Apply the transformer to add the import
378-
module_node = module_node.visit(transformer)
379-
modified_code = isort.code(module_node.code, float_to_top=True)
380-
# write to file
381-
with open(file_path, "w", encoding="utf-8") as file:
382-
file.write(modified_code)
383-
#do this only for the main file and not the helper files, can use libcst but will go just with some simple string manipulation
384-
with open(file_paths[0],'r') as f:
385-
file_contents = f.readlines()
386-
for idx, line in enumerate(file_contents):
387-
if 'from line_profiler import profile' in line:
388-
file_contents.insert(idx+1, f"profile.enable(output_prefix='{db_file}')\n")
389-
break
390-
with open(file_paths[0],'w') as f:
391-
f.writelines(file_contents)
392-
393-
394-
395-
396-
class ImportAdder(cst.CSTTransformer):
397-
def __init__(self, import_statement='from line_profiler import profile'):
398-
self.import_statement = import_statement
399-
self.has_import = False
400-
401-
def leave_Module(self, original_node, updated_node):
402-
# If the import is already there, don't add it again
403-
if self.has_import:
404-
return updated_node
405-
406-
# Parse the import statement into a CST node
407-
import_node = cst.parse_statement(self.import_statement)
408-
409-
# Add the import to the module's body
410-
return updated_node.with_changes(
411-
body=[import_node] + list(updated_node.body)
412-
)
413-
414-
def visit_Import(self, node):
415-
pass
416-
417-
def visit_ImportFrom(self, node):
418-
# Check if the profile is already imported from line_profiler
419-
if node.module and node.module.value == "line_profiler":
420-
for import_alias in node.names:
421-
if import_alias.name.value == "profile":
422-
self.has_import = True
338+
)

codeflash/code_utils/coverage_utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,3 @@ def prepare_coverage_files() -> tuple[Path, Path]:
5959
coveragerc_content = f"[run]\n branch = True\ndata_file={coverage_database_file}\n"
6060
coveragercfile.write_text(coveragerc_content)
6161
return coverage_database_file, coveragercfile
62-
63-
def prepare_lprofiler_files(prefix="") -> tuple[Path]:
64-
"""Prepare line profiler output file."""
65-
lprofiler_database_file = get_run_tmp_file(prefix)
66-
return lprofiler_database_file

0 commit comments

Comments
 (0)