Dokumentation

Python-Pakete entwickeln

James Fulton

Climate informatics researcher

Warum Dokumentation?

  • Hilft Nutzer:innen, deinen Code zu verwenden
  • Dokumentiere jeweils
    • Funktion
    • Klasse
    • Klassenmethode
import numpy as np
help(np.sum)
...
sum(a, axis=None, dtype=None, out=None)
    Sum of array elements over a given axis.

    Parameters
    ----------
    a : array_like
        Elements to sum.
    axis : None or int or tuple of ints, optional
        Axis or axes along which a sum is performed.  
        The default, axis=None, will sum all of the 
        elements of the input array.
...
Python-Pakete entwickeln

Warum Dokumentation?

  • Hilft Nutzer:innen, deinen Code zu verwenden
  • Dokumentiere jeweils
    • Funktion
    • Klasse
    • Klassenmethode
import numpy as np
help(np.array)
...
    array(object, dtype=None, copy=True)

    Create an array.

    Parameters
    ----------
    object : array_like
        An array, any object exposing the array 
        interface ...
    dtype : data-type, optional
        The desired data-type for the array. 
    copy : bool, optional
        If true (default), then the object is copied.
...
Python-Pakete entwickeln

Warum Dokumentation?

  • Hilft Nutzer:innen, deinen Code zu verwenden
  • Dokumentiere jeweils
    • Funktion
    • Klasse
    • Klassenmethode
import numpy as np
x = np.array([1,2,3,4])
help(x.mean)
...
mean(...) method of numpy.ndarray instance
    a.mean(axis=None, dtype=None, out=None)

    Returns the average of the array elements 
    along given axis.

    Refer to `numpy.mean` for full documentation.
...
Python-Pakete entwickeln

Funktionsdokumentation

def count_words(filepath, words_list):

""" ... """
Python-Pakete entwickeln

Funktionsdokumentation

def count_words(filepath, words_list):

"""Zählt, wie oft diese Wörter insgesamt vorkommen."""
Python-Pakete entwickeln

Funktionsdokumentation

def count_words(filepath, words_list):

"""Zählt, wie oft diese Wörter insgesamt vorkommen. Gezählt wird in einer Textdatei am angegebenen Pfad. """
Python-Pakete entwickeln

Funktionsdokumentation

def count_words(filepath, words_list):

"""Zählt, wie oft diese Wörter insgesamt vorkommen. Gezählt wird in einer Textdatei am angegebenen Pfad. [erkläre, was filepath und words_list sind] [was zurückgegeben wird] """
Python-Pakete entwickeln

Dokumentationsstile

Google-Dokumentationsstil

"""Summary line.

Extended description of function.

Args:
    arg1 (int): Description of arg1
    arg2 (str): Description of arg2

NumPy-Stil

    """Summary line.

    Extended description of function.

    Parameters
    ----------
    arg1 : int
        Description of arg1 ...

    Returns
    ----------
    numpy.ndarray

reStructuredText-Stil

    """Summary line.

    Extended description of function.

    :param arg1: Description of arg1
    :type arg1: int
    :param arg2: Description of arg2
    :type arg2: str

Epytext-Stil

"""Summary line.

  Extended description of function.

  @type arg1: int
  @param arg1:  Description of arg1
  @type arg2: str
  @param arg2: Description of arg2
Python-Pakete entwickeln

NumPy-Dokumentationsstil

Beliebt in wissenschaftlichen Python-Paketen wie

  • numpy
  • scipy
  • pandas
  • sklearn
  • matplotlib
  • dask
  • usw.
Python-Pakete entwickeln

NumPy-Dokumentationsstil

import scipy
help(scipy.percentile)
percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear')
    Compute the q-th percentile of the data along the specified axis.

    Returns the q-th percentile(s) of the array elements.


Parameters ----------
a : array_like
Input array or object that can be converted to an array.

Other types include - int, float, bool, str, dict, numpy.array, etc.

Python-Pakete entwickeln

NumPy-Dokumentationsstil

import scipy
help(scipy.percentile)
percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear')
    ...
    Parameters
    ----------
    ...
    axis : {int, tuple of int, None}
    ...
    interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
  • Mehrere Typen angeben, wenn sinnvoll
  • Akzeptierte Werte auflisten, wenn nur wenige gültig sind
Python-Pakete entwickeln

NumPy-Dokumentationsstil

import scipy
help(scipy.percentile)
percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear')
    ...
    Returns
    -------
    percentile : scalar or ndarray
        If `q` is a single percentile and `axis=None`, then the result
        is a scalar. If multiple percentiles are given, first axis of
        the result corresponds to the percentiles...
    ...
Python-Pakete entwickeln

NumPy-Dokumentationsstil

Weitere Abschnitte

  • Raises
  • See Also
  • Notes
  • References
  • Examples
1 https://numpydoc.readthedocs.io/en/latest/format.html
Python-Pakete entwickeln

Dokumentationsvorlagen und Stilübersetzung

  • pyment kann Docstrings erzeugen
  • Im Terminal ausführen
  • Beliebiger Dokumentationsstil aus
    • Google
    • Numpydoc
    • reST (also reStructuredText)
    • Javadoc (also epytext)
  • Dokumentation zwischen Stilen konvertieren
Python-Pakete entwickeln

Dokumentationsvorlagen und Stilübersetzung

pyment -w -o numpydoc textanalysis.py
def count_words(filepath, words_list):
    # Open the text file
    ...
    return n
  • -w – Datei überschreiben
  • -o numpydoc – Ausgabe im NumPy-Stil
Python-Pakete entwickeln

Dokumentationsvorlagen und Stilübersetzung

pyment -w -o numpydoc textanalysis.py
def count_words(filepath, words_list):
    """

    Parameters
    ----------
    filepath :

    words_list :


    Returns
    -------
    type
    """
Python-Pakete entwickeln

In Google-Stil übersetzen

pyment -w -o google textanalysis.py
def count_words(filepath, words_list):
    """Zählt, wie oft diese Wörter insgesamt vorkommen.

    Gezählt wird in einer Textdatei am angegebenen Pfad.

    Parameters
    ----------
    filepath : str
        Pfad zur Textdatei.
    words_list : list of str
        Zählt die Gesamtanzahl des Vorkommens dieser Wörter.

    Returns
    -------

    """
Python-Pakete entwickeln

In Google-Stil übersetzen

pyment -w -o google textanalysis.py
def count_words(filepath, words_list):
    """Zählt, wie oft diese Wörter insgesamt vorkommen.

    Gezählt wird in einer Textdatei am angegebenen Pfad.

    Args:
      filepath(str): Pfad zur Textdatei.
      words_list(list of str): Zählt die Gesamtanzahl des Vorkommens dieser Wörter.

    Returns:


    """
Python-Pakete entwickeln

Dokumentation für Package, Subpackage und Modul

mysklearn/__init__.py

"""
Lineare Regression für Python
============================

mysklearn ist ein vollständiges Paket zur Implementierung
linearer Regression in Python. 
"""

mysklearn/preprocessing/__init__.py

"""
Ein Subpackage für Standard-Preprocessing-Operationen.
"""

 

mysklearn/preprocessing/normalize.py

"""
Ein Modul zum Normalisieren von Daten.
"""
Python-Pakete entwickeln

Lass uns üben!

Python-Pakete entwickeln

Preparing Video For Download...