mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
558 stars 142 forks source link

Make Path Objects Hashable #154

Closed matmill5 closed 3 years ago

matmill5 commented 3 years ago

It would be nice if Path objects were hashable.

Something like this:

Hashable Path

def __hash__(self):
    return hash((self._start, self._length, self._lengths, self._segments, self._end, self.meta, self._closed))

# Comparable
def __cmp__(self):
    # TODO
mathandy commented 3 years ago

Yeah, agreed.
Path segments (Line, QuadraticBezier, CubicBezier, and Arc) are very hashable.
E.g.

def __hash__(self):
    hash(self.bpoints())

would work for bezier path segments (and __hash__ is already implemented similarly for Arc objects).

The case of Path itself is a bit more complicated if you want to include style or transform information that's not included in the Path.element attribute (this can I think be the case when the path was part of an SVG-Group that was transformed). That said, I'd be open to pushing the solution:

def __hash__(self):
    return hash((tuple(self._segments), self._closed))

I think the rest of the attributes included in your rough solution above are actually redundant (except Path.meta which is too general-purpose to be hashable perhaps).

Would this work for your use case?

mathandy commented 3 years ago

@matmill5 I pushed this solution to the hashable-paths branch.

If it doesn't work for your use case, then please let me know and we can adjust it before I merge it in.

matmill5 commented 3 years ago

@mathandy Thank you, I appreciate it, this looks like it will work for my use case.

matmill5 commented 3 years ago

Issue resolved - closing ticket.