Documentatie

Python-pakketten ontwikkelen

James Fulton

Climate informatics researcher

Waarom documenteren?

  • Helpt je gebruikers je code te gebruiken
  • Documenteer elke
    • Functie
    • Class
    • Classmethode
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-pakketten ontwikkelen

Waarom documenteren?

  • Helpt je gebruikers je code te gebruiken
  • Documenteer elke
    • Functie
    • Class
    • Classmethode
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-pakketten ontwikkelen

Waarom documenteren?

  • Helpt je gebruikers je code te gebruiken
  • Documenteer elke
    • Functie
    • Class
    • Classmethode
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-pakketten ontwikkelen

Functiedocumentatie

def count_words(filepath, words_list):

""" ... """
Python-pakketten ontwikkelen

Functiedocumentatie

def count_words(filepath, words_list):

"""Tel hoe vaak deze woorden in totaal voorkomen."""
Python-pakketten ontwikkelen

Functiedocumentatie

def count_words(filepath, words_list):

"""Tel hoe vaak deze woorden in totaal voorkomen. De telling gebeurt op een tekstbestand op de opgegeven locatie. """
Python-pakketten ontwikkelen

Functiedocumentatie

def count_words(filepath, words_list):

"""Tel hoe vaak deze woorden in totaal voorkomen. De telling gebeurt op een tekstbestand op de opgegeven locatie. [leg uit wat filepath en words_list zijn] [wat er wordt geretourneerd] """
Python-pakketten ontwikkelen

Documentatiestijl

Google-stijl voor documentatie

"""Samenvattende regel.

Uitgebreide beschrijving van de functie.

Args:
    arg1 (int): Beschrijving van arg1
    arg2 (str): Beschrijving van arg2

NumPy-stijl

    """Samenvattende regel.

    Uitgebreide beschrijving van de functie.

    Parameters
    ----------
    arg1 : int
        Beschrijving van arg1 ...

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

reStructuredText-stijl

    """Samenvattende regel.

    Uitgebreide beschrijving van de functie.

    :param arg1: Beschrijving van arg1
    :type arg1: int
    :param arg2: Beschrijving van arg2
    :type arg2: str

Epytext-stijl

"""Samenvattende regel.

  Uitgebreide beschrijving van de functie.

  @type arg1: int
  @param arg1:  Beschrijving van arg1
  @type arg2: str
  @param arg2: Beschrijving van arg2
Python-pakketten ontwikkelen

NumPy-documentatiestijl

Populair in wetenschappelijke Python-pakketten zoals

  • numpy
  • scipy
  • pandas
  • sklearn
  • matplotlib
  • dask
  • enz.
Python-pakketten ontwikkelen

NumPy-documentatiestijl

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.

Andere types zijn o.a. int, float, bool, str, dict, numpy.array, enz.

Python-pakketten ontwikkelen

NumPy-documentatiestijl

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'}
  • Noem meerdere typen voor een parameter als dat zinvol is
  • Noem toegestane waarden als er maar enkele opties zijn
Python-pakketten ontwikkelen

NumPy-documentatiestijl

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-pakketten ontwikkelen

NumPy-documentatiestijl

Andere secties

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

Sjablonen en stijlconversie voor documentatie

  • pyment kan docstrings genereren
  • Vanuit de terminal uitvoeren
  • Elke documentatiestijl uit
    • Google
    • Numpydoc
    • reST (reStructuredText)
    • Javadoc (epytext)
  • Documentatie omzetten van de ene stijl naar de andere
Python-pakketten ontwikkelen

Sjablonen en stijlconversie voor documentatie

pyment -w -o numpydoc textanalysis.py
def count_words(filepath, words_list):
    # Open the text file
    ...
    return n
  • -w - bestand overschrijven
  • -o numpydoc - uitvoer in NumPy-stijl
Python-pakketten ontwikkelen

Sjablonen en stijlconversie voor documentatie

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

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

    words_list :


    Returns
    -------
    type
    """
Python-pakketten ontwikkelen

Vertaal naar Google-stijl

pyment -w -o google textanalysis.py
def count_words(filepath, words_list):
    """Tel hoe vaak deze woorden in totaal voorkomen.

    De telling gebeurt op een tekstbestand op de opgegeven locatie.

    Parameters
    ----------
    filepath : str
        Pad naar tekstbestand.
    words_list : list of str
        Tel hoe vaak deze woorden voorkomen.

    Returns
    -------

    """
Python-pakketten ontwikkelen

Vertaal naar Google-stijl

pyment -w -o google textanalysis.py
def count_words(filepath, words_list):
    """Tel hoe vaak deze woorden in totaal voorkomen.

    De telling gebeurt op een tekstbestand op de opgegeven locatie.

    Args:
      filepath(str): Pad naar tekstbestand.
      words_list(list of str): Tel hoe vaak deze woorden voorkomen.

    Returns:


    """
Python-pakketten ontwikkelen

Documentatie voor package, subpackage en module

mysklearn/__init__.py

"""
Lineaire regressie voor Python
============================

mysklearn is een compleet pakket om
lineaire regressie in Python te implementeren. 
"""

mysklearn/preprocessing/__init__.py

"""
Een subpackage voor standaard pre-processing-bewerkingen.
"""

 

mysklearn/preprocessing/normalize.py

"""
Een module voor het normaliseren van data.
"""
Python-pakketten ontwikkelen

Laten we oefenen!

Python-pakketten ontwikkelen

Preparing Video For Download...