add logging and main function to fancymath

This commit is contained in:
2025-10-23 11:25:35 -05:00
parent a40a2ff442
commit bac8d74803
2 changed files with 43 additions and 1 deletions

36
src/fancymath/__main__.py Normal file
View File

@@ -0,0 +1,36 @@
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=}")
v4 = v2.dot([1,2,3])

View File

@@ -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__):
logger.error(f"Expected an instance of {self.__class__}, but got {type(v)} instead.")
return None
d = self.x * v.x + self.y * v.y + self.z * v.z
return d