Skip to content

Commit 654a6ec

Browse files
refactoring
1 parent 57f8af0 commit 654a6ec

File tree

5 files changed

+12
-23
lines changed

5 files changed

+12
-23
lines changed
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
DEFAULT_API_URL = "https://api.galileo.ai/"
22
DEFAULT_APP_URL = "https://app.galileo.ai/"
3-
4-
5-
# function_names: GalileoApiClient.get_console_url
6-
# module_abs_path : /home/mohammed/Work/galileo-python/src/galileo/api_client.py
7-
# preexisting_objects: {('GalileoApiClient', ()), ('_set_destination', ()), ('get_console_url', (FunctionParent(name='GalileoApiClient', type='ClassDef'),))}
8-
# project_root_path: /home/mohammed/Work/galileo-python/src

codeflash/models/models.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,19 @@ def markdown(self) -> str:
169169
]
170170
)
171171

172-
def path_to_code_string(self) -> dict[str, str]:
173-
return {str(code_string.file_path): code_string.code for code_string in self.code_strings}
174-
175172
@staticmethod
176-
def from_str_with_markers(code_with_markers: str) -> CodeStringsMarkdown:
173+
def parse_splitter_markers(code_with_markers: str) -> dict[str, str]:
177174
pattern = rf"{LINE_SPLITTER_MARKER_PREFIX}([^\n]+)\n"
178175
matches = list(re.finditer(pattern, code_with_markers))
179176

180-
results = []
177+
results = {}
181178
for i, match in enumerate(matches):
182179
start = match.end()
183180
end = matches[i + 1].start() if i + 1 < len(matches) else len(code_with_markers)
184181
file_path = match.group(1).strip()
185182
code = code_with_markers[start:end].lstrip("\n")
186-
results.append(CodeString(file_path=file_path, code=code))
187-
return CodeStringsMarkdown(code_strings=results)
183+
results[file_path] = code
184+
return results
188185

189186

190187
class CodeOptimizationContext(BaseModel):

codeflash/optimization/function_optimizer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,22 @@ def replace_function_and_helpers_with_optimized_code(
621621
read_writable_functions_by_file_path[self.function_to_optimize.file_path].add(
622622
self.function_to_optimize.qualified_name
623623
)
624-
file_to_code_context = CodeStringsMarkdown.from_str_with_markers(optimized_code).path_to_code_string()
625-
logger.debug(f"Optimized code: {file_to_code_context}")
624+
625+
file_to_code_context = CodeStringsMarkdown.parse_splitter_markers(optimized_code)
626+
626627
for helper_function in code_context.helper_functions:
627628
if helper_function.jedi_definition.type != "class":
628629
read_writable_functions_by_file_path[helper_function.file_path].add(helper_function.qualified_name)
630+
629631
for module_abspath, qualified_names in read_writable_functions_by_file_path.items():
630632
relative_module_path = str(module_abspath.relative_to(self.project_root))
631633
logger.debug(f"applying optimized code to: {relative_module_path}")
632634

633635
scoped_optimized_code = file_to_code_context.get(relative_module_path, None)
634636
if scoped_optimized_code is None:
635637
logger.warning(
636-
f"Optimized code not found for {relative_module_path}, existing files in the context are: {list(file_to_code_context.keys())}, re-check your 'split markers'"
638+
f"Optimized code not found for {relative_module_path} In the context\n-------\n{optimized_code}\n-------\n"
639+
"Existing files in the context are: {list(file_to_code_context.keys())}, re-check your 'split markers'"
637640
)
638641
scoped_optimized_code = ""
639642

tests/test_code_context_extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,8 +2449,8 @@ def simple_method(self):
24492449
assert "return 42" in code_content
24502450

24512451

2452-
2453-
def test_replace_functions_and_add_imports():
2452+
# This shouldn't happen as we are now using a scoped optimization context, but keep it just in case
2453+
def test_circular_deps():
24542454
path_to_root = Path(__file__).resolve().parent.parent / "code_to_optimize" / "code_directories" / "circular_deps"
24552455
file_abs_path = path_to_root / "api_client.py"
24562456
optimized_code = Path(path_to_root / "optimized.py").read_text(encoding="utf-8")

tests/test_multi_file_code_replacement.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ def _get_string_usage(text: str) -> Usage:
5656
5757
from pydantic_ai_slim.pydantic_ai.messages import BinaryContent, UserContent
5858
59-
# Compile regex once, as in original
6059
_TOKEN_SPLIT_RE = re.compile(r'[\\s",.:]+')
61-
62-
# Precompute translation table for fast token splitting for string input
63-
# This covers the chars: whitespace (\\x09-\\x0d, space), " (0x22), , (0x2c),
64-
# Map those codepoints to ' '
6560
_translate_table = {{ord(c): ord(' ') for c in ' \\t\\n\\r\\x0b\\x0c",.:'}}
6661
6762
def _estimate_string_tokens(content: str | Sequence[UserContent]) -> int:

0 commit comments

Comments
 (0)