Skip to content

Commit d700289

Browse files
authored
Merge pull request #42 from UBC-MDS/dev
Dev v2
2 parents 1a7b534 + 748ca39 commit d700289

13 files changed

+925
-100
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ DumbPy is an alternative version of NumPy, which facilitates scientific computin
1111

1212
DumbPy Functions:
1313

14-
- Helper functions - these functions will test the inputted object, and will catch any input besides a list of numbers and produce suitable error messages. Additionally, there will be a helper function that will take user input on whether to flatten an inputted list of lists into one list.
14+
- `support_functions` - these functions will test the inputted object, and will catch any input besides a list of numbers and produce suitable error messages. Additionally, there will be a helper function that will take user input on whether to flatten an inputted list of lists into one list.
1515

16-
- Mean - the mean function will calculate and return the mean, or the average, of the inputted numerical list.
16+
- `arithmetic_mean` - the mean function will calculate and return the mean, or the average, of the inputted numerical list.
1717

18-
- Standard Deviation - the standard deviation function will calculate and return the standard deviation of the inputted numerical list.
18+
- `std_deviation` - the standard deviation function will calculate and return the standard deviation of the inputted numerical list.
1919

20-
- Median - the median function will calculate and return the median value of the inputted numerical list
20+
- `median` - the median function will calculate and return the median value of the inputted numerical list
2121

2222
As stated above, the NumPy package already exists and provides similar functions. NumPy can be found at the following link: <https://numpy.org/>. DumbPy is an alternative version, which is much simpler and has a narrower focus.
2323

