Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Week06 - Using doctrings and doctest #103

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
62 changes: 62 additions & 0 deletions week06/average-squares-example/average_squares/squares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Computation of weighted average of squares."""


def average_of_squares(list_of_numbers, list_of_weights=None):
""" Return the weighted average of a list of values.

By default, all values are equally weighted, but this can be changed
by the list_of_weights argument.

Example:
--------
>>> average_of_squares([1, 2, 4])
7.0
>>> average_of_squares([2, 4], [1, 0.5])
6.0
>>> average_of_squares([1, 2, 4], [1, 0.5])
Traceback (most recent call last):
AssertionError: weights and numbers must have same length

"""
if list_of_weights is not None:
assert len(list_of_weights) == len(list_of_numbers), \
"weights and numbers must have same length"
effective_weights = list_of_weights
else:
effective_weights = [1] * len(list_of_numbers)
squares = [
(weight * number * number) / len(list_of_numbers)
for number, weight
in zip(list_of_numbers, effective_weights)
]

return sum(squares)


def convert_numbers(list_of_strings):
"""Convert a list of strings into numbers, ignoring whitespace.

Example:
--------
>>> convert_numbers(["4", " 8 ", "15 16", " 23 42 "])
[4, 8, 15, 16, 23, 42]

"""
all_numbers = []
for s in list_of_strings:
# Take each string in the list, split it into substrings separated by
# whitespace, and collect them into a single list...
all_numbers.extend([token.strip() for token in s.split()])
# ...then convert each substring into a number
return [int(number_string) for number_string in all_numbers]

if __name__ == "__main__":
numbers_strings = ["1","2","4"]
weight_strings = ["1","1","1"]

numbers = convert_numbers(numbers_strings)
weights = convert_numbers(weight_strings)

result = average_of_squares(numbers, weights)

print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Average Squares Documentation
=============================
.. autofunction:: average_of_squares
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Average Squares Documentation
=============================
.. autofunction:: average_of_squares
55 changes: 55 additions & 0 deletions week06/average-squares-example/docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('.'))


# -- Project information -----------------------------------------------------

project = 'documentation'
copyright = '2020, Regina Vivian Barli'
author = 'Regina Vivian Barli'

# The full version, including alpha/beta/rc tags
release = '0.0.1'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc'
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
24 changes: 24 additions & 0 deletions week06/average-squares-example/docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.. documentation documentation master file, created by
sphinx-quickstart on Fri Nov 13 21:51:39 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to documentation's documentation!
=========================================

Purpose of the project is to find the average squares and also convert numbers

.. toctree::
:maxdepth: 2
:caption: Contents:

average-square-docs



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`