realpython / realpython/python-guide
adding a new @classmethod to an existing class
Nobody has claimed this yet.
- Dominant language
- Batchfile
- Stars
- 29.8k
- Forks
- 5.9k
- PR merge metrics
- No merged PRs in 30d
Description
I have a python programming issue which I have not solved.
In python3, using 'from opencadd.structure.core import Structure'
defines the class Structure. The section of the Structure code that I need to modify is
@classmethod
@redo.retriable(attempts=10, sleeptime=2)
def from_pdbid(cls, pdbid):
import mmtf
return cls(mmtf.fetch(pdbid))
where I would like to include
# New method for CIF files
@classmethod
def from_cif(cls, filepath: str):
"""
Create a Structure object from a local CIF file.
"""
parser = MMCIFParser()
structure_data = parser.get_structure("cif_file", filepath)
# ... (processing to build Volkamer Structure object)
return cls(atoms, residues, bonds)
This will skip trying to execute mmtf.fetch (which will timeout because pdb is no longer available for mmtf.fetch).
Instead using Structure.from_cif will avoid this issue and retrieve the file.
So far, I have not been able to do this successfully. Many blogs offer suggestions about adding a classmethod,
but I have failed and am asking for help from python exports.
Thanks in advance,
David
Fortran, R, Matlab, C, AWK, Perl, no Python
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
First verify whether this request belongs in python-guide: the payload targets opencadd.structure.core.Structure rather than a guide file. If it is in scope, read Structure.from_pdbid and the named mmtf/MMCIFParser entry points; done requires a defined, tested way to construct Structure objects from local CIF files.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 15/100