Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of argparse library @Chole1428 #44

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
_build/
_static/
_templates/
20 changes: 20 additions & 0 deletions 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)
28 changes: 28 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'Average_squares'
copyright = '2024, Fai'
author = 'Fai'
release = '2024'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']
18 changes: 18 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. Average_squares documentation master file, created by
sphinx-quickstart on Sun Nov 10 23:20:04 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Average_squares documentation
=============================

Add your content using ``reStructuredText`` syntax. See the
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
documentation for details.

The purpos of this project is to calculate the average_of_squares and then convert_numbers from strings into numbers

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

35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
36 changes: 21 additions & 15 deletions squares.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import argparse

"""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.
"""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])
Expand All @@ -19,22 +21,22 @@ def average_of_squares(list_of_numbers, list_of_weights=None):

"""
if list_of_weights is not None:
assert len(list_of_weights) == len(list_of_numbers), \
"weights and numbers must have same length"
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)
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 "])
Expand All @@ -51,12 +53,16 @@ def convert_numbers(list_of_strings):


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

numbers = convert_numbers(numbers_strings)
parser = argparse.ArgumentParser(description="sequence numbers")
parser.add_argument("numbers_strings", type=str, nargs="+")
args = parser.parse_args()
numbers_strings = None
# ["1","2","4"]
weight_strings = ["1", "1", "1"]

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

result = average_of_squares(numbers, weights)
print(result)

print(result)