0

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?

simone
  • 4,667
  • 4
  • 25
  • 47
  • I don't understand why you don't want to subclass Path. It makes much more sense, doesn't it? – Riccardo Bucco Sep 18 '21 at 10:08
  • @RiccardoBucco - it feels wrong to have a PathWithDistance class, where the only added functionality is path-to-path distance. I also come from a traits/roles oriented approach, which I generally find more logical and manageable - see https://en.wikipedia.org/wiki/Trait_(computer_programming) and specifically in perl (as an example) https://metacpan.org/pod/Role::Tiny. Maybe it's a matter of taste - but I'd like to look into the feasibility – simone Sep 18 '21 at 10:27
  • And does having an entire new module just to change the method of a class feel much better? I don't know, maybe you're right and it's a matter of taste, but I personally would never take this direction in Python. – Riccardo Bucco Sep 18 '21 at 10:31
  • Anyway, if you *really* want to add a submodule then you could either have to modify the package you are using (sygpathtools), or you could create a new module from scratch (but again that does feel terribly complicated to just replace a method of a class) – Riccardo Bucco Sep 18 '21 at 10:32
  • @RiccardoBucco - so it's not possible to do ```import svgpathtools.distance```, while keeping (and importing) ```svgpathtools``` in ```/home/simone/.local/lib/python3.8/site-packages```? – simone Sep 18 '21 at 13:41

0 Answers0