diff options
Diffstat (limited to 'lib/stitch_plan/stitch.py')
| -rw-r--r-- | lib/stitch_plan/stitch.py | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 3bfa7075..c1553ce5 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -12,6 +12,10 @@ class Stitch(Point): def __init__(self, x, y=None, color=None, jump=False, stop=False, trim=False, color_change=False, tie_modus=0, force_lock_stitches=False, no_ties=False, tags=None): + # DANGER: if you add new attributes, you MUST also set their default + # values in __new__() below. Otherwise, cached stitch plans can be + # loaded and create objects without those properties defined, because + # unpickling does not call __init__()! base_stitch = None if isinstance(x, Stitch): @@ -42,17 +46,26 @@ class Stitch(Point): if base_stitch is not None: self.add_tags(base_stitch.tags) + def __new__(cls, *args, **kwargs): + instance = super().__new__(cls) + + # Set default values for any new attributes here (see note in __init__() above) + # instance.foo = None + + return instance + def __repr__(self): - return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, - self.y, - self.color, - "JUMP" if self.jump else " ", - "TRIM" if self.trim else " ", - "STOP" if self.stop else " ", - "TIE MODUS" if self.tie_modus else " ", - "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", - "NO TIES" if self.no_ties else " ", - "COLOR CHANGE" if self.color_change else " ") + return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, + self.y, + self.color, + self.tags, + "JUMP" if self.jump else " ", + "TRIM" if self.trim else " ", + "STOP" if self.stop else " ", + self.tie_modus, + "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", + "NO TIES" if self.no_ties else " ", + "COLOR CHANGE" if self.color_change else " ") def _set(self, attribute, value, base_stitch): # Set an attribute. If the caller passed a Stitch object, use its value, unless @@ -92,3 +105,12 @@ class Stitch(Point): attributes = dict(vars(self)) attributes['tags'] = list(attributes['tags']) return attributes + + def __getstate__(self): + # This is used by pickle. We want to sort the tag list so that the + # pickled representation is stable, since it's used to generate cache + # keys. + state = self.__json__() + state['tags'].sort() + + return state |
