Skip to content

Commit f48c77d

Browse files
flat method rename
1 parent d3e5e6f commit f48c77d

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

codeflash/context/code_context_extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ def get_code_optimization_context(
8585
)
8686

8787
# Handle token limits
88-
final_read_writable_tokens = encoded_tokens_len(final_read_writable_code.__str__)
88+
final_read_writable_tokens = encoded_tokens_len(final_read_writable_code.flat)
8989
if final_read_writable_tokens > optim_token_limit:
9090
raise ValueError("Read-writable code has exceeded token limit, cannot proceed")
9191

9292
# Setup preexisting objects for code replacer
9393
preexisting_objects = set(
9494
chain(
95-
find_preexisting_objects(final_read_writable_code.__str__),
95+
find_preexisting_objects(final_read_writable_code.flat),
9696
*(find_preexisting_objects(codestring.code) for codestring in read_only_code_markdown.code_strings),
9797
)
9898
)

codeflash/models/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,19 @@ class CodeString(BaseModel):
139139
file_path: Optional[Path] = None
140140

141141

142-
SPLITTER_MARKER = "# codeflash-splitter__"
142+
LINE_SPLITTER_MARKER_PREFIX = "# codeflash-splitter__"
143143

144144

145145
def get_code_block_splitter(file_path: Path) -> str:
146-
return f"{SPLITTER_MARKER}{file_path}"
146+
return f"{LINE_SPLITTER_MARKER_PREFIX}{file_path}"
147147

148148

149149
class CodeStringsMarkdown(BaseModel):
150150
code_strings: list[CodeString] = []
151151
cached_code: Optional[str] = None
152152

