Add __repr__ methods to most objects`

These are closer to what __str__ should be and will likely change in the
future.
This commit is contained in:
Jan Petykiewicz 2020-05-11 20:31:07 -07:00
parent c79eee2a8c
commit c236fdb81b
10 changed files with 64 additions and 0 deletions

View File

@ -170,3 +170,7 @@ class Label:
object.__setattr__(self, 'locked', False) object.__setattr__(self, 'locked', False)
self.offset.flags.writeable = True self.offset.flags.writeable = True
return self return self
def __repr__(self) -> str:
locked = ' L' if self.locked else ''
return f'<Label "{self.string}" l{self.layer} o{self.offset}{locked}>'

View File

@ -876,3 +876,7 @@ class Pattern:
toplevel = list(patterns - not_toplevel) toplevel = list(patterns - not_toplevel)
return toplevel return toplevel
def __repr__(self) -> str:
locked = ' L' if self.locked else ''
return (f'<Pattern "{self.name}": sh{len(self.shapes)} sp{len(self.subpatterns)} la{len(self.labels)}{locked}>')

View File

@ -531,3 +531,14 @@ class GridRepetition:
self.unlock() self.unlock()
self.pattern.deepunlock() self.pattern.deepunlock()
return self return self
def __repr__(self) -> str:
name = self.pattern.name if self.pattern is not None else None
rotation = f' r{self.rotation*180/pi:g}' if self.rotation != 0 else ''
scale = f' d{self.scale:g}' if self.scale != 1 else ''
mirrored = ' m{:d}{:d}'.format(*self.mirrored) if self.mirrored.any() else ''
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
bv = f', {self.b_vector}' if b_vector is not None else ''
return (f'<GridRepetition "{name}" at {self.offset} {rotation}{scale}{mirrored}{dose}'
f' {self.a_count}x{self.b_count} ({self.a_vector}{bv}){locked}>')

View File

@ -398,3 +398,10 @@ class Arc(Shape):
self.radii.flags.writeable = True self.radii.flags.writeable = True
self.angles.flags.writeable = True self.angles.flags.writeable = True
return self return self
def __repr__(self) -> str:
angles = f'{self.angles*180/pi}'
rotation = f'{self.rotation*180/pi:g}' if self.rotation != 0 else ''
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<Arc l{self.layer} o{self.offset} r{self.radii}{angles} w{self.width:g}{rotation}{dose}{locked}>'

View File

@ -110,3 +110,7 @@ class Circle(Shape):
(self.offset, magnitude, rotation, False, self.dose), \ (self.offset, magnitude, rotation, False, self.dose), \
lambda: Circle(radius=norm_value, layer=self.layer) lambda: Circle(radius=norm_value, layer=self.layer)
def __repr__(self) -> str:
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<Circle l{self.layer} o{self.offset} r{self.radius:g}{dose}{locked}>'

View File

@ -189,3 +189,9 @@ class Ellipse(Shape):
Shape.unlock(self) Shape.unlock(self)
self.radii.flags.writeable = True self.radii.flags.writeable = True
return self return self
def __repr__(self) -> str:
rotation = f' r{self.rotation*180/pi:g}' if self.rotation != 0 else ''
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<Ellipse l{self.layer} o{self.offset} r{self.radii}{rotation}{dose}{locked}>'

View File

@ -407,3 +407,9 @@ class Path(Shape):
if self.cap_extensions is not None: if self.cap_extensions is not None:
self.cap_extensions.flags.writeable = True self.cap_extensions.flags.writeable = True
return self return self
def __repr__(self) -> str:
centroid = self.offset + self.vertices.mean(axis=0)
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<Path l{self.layer} centroid {centroid} v{len(self.vertices)} w{self.width} c{self.cap}{dose}{locked}>'

View File

@ -339,3 +339,9 @@ class Polygon(Shape):
Shape.unlock(self) Shape.unlock(self)
self.vertices.flags.writeable = True self.vertices.flags.writeable = True
return self return self
def __repr__(self) -> str:
centroid = self.offset + self.vertices.mean(axis=0)
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<Polygon l{self.layer} centroid {centroid} v{len(self.vertices)}{dose}{locked}>'

View File

@ -253,3 +253,10 @@ def get_char_as_polygons(font_path: str,
Shape.unlock(self) Shape.unlock(self)
self.mirrored.flags.writeable = True self.mirrored.flags.writeable = True
return self return self
def __repr__(self) -> str:
rotation = f'{self.rotation*180/pi:g}' if self.rotation != 0 else ''
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
mirrored = ' m{:d}{:d}'.format(*self.mirrored) if self.mirrored.any() else ''
return f'<TextShape "{self.string}" l{self.layer} o{self.offset} h{self.height:g}{rotation}{mirrored}{dose}{locked}>'

View File

@ -339,3 +339,12 @@ class SubPattern:
self.unlock() self.unlock()
self.pattern.deepunlock() self.pattern.deepunlock()
return self return self
def __repr__(self) -> str:
name = self.pattern.name if self.pattern is not None else None
rotation = f' r{self.rotation*180/pi:g}' if self.rotation != 0 else ''
scale = f' d{self.scale:g}' if self.scale != 1 else ''
mirrored = ' m{:d}{:d}'.format(*self.mirrored) if self.mirrored.any() else ''
dose = f' d{self.dose:g}' if self.dose != 1 else ''
locked = ' L' if self.locked else ''
return f'<SubPattern "{name}" at {self.offset}{rotation}{scale}{mirrored}{dose}{locked}>'