8000 Allow Paths to be marked as readonly by mdboom · Pull Request #2010 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Allow Paths to be marked as readonly #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add docstrings
  • Loading branch information
mdboom committed May 15, 2013
commit 4c9a407b59c0c9167c505e88d70a596802105d0f
40 changes: 40 additions & 0 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def _update_values(self):

@property
def vertices(self):
"""
The list of vertices in the `Path` as an Nx2 numpy array.
"""
return self._vertices

@vertices.setter
Expand All @@ -167,6 +170,14 @@ def vertices(self, vertices):

@property
def codes(self):
"""
The list of codes in the `Path` as a 1-D numpy array. Each
code is one of `STOP`, `MOVETO`, `LINETO`, `CURVE3`, `CURVE4`
or `CLOSEPOLY`. For codes that correspond to more than one
vertex (`CURVE3` and `CURVE4`), that code will be repeated so
that the length of `self.vertices` and `self.codes` is always
the same.
"""
return self._codes

@codes.setter
Expand All @@ -178,27 +189,56 @@ def codes(self, codes):

@property
def simplify_threshold(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this property (and some of the others) really need to exist. The original attribute access was perfectly satisfactory - if you changed something that didn't make sense then you would expect it to blow up...
I guess what I'm saying is that I'm not really a big fan of protecting values in this way - if a user wants to fiddle with them, then they should be able to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nice thing about this approach is that when the vertices are updated, these values are updated as well, so they should always be correct. I think that's a good thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good thing.

I agree, but there are always corner cases 😉

path.vertices[10:50] += 10

I'm happy with the read only protection for coding mistakes (such as mine) - but this is python: I don't think we need to worry about protecting the whole state of Path instances.

Either way, it's much of a muchness - and more important than whether it is a property or an actual attribute, I think there should be a oneline docstring for all of these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll leave this as properties -- I think in most cases this is preferred.

Agreed about the docstrings.

I realise we can't protect much, but I think this was a particularly surprising one, mainly because I think users are surprised that Path.unit_circle() is a singleton. I could have also resolved this by having those return a copy, but that breaks some optimizations in markers and elsewhere where it tries to save the same path only once wherever possible.

"""
The fraction of a pixel difference below which vertices will
be simplified out.
"""
return self._simplify_threshold

@simplify_threshold.setter
def simplify_threshold(self, threshold):
self._simplify_threshold = threshold

@property
def has_nonfinite(self):
"""
`True` if the vertices array has nonfinite values.
"""
return self._has_nonfinite

@property
def should_simplify(self):
"""
`True` if the vertices array should be simplified.
"""
return self._should_simplify

@should_simplify.setter
def should_simplify(self, should_simplify):
self._should_simplify = should_simplify

@property
def readonly(self):
"""
`True` if the `Path` is read-only.
"""
return self._readonly

def __copy__(self):
"""
Returns a shallow copy of the `Path`, which will share the
vertices and codes with the source `Path`.
"""
import copy
return copy.copy(self)

copy = __copy__

def __deepcopy__(self):
"""
Returns a deepcopy of the `Path`. The `Path` will not be
readonly, even if the source `Path` is.
"""
return self.__class__(
self.vertices.copy(), self.codes.copy(),
_interpolation_steps=self._interpolation_steps)
Expand Down
0