Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pathlib + f-strings #11

Merged
merged 2 commits into from
Mar 25, 2024
Merged

Pathlib + f-strings #11

merged 2 commits into from
Mar 25, 2024

Conversation

ecomodeller
Copy link
Member

@ecomodeller ecomodeller commented Jan 12, 2024

Pathlib is an object oriented way of handling files and paths (Python 3.4+)

f-strings is string interpolation, more readable than concatenating strings (similar to C# https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation) (Python 3.6+)

This PR is not a complete conversion of all files, only engine1d.py. Use it as inspiration.

@gedaskir
Copy link
Collaborator

@ecomodeller I think the current change makes the string/path manipulations look more readable. Regarding the use of os.path vs. pathlib.Path I think it is a matter of preference and both can be used equally well, especially in the case if the old code everywhere used os.path. Though, I also prefer pathlib. It is good to avoid statements like file_name = file.split('.')[0] and if using os.path it could be written as name, extension = os.path.splitext('/path/to/somefile.ext'). Regarding the + operator for strings, even PEP8 has something to say about it (though performance consideration likely here is not an issue):

For example, do not rely on CPython’s efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn’t present at all in implementations that don’t use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.

@ryan-kipawa ryan-kipawa added help wanted Extra attention is needed good first issue Good for newcomers labels Mar 21, 2024
@wuwwen wuwwen self-requested a review March 25, 2024 06:20
@wuwwen wuwwen merged commit 55edd4a into main Mar 25, 2024
1 check passed
@wuwwen wuwwen deleted the pathlib branch March 28, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants