Code Monkey home page Code Monkey logo

Comments (1)

jieguangzhou avatar jieguangzhou commented on June 8, 2024

Yes, we need to do this.
Something like this

import re
import inspect

def extract_params(docstring):
    """Extract :param lines from a docstring with their indentation."""
    if not docstring:
        return []
    return re.findall(r'(\s*:param [^:]+: [^\n]+)', docstring)

def merge_docs(cls):
    """Decorator to merge :param lines from all parent classes."""
    parent_params = set()
    for base in inspect.getmro(cls)[1:]:
        if base.__doc__:
            parent_params.update(extract_params(base.__doc__))
    
    if cls.__doc__:
        cls_params = set(extract_params(cls.__doc__))
    else:
        cls_params = set()

    unique_params = parent_params - cls_params

    # Combine the class's original docstring with the new parameters
    combined_doc = cls.__doc__ or ""
    if combined_doc and unique_params:
        combined_doc = combined_doc.rstrip() + "\n\n"
    combined_doc += "\n".join(unique_params)

    cls.__doc__ = combined_doc
    return cls

@merge_docs
class OpenAI:
    """OpenAI base class.
    
    :param api_key: The API key for authentication.
    :param timeout: The timeout for API requests.
    """

    def __init__(self, api_key, timeout):
        self.api_key = api_key
        self.timeout = timeout

@merge_docs
class OpenAIAudioTranslation(OpenAI):
    """OpenAI audio translation predictor.

    :param takes_context: Whether the model takes context into account.
    :param prompt: The prompt to guide the model's style. Should contain ``{context}``.
    """

    def __init__(self, api_key, timeout, takes_context, prompt):
        super().__init__(api_key, timeout)
        self.takes_context = takes_context
        self.prompt = prompt

# Print the updated __doc__ for the subclass
print(OpenAIAudioTranslation.__doc__)

from superduperdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.