Compare commits
28 Commits
e03dec68d3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b69fe25b4 | |||
| 97d4d9c9a7 | |||
| e3ffd2210b | |||
| aa7dc496dc | |||
|
|
66123f6064 | ||
| 913169fe68 | |||
| 5850743394 | |||
| 79b6c10b70 | |||
| 491c531ed8 | |||
|
|
437be17f51 | ||
| 64f06ce7c1 | |||
| 4d5e2db71a | |||
| b613241807 | |||
|
|
13ac74132c | ||
| 35a1705570 | |||
| 352480ac14 | |||
| 6ba84c9859 | |||
| 6d98c08e89 | |||
| 56c892d944 | |||
| 922ed4d394 | |||
| a3e89d3460 | |||
| b90d601490 | |||
| 6ae7933baa | |||
| 4198198e02 | |||
| bac8d74803 | |||
| a40a2ff442 | |||
| 830eda6e1c | |||
| 68d6df3a4f |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@
|
||||
*.egg-info
|
||||
doc/_build/
|
||||
*.ipynb_checkpoints/
|
||||
fancymath.log
|
||||
25
README.md
25
README.md
@@ -28,5 +28,28 @@ $ pip install -e .
|
||||
|
||||
# Day 3 Resources
|
||||
- *Python Typing is for Humans*: [Unital.dev blog](https://www.unital.dev/blog/python-typing-is-for-humans/) or [LinkedIn](https://www.linkedin.com/pulse/python-typing-humans-corran-webster-u66ee)
|
||||
- [*Domain Driven Design*](https://www.oreilly.com/library/view/domain-driven-design-tackling/0321125215/) (O'Reilly)
|
||||
- [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/)
|
||||
- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/)
|
||||
- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/)
|
||||
- Find the refactoring Give It A Try in `files/convert_mass.py`
|
||||
|
||||
# Day 4 Resourecs
|
||||
- The [Day 4 Notebook](files/Day4.ipynb) is in `files/Day4.ipynb`.
|
||||
- [Arrays and Lists](https://blog.dillerdigital.com/arrays-and-lists/) from the Diller Digital Blog
|
||||
- [Cython](https://cython.org/) - tool for building C extensions in Python. Particularly good for seamlessly handing off Numpy `ndarray`s to C-array code. Tim and Corran's former coworker Kurt Smith literally [wrote the book](https://www.oreilly.com/library/view/cython/9781491901731/) on Cython.
|
||||
- [`ctypes`](https://docs.python.org/3/library/ctypes.html) - a lower level tool for extending Python with C.
|
||||
- [Tim's favorite logging setup](https://github.com/timdiller/favorite_logging_setup) on Github.
|
||||
- [`line_profiler`](https://kernprof.readthedocs.io/en/latest/#line-profiler-basic-usage) written by Tim and Corran's former coworker Robert Kern (hence the name `kernprof` for the command line tool). Summary of usage:
|
||||
```
|
||||
> pip install line_profiler
|
||||
# add `from line_profiler import profile` to module to be profiled
|
||||
# decorate functions to be profiled with `@profile`
|
||||
> kernprof -l module_to_profile.py
|
||||
Wrote profile results to 'module_to_profile.py.lprof'
|
||||
Inspect results with:
|
||||
python -m line_profiler -rmt module_to_profile.py.lprof
|
||||
> python -m line_profiler -rmt module_to_profile.py.lprof
|
||||
```
|
||||
|
||||
# Day 5 Resources
|
||||
- [5 Worlds](https://www.joelonsoftware.com/2002/05/06/five-worlds/) from Joel Spolsky's blog.
|
||||
|
||||
@@ -17,7 +17,13 @@ release = '0.0.1'
|
||||
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
|
||||
|
||||
templates_path = ['_templates']
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.ipynb_checkpoints']
|
||||
exclude_patterns = [
|
||||
'_build',
|
||||
'Thumbs.db',
|
||||
'.DS_Store',
|
||||
'.ipynb_checkpoints',
|
||||
'api/.ipynb_checkpoints'
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
2001
files/Day4.ipynb
Normal file
2001
files/Day4.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
27
files/convert_mass.py
Normal file
27
files/convert_mass.py
Normal file
@@ -0,0 +1,27 @@
|
||||
''' Refactor the following code
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4]
|
||||
mass_g = [mass * 28.3496 for mass in mass_oz]
|
||||
mass_kg = [mass * 28.3496 / 1000 for mass in mass_oz]
|
||||
'''
|
||||
|
||||
# Refactor Step 1
|
||||
G_OZ = 28.3496
|
||||
KG_OZ = G_OZ / 1000
|
||||
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4]
|
||||
mass_g = [mass * G_OZ for mass in mass_oz]
|
||||
mass_kg = [mass * KG_OZ for mass in mass_oz]
|
||||
|
||||
# Refactor Step 2
|
||||
def convert_mass(mass_oz, conv_factor):
|
||||
return [m * conv_factor for m in mass_oz]
|
||||
|
||||
# Refactor Step 3
|
||||
if __name__ == "__main__":
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4, 5.6, 7.8, 9.1]
|
||||
print("Ounces")
|
||||
print(", ".join(str(m) for m in mass_oz))
|
||||
print("Grams")
|
||||
print(", ".join(str(m) for m in convert_mass(mass_oz, G_OZ)))
|
||||
print("Kilograms")
|
||||
print(", ".join(str(m) for m in convert_mass(mass_oz, KG_OZ)))
|
||||
39
src/fancymath/__main__.py
Normal file
39
src/fancymath/__main__.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
|
||||
fmt = "%(asctime)s %(levelname)-8.8s [%(name)s:%(lineno)s] %(message)s"
|
||||
formatter = logging.Formatter(fmt)
|
||||
|
||||
sh = logging.StreamHandler()
|
||||
sh.setFormatter(formatter)
|
||||
sh.setLevel(logging.INFO)
|
||||
|
||||
fh = logging.FileHandler("fancymath.log")
|
||||
fh.setFormatter(formatter)
|
||||
fh.setLevel(logging.NOTSET)
|
||||
|
||||
root_logger.addHandler(sh)
|
||||
root_logger.addHandler(fh)
|
||||
|
||||
root_logger.setLevel(logging.NOTSET)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
logger.info("Welcome to FancyMath, a silly library for demonstrations.")
|
||||
|
||||
from fancymath.vector import Vector
|
||||
|
||||
v1 = Vector(1,2,3)
|
||||
v2 = Vector(2,3,4)
|
||||
|
||||
v3 = v1.dot(v2)
|
||||
|
||||
logger.info(f"{v1=}")
|
||||
logger.info(f"{v2=}")
|
||||
logger.info(f"{v3=}")
|
||||
|
||||
try:
|
||||
v4 = v2.dot([1,2,3])
|
||||
except ValueError as err:
|
||||
logger.exception(err)
|
||||
@@ -12,4 +12,19 @@ def fact(n: int) -> int:
|
||||
if n == 1:
|
||||
return 1
|
||||
else:
|
||||
return n * fact(n - 1)
|
||||
return n * fact(n - 1)
|
||||
|
||||
|
||||
def is_even(x):
|
||||
return x % 2 == 0
|
||||
|
||||
|
||||
def is_odd(x):
|
||||
return x % 2 == 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
n = 5
|
||||
assert fact(n) == 120
|
||||
assert add(5, 6) == 11
|
||||
print("Tests passed")
|
||||
@@ -1,8 +1,11 @@
|
||||
"""This module defines the Vector class. """
|
||||
|
||||
import logging
|
||||
from math import acos, sqrt
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Vector(object):
|
||||
def __init__(self, x, y, z):
|
||||
"""Constructor method."""
|
||||
@@ -12,6 +15,9 @@ class Vector(object):
|
||||
|
||||
def dot(self, v):
|
||||
"""Returns the dot product with Vector *v*."""
|
||||
if not isinstance(v, self.__class__):
|
||||
msg = f"Expected an instance of {self.__class__}, but got {type(v)} instead."
|
||||
raise ValueError(msg)
|
||||
d = self.x * v.x + self.y * v.y + self.z * v.z
|
||||
return d
|
||||
|
||||
@@ -24,9 +30,26 @@ class Vector(object):
|
||||
s = "Vector(x=%s, y=%s, z=%s)" % (self.x, self.y, self.z)
|
||||
return s
|
||||
|
||||
def __add__(self, other):
|
||||
x = self.x + other.x
|
||||
y = self.y + other.y
|
||||
z = self.z + other.z
|
||||
return self.__class__(x, y, z)
|
||||
|
||||
def __mul__(self, other):
|
||||
if not isinstance(other, Vector):
|
||||
return NotImplemented
|
||||
x = self.y * other.z - self.z * other.y
|
||||
y = self.z * other.x - self.x * other.z
|
||||
z = self.x * other.y - self.y * other.x
|
||||
return self.__class__(x, y, z)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
v1 = Vector(2.0, 13.0, -1.0)
|
||||
v1 = Vector(3, 4, 0)
|
||||
print(v1, " magnitude is", v1.abs())
|
||||
v2 = Vector(1.0, 2.0, 3.0)
|
||||
print("v1.dot(v2) =", v1.dot(v2))
|
||||
|
||||
assert v1.abs() == 5
|
||||
print("Tests passed!")
|
||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
25
test/test_math.py
Normal file
25
test/test_math.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
|
||||
from fancymath.math import is_even, is_odd
|
||||
|
||||
|
||||
class TestMath(unittest.TestCase):
|
||||
def test_is_even(self):
|
||||
"Test the is_even function"
|
||||
self.assertTrue(is_even(2))
|
||||
self.assertTrue(is_even(-4))
|
||||
self.assertTrue(is_even(0))
|
||||
self.assertFalse(is_even(1))
|
||||
self.assertFalse(is_even(2.5))
|
||||
|
||||
def test_is_odd(self):
|
||||
"Test the is_odd function"
|
||||
self.assertTrue(is_odd(3))
|
||||
self.assertTrue(is_odd(-5))
|
||||
self.assertFalse(is_odd(0))
|
||||
self.assertFalse(is_odd(2))
|
||||
self.assertFalse(is_odd(2.5))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
0
test/test_util.py
Normal file
0
test/test_util.py
Normal file
59
test/test_vector.py
Normal file
59
test/test_vector.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import unittest
|
||||
|
||||
from fancymath.vector import Vector
|
||||
|
||||
|
||||
class TestVector(unittest.TestCase):
|
||||
def test_cross_product(self):
|
||||
# Dictionary of (v1, v2): v3
|
||||
values = {
|
||||
(Vector(1, 2, 3), Vector(3, 2, 1)): Vector(-4, 8, -4),
|
||||
(Vector(0, 0, 0), Vector(1, 2, 3)): Vector(0, 0, 0),
|
||||
(Vector(1, 0, 0), Vector(1, 0, 0)): Vector(0, 0, 0),
|
||||
}
|
||||
|
||||
for inputs, expected in values.items():
|
||||
v1, v2 = inputs
|
||||
with self.subTest(v1=v1, v2=v2):
|
||||
v3 = v1 * v2
|
||||
|
||||
self.assertIsInstance(v3, Vector)
|
||||
self.assertEqual(v3.x, expected.x)
|
||||
self.assertEqual(v3.y, expected.y)
|
||||
self.assertEqual(v3.z, expected.z)
|
||||
|
||||
def test_cross_product_failure(self):
|
||||
v1 = Vector(1, 2, 3)
|
||||
v2 = (3, 2, 1)
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
v1 * v2
|
||||
|
||||
|
||||
def test_add_vector(self):
|
||||
v1 = Vector(1, 2, 3)
|
||||
v2 = Vector(-3, -2, -1)
|
||||
v3 = v1 + v2
|
||||
self.assertIsInstance(v3, Vector)
|
||||
self.assertEqual(v3.x, v1.x + v2.x)
|
||||
|
||||
def test_abs(self):
|
||||
v1 = Vector(0, 0, 0)
|
||||
v2 = Vector(1, 2, 2)
|
||||
v3 = Vector(1, -2, 2)
|
||||
self.assertEqual(v1.abs(), 0)
|
||||
self.assertEqual(v2.abs(), 3)
|
||||
self.assertEqual(v2.abs(), v3.abs())
|
||||
|
||||
def test_dot_failure(self):
|
||||
with self.assertRaises(ValueError):
|
||||
v1 = Vector(1, 2, 3)
|
||||
v1.dot(3)
|
||||
|
||||
def test_dot_product(self):
|
||||
v1 = Vector(1, 2, 3)
|
||||
v2 = Vector(-3, -2, -1)
|
||||
v3 = v1.dot(v2)
|
||||
self.assertEqual(v3,v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)
|
||||
vzero = Vector(0, 0, 0)
|
||||
self.assertEqual(v1.dot(vzero),0)
|
||||
Reference in New Issue
Block a user