-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support pathlib.Path in place of path strings #740
Comments
Adding the |
In [1]: import pathlib
In [2]: from openff.toolkit import Molecule, ForceField
In [3]: ForceField("openff-2.1.0.offxml").to_file(pathlib.Path("x.offxml"))
In [4]: !head x.offxml
<?xml version="1.0" encoding="utf-8"?>
<SMIRNOFF version="0.3" aromaticity_model="OEAroModel_MDL">
<Author>The Open Force Field Initiative</Author>
<Date>2023-05-02</Date>
<Constraints version="0.3">
<Constraint smirks="[#1:1]-[*:2]" id="c1"></Constraint>
<Constraint smirks="[#1:1]-[#8X2H2+0:2]-[#1]" id="c-tip3p-H-O" distance="0.9572 * angstrom ** 1"></Constraint>
<Constraint smirks="[#1:1]-[#8X2H2+0]-[#1:2]" id="c-tip3p-H-O-H" distance="1.5139006545247014 * angstrom ** 1"></Constraint>
</Constraints>
<Bonds version="0.4" potential="harmonic" fractional_bondorder_method="AM1-Wiberg" fractional_bondorder_interpolation="linear">
In [5]: Molecule.from_smiles("CCO").to_file(pathlib.Path("molecule.sdf"), file_format="sdf")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 Molecule.from_smiles("CCO").to_file(pathlib.Path("molecule.sdf"), file_format="sdf")
File ~/software/openff-toolkit/openff/toolkit/topology/molecule.py:4162, in FrozenMolecule.to_file(self, file_path, file_format, toolkit_registry)
4160 toolkit.to_file(self, file_path, file_format)
4161 else:
-> 4162 toolkit.to_file_obj(self, file_path, file_format)
File ~/software/openff-toolkit/openff/toolkit/utils/openeye_wrapper.py:554, in OpenEyeToolkitWrapper.to_file_obj(self, molecule, file_obj, file_format)
552 # This function requires a text-mode file_obj.
553 try:
--> 554 file_obj.write("")
555 except TypeError:
556 # Switch to a ValueError and use a more informative exception
557 # message to match RDKit.
558 raise ValueError(
559 "Need a text mode file object like StringIO or a file opened with mode 't'"
560 ) from None
AttributeError: 'PosixPath' object has no attribute 'write' |
5 tasks
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Is your feature request related to a problem? Please describe.
Some, likely many, I/O operations work on path strings but not
pathlib.Path
objects.Most Python APIs treat them the same, allowing for a largely painless transition from
os.path
topathlib.Path
.Describe the solution you'd like
Long-term, it would be nice for all methods that expect
str
to also work onPath
objects. Many probably already work, I have not looked into this deeply.Describe alternatives you've considered
There are decent enough workarounds
Additional context
This may be blocked by upstream dependencies (i.e. rdkit/rdkit#2923). Also looks like a
Pydantic
refactor may handle this for free (in the domains we control).pathlib
is in the standard library as of Python 3.4 or so.The text was updated successfully, but these errors were encountered: