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

Week07 - Argument parsing #156

Open
wants to merge 4 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
70 changes: 70 additions & 0 deletions week06/average-squares-example/average_squares/squares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from argparse import ArgumentParser


def average_of_squares(list_of_numbers, list_of_weights=1):
""" 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
for number, weight
in zip(list_of_numbers, effective_weights)
]
return sum(squares)/len(list_of_numbers)


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__":

parser = ArgumentParser(description="Weighted mean of squared numbers")
parser.add_argument("numbers", type=int, nargs='+', help="the list of numbers")
arguments = parser.parse_args()

numbers = arguments.numbers
weights = [1] * len(numbers)

result = average_of_squares(numbers, weights)

# example input python squares.py 1 2 3

print(result)




20 changes: 20 additions & 0 deletions week06/average-squares-example/docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
4 changes: 4 additions & 0 deletions week06/average-squares-example/docs/_build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 442a4e5d640cbd610d0424118565e485
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Average Squares Documentation
=============================
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. SQUARES documentation master file, created by
sphinx-quickstart on Tue Nov 17 23:50:17 2020.

Welcome to SQUARES's documentation!
===================================
Simple project consisting of two functions. One
to calculate the weighted average of square numbers,
the other to convert strings into numbers.


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

Average Squares Documentation <content/average-squares-docs>


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

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