Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-26 - Optimize List Comprehensions with Walrus Operator
**Learning:** List comprehensions with redundant function calls (e.g., `[x.strip() for x in items if x.strip()]`) are a common anti-pattern that can be optimized using the walrus operator (`:=`) in Python 3.8+ to compute and bind the result once (e.g., `[stripped for x in items if (stripped := x.strip())]`).
**Action:** Always use the walrus operator when filtering and mapping list elements simultaneously to avoid redundant calculations.
3 changes: 2 additions & 1 deletion api/backup_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ async def process(self, input: dict, request: Request) -> dict | Response:
patterns_string = input.get("patterns", "")
if patterns_string and not include_patterns and not exclude_patterns:
# Parse legacy format
lines = [line.strip() for line in patterns_string.split('\n') if line.strip() and not line.strip().startswith('#')]
# Bolt: Avoid redundant strip() using walrus operator
lines = [stripped for line in patterns_string.split('\n') if (stripped := line.strip()) and not stripped.startswith('#')]
for line in lines:
if line.startswith('!'):
exclude_patterns.append(line[1:])
Expand Down
3 changes: 2 additions & 1 deletion api/backup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ async def process(self, input: dict, request: Request) -> dict | Response:
patterns_string = input.get("patterns", "")
if patterns_string and not include_patterns:
# Parse patterns string into arrays
lines = [line.strip() for line in patterns_string.split('\n') if line.strip() and not line.strip().startswith('#')]
# Bolt: Avoid redundant strip() using walrus operator
lines = [stripped for line in patterns_string.split('\n') if (stripped := line.strip()) and not stripped.startswith('#')]
for line in lines:
if line.startswith('!'):
exclude_patterns.append(line[1:])
Expand Down
3 changes: 2 additions & 1 deletion helpers/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ async def test_patterns(self, metadata: Dict[str, Any], max_files: int = 1000) -
patterns_string = self._patterns_to_string(include_patterns, exclude_patterns)

# Parse patterns using pathspec
pattern_lines = [line.strip() for line in patterns_string.split('\n') if line.strip() and not line.strip().startswith('#')]
# Bolt: Optimize list comprehension by avoiding redundant strip() calls
pattern_lines = [stripped for line in patterns_string.split('\n') if (stripped := line.strip()) and not stripped.startswith('#')]

if not pattern_lines:
return []
Expand Down
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def get_api_key(service: str) -> str:
)
# if the key contains a comma, use round-robin
if "," in key:
api_keys = [k.strip() for k in key.split(",") if k.strip()]
# Bolt: Avoid redundant strip() calculations with walrus operator
api_keys = [stripped for k in key.split(",") if (stripped := k.strip())]
api_keys_round_robin[service] = api_keys_round_robin.get(service, -1) + 1
key = api_keys[api_keys_round_robin[service] % len(api_keys)]
return key
Expand Down