realpython / realpython/python-guide

adding a new @classmethod to an existing class

Open
#1,173 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.