> As you say, it's a minor thing (either way) but I've never had the
> need for a `with_stem` method. Do you have a real-life use case (as
> opposed to just wanting it for completeness)?
>
> Having  a real example would help decide the appropriate behaviour for
> corner cases like x.with_stem('name.ext'). Your two suggested
> equivalents behave differently in that case:
>
> >>> old_path = Path(r"C:\x.txt")
> >>> old_path.with_name("y.xxx" + old_path.suffix)
> WindowsPath('C:/y.xxx.txt')
> >>> old_path.with_name("y.xxx").with_suffix(old_path.suffix)
> WindowsPath('C:/y.txt')
>

I hadn't considered that case. Worth discussing.

Not just "for completeness", I had a real-life use case this morning;
reached for it, wasn't there, frustrated.

The relevant chunk of the code below is here, in which you can see I would
have like to use this feature twice:

for ym in moduli:
    update_soup(soup, moduli)
    new_name =  filename_formatter.format(ym=ym)
    new_path =
source_file_path.with_name(new_name).with_suffix(source_file_path.suffix)
    new_path.write_text(soup.prettify())
    run_model(new_path)
    deflection = get_deflection()
        discard_data()  # completed files are very large; close program and
don't save finished file
        deflection_stem =
deflection_formatter.format(deflection=deflection)

new_path.rename(new_path.with_name(deflection_stem).with_suffix(source_file_path.suffix)

Would be nice to say:

for ym in moduli:
    update_soup(soup, moduli)
    new_name =  filename_formatter.format(ym=ym)
    new_path = source_file_path.with_stem(new_name)  # yay!
    new_path.write_text(soup.prettify())
    run_model(new_path)
    deflection = get_deflection()
    discard_data()  # completed files are very large; close program and
don't save finished file
    deflection_stem =  deflection_formatter.format(deflection=deflection)
    new_path.rename(new_path.with_stem(deflection_stem)  # yay!


*Details*

I have a file named like so:

*5340 ksi 69% plastic E 90% plastic TIG - 25.02 mm.liml*

It is an XML format input file (for a finite element analysis program).

The file name is my own convention; it has the YM as part of the file name
(with some other info I'm not changing), and the deflection of a model of a
wall in mm at the end.

I'm doing a sensitivity analysis so the goal is to make a copy of the input
file changing a single input parameter for one of the materials (YM) and
name the new input file appropriately (with XX as an intial placeholder for
the final deflection), run the model, obtain the model deflection, and
finally rename the file with the final deflection.

I'm parsing the file name (using pent) and content (using beautifulsoup to
get the XML element), replacing the YM in the relevant XML attribute, and
copying the file to a new filename.

In the end I'll have a directory of files like this:

5340 ksi 69% plastic E 90% plastic TIG - 25.02 mm.liml
3041 ksi 69% plastic E 90% plastic TIG - XX mm.liml
2087 ksi 69% plastic E 90% plastic TIG - XX mm.liml
1693 ksi 69% plastic E 90% plastic TIG - XX mm.liml

(the XX of each will be replaced after the files have been run)

The code I am using to do much of this is, roughly:

# define starting file and some list of moduli i am interested in (strings)
moduli = [ "3041 ksi", "2087 ksi", "1693 ksi", ]
source_file_path =  Path("5340 ksi 69% plastic E 90% plastic TIG - 25.02
mm.liml")

# parse the filename
captured = pent.Parser(body="#.+i &. ~! #.+d
@.mm").parse_body(source_file_path.stem)[0][0]
captured_string = " ".join(captured)
filename_formatter = f"{{ym}} {captured_string} XX mm"
deflection_formatter = f"{{ym}} {captured_string} {{deflection:.2f} mm}"

# load the XML soup
with  source_file_path.open() as doc:
    soup = bs4.bs.BeautifulSoup(doc, 'html.parser')

# create a file for each young's modulus, run it, and rename it
for ym in moduli:
    update_soup(soup, moduli)
    new_name =  filename_formatter.format(ym=ym)
    new_path = source_file_path.with_stem(new_name)  # shorter code: yay!
    new_path.write_text(soup.prettify())
    run_model(new_path)
    deflection = get_deflection()
    discard_data()  # completed files are very large; close program and
don't save finished file
    deflection_stem =  deflection_formatter.format(deflection=deflection,
ym=ym)
    new_path.rename(new_path.with_stem(deflection_stem)  # shorter
code: yay!
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JQQPWQ3YVZU64B62P3ZCYQZQ4ZHYA2CT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to