Added a function to add backslash before a space#541
Added a function to add backslash before a space#541MichalKesl wants to merge 3 commits intomainfrom
Conversation
|
If you do want to just replace all the In [3]: folder_name = 'foo bar baz'
In [4]: folder_name.replace(' ','\ ')
Out[4]: 'foo\\ bar\\ \\ baz'But there may be a better fix. This won't escape anything except spaces. Most functions don't need the path escaping. The few that do include
|
alongd
left a comment
There was a problem hiding this comment.
Thanks, please see some comments
arc/common.py
Outdated
| arc_r_symbols = [atom.element.symbol for atom in | ||
| chain(*tuple(arc_reaction.r_species[i].mol.atoms for i in range(num_rs)))] | ||
| arc_p_symbols = [atom.element.symbol for atom in | ||
| chain(*tuple(arc_reaction.p_species[i].mol.atoms for i in range(num_ps)))] |
There was a problem hiding this comment.
All the modifications to the file up to this point are style changes. If you think they are necessary, please add them as a different commit. I would recommend removing them, the previous version is "OK", I think
arc/common.py
Outdated
| new_folder_name (str): modified folder name. | ||
|
|
||
| """ | ||
| folder_name.split(" ") |
There was a problem hiding this comment.
this line has no effect (we don't store the returned value)
arc/common.py
Outdated
| """ | ||
| folder_name.split(" ") | ||
| new_folder_name = "" | ||
| for i in folder_name.split(" "): |
There was a problem hiding this comment.
minor: I would recommend changing i to a different name, i is usually an integer iterator. maybe use split?
There was a problem hiding this comment.
You can do either folder_name.split() or folder_name.split(" "), they are the same (the first one is shorter or "cleaner")
arc/common.py
Outdated
| folder_name.split(" ") | ||
| new_folder_name = "" | ||
| for i in folder_name.split(" "): | ||
| new_folder_name += i + "\ " |
There was a problem hiding this comment.
If you'd like, you can achieve this in a single line instead of the loop. Try:
new_folder_name = '\ '.join(folder_name.split())
Codecov Report
@@ Coverage Diff @@
## main #541 +/- ##
==========================================
+ Coverage 73.18% 73.20% +0.01%
==========================================
Files 99 99
Lines 26522 26534 +12
Branches 5546 5546
==========================================
+ Hits 19409 19423 +14
+ Misses 5772 5771 -1
+ Partials 1341 1340 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
alongd
left a comment
There was a problem hiding this comment.
Thanks @MichalKesl, looks good!
I added some comments, please take a look
arc/common_test.py
Outdated
| self.assertTrue(common.fill_in_the_blanks(ex1), "michalkfir") | ||
| self.assertTrue(common.fill_in_the_blanks(ex2), "michal\\ kfir") | ||
| self.assertTrue(common.fill_in_the_blanks(ex3), "mich\\ al\\ kfir") | ||
| self.assertTrue(common.fill_in_the_blanks(ex4), "michal\\ \\ kfir") |
There was a problem hiding this comment.
why do we have two slashes in the tests?
There was a problem hiding this comment.
If we do michal kfir.replace(" ", "\ ") the string changes to michal\\ kfir, because \ is an operator (like \n).
|
@MichalKesl, when you get a chance, can we finish this PR? |
d48f504 to
df9dff7
Compare
|
@MichalKesl is this PR relevant? |
|
@calvinp0, I revived it, could you please have a look and merge? |
There was a problem hiding this comment.
Pull Request Overview
This PR adds a utility function to handle file paths containing spaces by escaping them with backslashes. The function fill_in_the_blanks is implemented to modify folder names by adding backslashes before spaces, which is commonly needed in Unix-like systems for shell compatibility.
- Added
fill_in_the_blanksfunction to escape spaces in file paths - Integrated the function into the ARC class constructor to automatically handle project directories with spaces
- Added comprehensive test coverage for various space-containing scenarios
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| arc/common.py | Implements the fill_in_the_blanks function to escape spaces in folder names |
| arc/main.py | Imports and applies the new function to project directory paths |
| arc/common_test.py | Adds unit tests covering various space-escaping scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| Returns: | ||
| str: Modified folder name. | ||
| """ | ||
| return folder_name.replace(" ", "\ ") |
There was a problem hiding this comment.
The backslash escaping is incorrect. In Python string literals, you need to escape the backslash itself. This should be folder_name.replace(' ', '\\ ') or use a raw string folder_name.replace(' ', r'\ ').
| return folder_name.replace(" ", "\ ") | |
| return folder_name.replace(" ", "\\ ") |
| def fill_in_the_blanks(folder_name: str): | ||
| """ | ||
| Adding backslashes before spaces in the folder name. | ||
|
|
||
| Args: | ||
| folder_name (str): The string directory we want to modify. | ||
|
|
||
| Returns: | ||
| str: Modified folder name. | ||
| """ | ||
| return folder_name.replace(" ", "\ ") |
There was a problem hiding this comment.
[nitpick] The docstring should clarify that this function is for shell compatibility and mention that it works on any path, not just folder names. Also, the parameter description could be more precise: 'The file or directory path to escape'.
| def fill_in_the_blanks(folder_name: str): | |
| """ | |
| Adding backslashes before spaces in the folder name. | |
| Args: | |
| folder_name (str): The string directory we want to modify. | |
| Returns: | |
| str: Modified folder name. | |
| """ | |
| return folder_name.replace(" ", "\ ") | |
| def fill_in_the_blanks(path: str): | |
| """ | |
| Escape spaces in a file or directory path by adding backslashes before spaces for shell compatibility. | |
| This function is useful when constructing shell commands that use paths containing spaces. | |
| Args: | |
| path (str): The file or directory path to escape. | |
| Returns: | |
| str: The modified path string with spaces escaped. | |
| """ | |
| return path.replace(" ", "\ ") |
calvinp0
left a comment
There was a problem hiding this comment.
I am not really sure about this function - if we add the '\ ' to the space then I am not sure if it cause issues with functions like os.path.exists and os.makedirs and calling subprocesses.
|
I would like to see tests for those specific functions with this function being utilized |
If path has spaces the function will modify the name and add backslash before the space. a local test was preformed, the solution works.
fixes #535