@@ -35,13 +35,12 @@ You can install this package into your preferred Python environment using pip:
3535
$ pip install dumbpy
3636
```
3737

38-
TODO: Add a brief example of how to use the package to this section
39-
4038
To use dumbpy in your code:
4139

4240
``` python
4341
>>> import dumbpy
44-
>>> dumbpy.hello_world()
42+
>>> dumbpy.arithmetic_mean([1,2,3])
43+
2.0
4544
```
4645

4746
## Copyright

environment.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: dumbpy-env
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python
6+
- hatch
7+
- copier
8+
- pytest
9+
- pip

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ keywords = []
3838
dependencies = []
3939

4040
[project.urls]
41-
Homepage = "https://github.com/hpalafoxp/dumbpy"
42-
"Source Code" = "https://github.com/hpalafoxp/dumbpy"
43-
"Bug Tracker" = "https://github.com/hpalafoxp/dumbpy/issues"
44-
Documentation = "https://github.com/hpalafoxp/dumbpy/blob/main/README.md"
41+
Homepage = "https://github.com/UBC-MDS/dumbpy/dumbpy"
42+
"Source Code" = "https://github.com/UBC-MDS/dumbpy/dumbpy"
43+
"Bug Tracker" = "https://github.com/UBC-MDS/dumbpy/dumbpy/issues"
44+
Documentation = "https://github.com/UBC-MDS/dumbpy/dumbpy/blob/main/README.md"
4545
Download = "https://pypi.org/project/dumbpy/#files"
4646

4747
[project.optional-dependencies]

src/dumbpy/__init__.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,36 @@
2222
# SOFTWARE.
2323

2424
"""
25-
Add a docstring here for the init module.
25+
dumbpy
26+
======
2627
27-
This might include a very brief description of the package,
28-
its purpose, and any important notes.
28+
A small statistics-oriented package providing simple utilities for working with
29+
nested iterables of numeric values.
30+
31+
Public API
32+
----------
33+
mean
34+
Compute the arithmetic mean of numeric values.
35+
stddev
36+
Compute the population standard deviation of numeric values.
37+
median
38+
Compute the median of numeric values.
39+
flatten
40+
Flatten a nested iterable into a one-dimensional list.
41+
validate
42+
Flatten input and ensure all elements are numeric.
2943
"""
44+
45+
from dumbpy.arithmetic_mean import arithmetic_mean
46+
from dumbpy.median import median
47+
from dumbpy.std_deviation import std_deviation
48+
from dumbpy.support_functions import flatten_list
49+
from dumbpy.support_functions import validate_list
50+
51+
__all__: list[str] = [
52+
"arithmetic_mean",
53+
"std_deviation",
54+
"median",
55+
"flatten_list",
56+
"validate_list"
57+
]

src/dumbpy/arithmetic_mean.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,56 @@
11
"""
2-
A module that calculates the arithmetic mean of a list of numbers
2+
dumbpy.arithmetic_mean
3+
======================
34
5+
Compute the arithmetic mean of numeric values.
6+
7+
The input may be a nested iterable; nesting is flattened via
8+
:func:`dumbpy.support_functions.validate_list`.
49
"""
510

6-
from support_functions import *
11+
from collections.abc import Iterable
12+
from typing import Any
13+
14+
from dumbpy.support_functions import Numeric, validate_list
715

8-
def arithmetic_mean(values: list[float | int]):
16+
17+
def arithmetic_mean(values: Iterable[Any]) -> float:
918
"""
10-
Calculate the arithmetich mean of the inputted list and return the result.
19+
Compute the arithmetic mean of numeric values.
1120
1221
Parameters
1322
----------
14-
values : list[float | int]
15-
A list of numbers.
23+
values : Iterable[Any]
24+
A (possibly nested) iterable containing numeric values.
1625
1726
Returns
1827
-------
1928
float
20-
The arithmetic mean of the list of numbers.
29+
The arithmetic mean of the numeric values.
30+
31+
Raises
32+
------
33+
ValueError
34+
If the flattened input contains no elements.
35+
TypeError
36+
If the input is not a valid iterable for flattening, or if any element
37+
is non-numeric (raised by :func:`validate_list`).
2138
2239
Examples
2340
--------
2441
>>> arithmetic_mean([1, 2, 3, 4])
2542
2.5
43+
>>> arithmetic_mean([1, [2, 3]])
44+
2.0
2645
"""
27-
pass
46+
numbers: list[Numeric] = validate_list(values)
47+
n_elements: int = len(numbers)
48+
49+
if n_elements == 0:
50+
raise ValueError("The number list needs to have at least one numeric element")
51+
52+
total: float | int = 0
53+
for x in numbers:
54+
total += x
55+
56+
return total / n_elements

src/dumbpy/median.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
"""
2-
A module that calculates the median of a list of numbers.
2+
dumbpy.median
3+
=============
4+
5+
Compute the median of numeric values.
6+
7+
The input may be a nested iterable; nesting is flattened via
8+
:func:`dumbpy.support_functions.validate_list`.
39
"""
410

11+
from collections.abc import Iterable
12+
from typing import Any
13+
14+
from dumbpy.support_functions import Numeric, validate_list
515

6-
def median(input):
16+
17+
def median(values: Iterable[Any]) -> Numeric | float:
718
"""
8-
Calculate the median of the inputted list and return the result.
19+
Compute the median of numeric values.
920
1021
Parameters
1122
----------
12-
input : list
13-
A non-empty list of numbers.
23+
values : Iterable[Any]
24+
A (possibly nested) iterable containing numeric values.
1425
1526
Returns
1627
-------
17-
float
18-
The median value of the list. If the list length is even, the median is
19-
the average of the two middle values.
28+
Numeric | float
29+
The median value of the input after sorting. For odd-length inputs, the
30+
middle element is returned (so the type may be ``int``, ``float``, or
31+
``bool``). For even-length inputs, the average of the two middle values
32+
is returned, which is typically a ``float`` (but can be an ``int``/``bool``
33+
when the middle values are equal in this implementation).
2034
2135
Raises
2236
------
23-
TypeError
24-
If x is not a list or contains non-numeric values.
2537
ValueError
26-
If x is an empty list.
38+
If the flattened input contains no elements.
39+
TypeError
40+
If the input is not a valid iterable for flattening, or if any element
41+
is non-numeric (raised by :func:`validate_list`).
2742
2843
Examples
2944
--------
@@ -32,4 +47,19 @@ def median(input):
3247
>>> median([1, 2, 3, 4])
3348
2.5
3449
"""
35-
pass
50+
numbers: list[Numeric] = validate_list(values)
51+
n_elements: int = len(numbers)
52+
53+
if n_elements == 0:
54+
raise ValueError("The number list needs to have at least one numeric element")
55+
56+
numbers_sorted: list[Numeric] = sorted(numbers)
57+
mid: int = n_elements // 2
58+
59+
if n_elements % 2 == 1:
60+
return numbers_sorted[mid]
61+
62+
if numbers_sorted[mid - 1] == numbers_sorted[mid]:
63+
return numbers_sorted[mid - 1]
64+
65+
return (numbers_sorted[mid - 1] + numbers_sorted[mid]) / 2

src/dumbpy/std_deviation.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
"""
2-
A module that calculates the standard deviation of a list of numbers.
2+
dumbpy.std_deviation
3+
====================
34
5+
Compute the population standard deviation of numeric values.
6+
7+
The input may be a nested iterable; nesting is flattened via
8+
:func:`dumbpy.support_functions.validate_list`.
49
"""
510

6-
from support_functions import *
11+
from collections.abc import Iterable
12+
from typing import Any
13+
14+
from dumbpy.arithmetic_mean import arithmetic_mean
15+
from dumbpy.support_functions import Numeric, validate_list
16+
717

8-
def std_deviation(values: list[float | int]):
18+
def std_deviation(values: Iterable[Any]) -> float:
919
"""
10-
Calculate the standard deviation of the inputted list and return the result.
20+
Compute the population standard deviation of numeric values.
21+
22+
This uses the population variance definition:
23+
24+
``variance = sum((x - mean)**2) / n``
1125
1226
Parameters
1327
----------
14-
values : list[float | int]
15-
A list of numbers.
28+
values : Iterable[Any]
29+
A (possibly nested) iterable containing numeric values.
1630
1731
Returns
1832
-------
1933
float
20-
The standard deviation of the list of numbers.
34+
The population standard deviation of the numeric values.
35+
36+
Raises
37+
------
38+
ValueError
39+
If the flattened input contains no elements.
40+
TypeError
41+
If the input is not a valid iterable for flattening, or if any element
42+
is non-numeric (raised by :func:`validate_list`).
2143
2244
Examples
2345
--------
2446
>>> std_deviation([1, 1, 1, 1])
25-
0
47+
0.0
48+
>>> std_deviation([1, 2, 3, 4])
49+
1.118033988749895
2650
"""
27-
pass
28-
51+
numbers: list[Numeric] = validate_list(values)
52+
53+
if len(numbers) == 0:
54+
raise ValueError("The number list needs to have at least one numeric element")
55+
56+
n: int = len(numbers)
57+
mean: float = arithmetic_mean(numbers)
58+
59+
variance: float = sum((x - mean) ** 2 for x in numbers) / n
60+
return variance**0.5

0 commit comments

Comments
 (0)