I am trying to extend functionality of another existing module (svgpathtools) with a few additional methods. I am aware of this answer - wthis answerith the difference that I am trying to do that via a module - which isn't covered in the answer.
My approach is to create an additional module - called svgpathools.distance
(or something similar) which would add these methods to the relevant classes inside svgpathtools.
It does not make sense IMHO to subclass the classes in svgpathtools - the functionality is limited, and pertains closely to the classes in there. It does however make sense to have this extended functionality in a re-usable module.
My current directory structure looks like this:
─── svgpathtools
├── __init__.py
└── distance.py
where __init__.py
contains:
import sys
print(sys.path)
print('inside init')
and distance.py
contains
import svgpathtools
import numpy
def complex_distance(a, b):
return ((a.real - b.real) ** 2 + (a.imag - b.imag) ** 2) ** (1/2)
print('inside svgpathtools.distance')
def foo(path_a, path_b):
d=0
for t in range(0, 101, 1):
d = d + complex_distance(path_a.point(t / 100), path_b.point(t / 100))
return d / 100
setattr(svgpathtools.Path, 'foo', foo)
If I run it like this:
python3 -c "import svgpathtools.distance"
I get
AttributeError: module 'svgpathtools' has no attribute 'Path'
which I guess is due to __init__.py
overwriting the installed svgpathtools, and if I remove __init__.py
I get
ModuleNotFoundError: No module named 'svgpathtools.distance'
So how should I structure things so that I can add functionality via a reusable module?