153153
@property
154-
def __str__(self) -> str:
154+
def flat(self) -> str:
155155
if self.cached_code is not None:
156156
return self.cached_code
157157
self.cached_code = "\n".join(
@@ -174,7 +174,7 @@ def path_to_code_string(self) -> dict[str, str]:
174174

175175
@staticmethod
176176
def from_str_with_markers(code_with_markers: str) -> CodeStringsMarkdown:
177-
pattern = rf"{SPLITTER_MARKER}([^\n]+)\n"
177+
pattern = rf"{LINE_SPLITTER_MARKER_PREFIX}([^\n]+)\n"
178178
matches = list(re.finditer(pattern, code_with_markers))
179179

180180
results = []

codeflash/optimization/function_optimizer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def can_be_optimized(self) -> Result[tuple[bool, CodeOptimizationContext, dict[P
165165
helper_code = f.read()
166166
original_helper_code[helper_function_path] = helper_code
167167

168-
if has_any_async_functions(code_context.read_writable_code.__str__):
168+
if has_any_async_functions(code_context.read_writable_code.flat):
169169
return Failure("Codeflash does not support async functions in the code to optimize.")
170170
# Random here means that we still attempt optimization with a fractional chance to see if
171171
# last time we could not find an optimization, maybe this time we do.
@@ -284,7 +284,7 @@ def optimize_function(self) -> Result[BestOptimization, str]:
284284

285285
should_run_experiment, code_context, original_helper_code = initialization_result.unwrap()
286286

287-
code_print(code_context.read_writable_code.__str__)
287+
code_print(code_context.read_writable_code.flat)
288288

289289
test_setup_result = self.generate_and_instrument_tests( # also generates optimizations
290290
code_context, should_run_experiment=should_run_experiment
@@ -376,7 +376,7 @@ def determine_best_candidate(
376376
ai_service_client = self.aiservice_client if exp_type == "EXP0" else self.local_aiservice_client
377377
future_line_profile_results = executor.submit(
378378
ai_service_client.optimize_python_code_line_profiler,
379-
source_code=code_context.read_writable_code.__str__,
379+
source_code=code_context.read_writable_code.flat,
380380
dependency_code=code_context.read_only_context_code,
381381
trace_id=self.function_trace_id[:-4] + exp_type if self.experiment_id else self.function_trace_id,
382382
line_profiler_results=original_code_baseline.line_profile_results["str_out"],
@@ -790,7 +790,7 @@ def generate_tests_and_optimizations(
790790
)
791791
future_optimization_candidates = executor.submit(
792792
self.aiservice_client.optimize_python_code,
793-
read_writable_code.__str__,
793+
read_writable_code.flat,
794794
read_only_context_code,
795795
self.function_trace_id[:-4] + "EXP0" if run_experiment else self.function_trace_id,
796796
N_CANDIDATES,
@@ -809,7 +809,7 @@ def generate_tests_and_optimizations(
809809
if run_experiment:
810810
future_candidates_exp = executor.submit(
811811
self.local_aiservice_client.optimize_python_code,
812-
read_writable_code.__str__,
812+
read_writable_code.flat,
813813
read_only_context_code,
814814
self.function_trace_id[:-4] + "EXP1",
815815
N_CANDIDATES,

tests/test_code_context_extractor.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def main_method(self):
126126
```
127127
"""
128128

129-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
129+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
130130
assert read_only_context.strip() == expected_read_only_context.strip()
131131
assert hashing_context.strip() == expected_hashing_context.strip()
132132

@@ -200,7 +200,7 @@ def topologicalSort(self):
200200
```
201201
"""
202202

203-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
203+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
204204
assert read_only_context.strip() == expected_read_only_context.strip()
205205
assert hashing_context.strip() == expected_hashing_context.strip()
206206

@@ -260,7 +260,7 @@ def sort_from_another_file(arr):
260260
return sorted_arr
261261
```
262262
"""
263-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
263+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
264264
assert read_only_context.strip() == expected_read_only_context.strip()
265265
assert hashing_context.strip() == expected_hashing_context.strip()
266266

@@ -647,7 +647,7 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
647647
return self.__backend__.get_cache_or_call(func=self.__wrapped__, args=args, kwargs=kwargs, lifespan=self.__duration__)
648648
```
649649
"""
650-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
650+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
651651
assert read_only_context.strip() == expected_read_only_context.strip()
652652
assert hashing_context.strip() == expected_hashing_context.strip()
653653

@@ -740,7 +740,7 @@ def helper_method(self):
740740
```
741741
"""
742742

743-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
743+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
744744
assert read_only_context.strip() == expected_read_only_context.strip()
745745
assert hashing_context.strip() == expected_hashing_context.strip()
746746

@@ -836,7 +836,7 @@ def helper_method(self):
836836
return self.x
837837
```
838838
"""
839-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
839+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
840840
assert read_only_context.strip() == expected_read_only_context.strip()
841841
assert hashing_context.strip() == expected_hashing_context.strip()
842842

@@ -923,7 +923,7 @@ def helper_method(self):
923923
return self.x
924924
```
925925
"""
926-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
926+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
927927
assert read_only_context.strip() == expected_read_only_context.strip()
928928
assert hashing_context.strip() == expected_hashing_context.strip()
929929

@@ -1118,7 +1118,7 @@ def fetch_and_process_data():
11181118
return processed
11191119
```
11201120
"""
1121-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1121+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
11221122
assert read_only_context.strip() == expected_read_only_context.strip()
11231123
assert hashing_context.strip() == expected_hashing_context.strip()
11241124

@@ -1217,7 +1217,7 @@ def fetch_and_transform_data():
12171217
return transformed
12181218
```
12191219
"""
1220-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1220+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
12211221
assert read_only_context.strip() == expected_read_only_context.strip()
12221222
assert hashing_context.strip() == expected_hashing_context.strip()
12231223

@@ -1297,7 +1297,7 @@ def transform_data_own_method(self, data: str) -> str:
12971297
```
12981298
"""
12991299

1300-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1300+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
13011301
assert read_only_context.strip() == expected_read_only_context.strip()
13021302
assert hashing_context.strip() == expected_hashing_context.strip()
13031303

@@ -1372,7 +1372,7 @@ def transform_data_same_file_function(self, data: str) -> str:
13721372
```
13731373
"""
13741374

1375-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1375+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
13761376
assert read_only_context.strip() == expected_read_only_context.strip()
13771377
assert hashing_context.strip() == expected_hashing_context.strip()
13781378

@@ -1434,7 +1434,7 @@ def update_data(data):
14341434
```
14351435
"""
14361436

1437-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1437+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
14381438
assert read_only_context.strip() == expected_read_only_context.strip()
14391439
assert hashing_context.strip() == expected_hashing_context.strip()
14401440

@@ -1510,7 +1510,7 @@ def circular_dependency(self, data):
15101510
```
15111511
"""
15121512

1513-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1513+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
15141514
assert read_only_context.strip() == expected_read_only_context.strip()
15151515
assert hashing_context.strip() == expected_hashing_context.strip()
15161516

@@ -1576,7 +1576,7 @@ def target_method(self):
15761576
return self.x + self.y
15771577
```
15781578
"""
1579-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1579+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
15801580
assert read_only_context.strip() == expected_read_only_context.strip()
15811581
assert hashing_context.strip() == expected_hashing_context.strip()
15821582

@@ -1658,7 +1658,7 @@ def fetch_and_transform_data():
16581658
def function_to_optimize():
16591659
return code_to_optimize.code_directories.retriever.main.fetch_and_transform_data()
16601660
"""
1661-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1661+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
16621662
assert read_only_context.strip() == expected_read_only_context.strip()
16631663
assert hashing_context.strip() == expected_hashing_context.strip()
16641664

@@ -1902,7 +1902,7 @@ def calculate(self, operation, x, y):
19021902
```
19031903
"""
19041904
# Verify the contexts match the expected values
1905-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
1905+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
19061906
assert read_only_context.strip() == expected_read_only_context.strip()
19071907
assert hashing_context.strip() == expected_hashing_context.strip()
19081908

@@ -2113,7 +2113,7 @@ def __init__(self, precision="high", fallback_precision=None, mode="standard"):
21132113
CALCULATION_BACKEND = "python"
21142114
```
21152115
"""
2116-
assert read_write_context.__str__.strip() == expected_read_write_context.strip()
2116+
assert read_write_context.flat.strip() == expected_read_write_context.strip()
21172117
assert read_only_context.strip() == expected_read_only_context.strip()
21182118

21192119

0 commit comments

Comments
 (0)