Skip to content

Commit b4c5e03

Browse files
committed
More flexible naming in format conversion routines
1 parent 2e5e52a commit b4c5e03

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

mathics/core/formatter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def add_conversion_fn(cls, module_fn_name=None) -> None:
4545
# unless it is already set.
4646
if module_fn_name is None:
4747
module_fn_name = cls.__name__.lower()
48+
elif hasattr(module_fn_name, "__name__"):
49+
module_fn_name = module_fn_name.__name__
50+
4851

4952
# Finally register the mapping: (Builtin-class, conversion name) -> conversion_function.
5053
format2fn[(conversion_type, cls)] = module_dict[module_fn_name]

mathics/formatter/asy.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def apply(self, asy):
6868
return self._template % (" * ".join(self.transforms), asy)
6969

7070

71-
def arrowbox(self) -> str:
71+
def arrow_box(self) -> str:
7272
width = self.style.get_line_width(face_element=False)
7373
pen = asy_create_pens(edge_color=self.edge_color, stroke_width=width)
7474
polyline = self.curve.make_draw_asy(pen)
@@ -86,10 +86,10 @@ def polygon(points):
8686
return "".join(self._draw(polyline, default_arrow, custom_arrow, extent))
8787

8888

89-
add_conversion_fn(ArrowBox)
89+
add_conversion_fn(ArrowBox, arrow_box)
9090

9191

92-
def beziercurvebox(self) -> str:
92+
def bezier_curve_box(self) -> str:
9393
line_width = self.style.get_line_width(face_element=False)
9494
pen = asy_create_pens(edge_color=self.edge_color, stroke_width=line_width)
9595

@@ -105,11 +105,10 @@ def beziercurvebox(self) -> str:
105105

106106
x, y, rx, ry, sx, sy, ex, ey, large_arc = self._arc_params()
107107

108+
add_conversion_fn(BezierCurveBox, bezier_curve_box)
108109

109-
add_conversion_fn(BezierCurveBox)
110110

111-
112-
def filledcurvebox(self) -> str:
111+
def filled_curve_box(self) -> str:
113112
line_width = self.style.get_line_width(face_element=False)
114113
pen = asy_create_pens(edge_color=self.edge_color, stroke_width=line_width)
115114

@@ -124,10 +123,10 @@ def components():
124123
return "".join(components())
125124

126125

127-
add_conversion_fn(FilledCurveBox)
126+
add_conversion_fn(FilledCurveBox, filled_curve_box)
128127

129128

130-
def graphicselements(self) -> str:
129+
def graphics_elements(self) -> str:
131130
result = []
132131
for element in self.elements:
133132
format_fn = lookup_method(element, "asy")
@@ -139,8 +138,8 @@ def graphicselements(self) -> str:
139138
return "\n".join(result)
140139

141140

142-
add_conversion_fn(GraphicsElements)
143-
graphics3delements = graphicselements
141+
add_conversion_fn(GraphicsElements, graphics_elements)
142+
graphics3delements = graphics_elements
144143

145144

146145
add_conversion_fn(Graphics3DElements)

mathics/formatter/svg.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def create_css(
7676
return "; ".join(css)
7777

7878

79-
def arrowbox(self, offset=None):
79+
def arrow_box(self, offset=None):
8080
width = self.style.get_line_width(face_element=False)
8181
style = create_css(edge_color=self.edge_color, stroke_width=width)
8282
polyline = self.curve.make_draw_svg(style)
@@ -94,7 +94,7 @@ def polygon(points):
9494
return "".join(self._draw(polyline, default_arrow, custom_arrow, extent))
9595

9696

97-
add_conversion_fn(ArrowBox)
97+
add_conversion_fn(ArrowBox, arrow_box)
9898

9999

100100
def beziercurvebox(self, offset=None):
@@ -112,7 +112,7 @@ def beziercurvebox(self, offset=None):
112112
add_conversion_fn(BezierCurveBox)
113113

114114

115-
def filledcurvebox(self, offset=None):
115+
def filled_curve_box(self, offset=None):
116116
line_width = self.style.get_line_width(face_element=False)
117117
style = create_css(
118118
edge_color=self.edge_color, face_color=self.face_color, stroke_width=line_width
@@ -130,9 +130,9 @@ def components():
130130
)
131131

132132

133-
add_conversion_fn(FilledCurveBox)
133+
add_conversion_fn(FilledCurveBox, filled_curve_box)
134134

135-
def graphicsbox(self, leaves=None, **options) -> str:
135+
def graphics_box(self, leaves=None, **options) -> str:
136136
if not leaves:
137137
leaves = self._leaves
138138

@@ -185,10 +185,10 @@ def graphicsbox(self, leaves=None, **options) -> str:
185185
return svg_main # , width, height
186186

187187

188-
add_conversion_fn(GraphicsBox)
188+
add_conversion_fn(GraphicsBox, graphics_box)
189189

190190

191-
def graphicselements(self, offset=None):
191+
def graphics_elements(self, offset=None):
192192
result = []
193193
for element in self.elements:
194194
format_fn = lookup_method(element, "svg")
@@ -200,13 +200,13 @@ def graphicselements(self, offset=None):
200200
return "\n".join(result)
201201

202202

203-
add_conversion_fn(GraphicsElements)
204-
graphics3delements = graphicselements
203+
add_conversion_fn(GraphicsElements, graphics_elements)
204+
graphics3delements = graphics_elements
205205

206206
add_conversion_fn(Graphics3DElements)
207207

208208

209-
def insetbox(self, offset=None):
209+
def inset_box(self, offset=None):
210210
x, y = self.pos.pos()
211211
if offset:
212212
x = x + offset[0]
@@ -237,10 +237,10 @@ def insetbox(self, offset=None):
237237
return svg
238238

239239

240-
add_conversion_fn(InsetBox)
240+
add_conversion_fn(InsetBox, inset_box)
241241

242242

243-
def linebox(self, offset=None):
243+
def line_box(self, offset=None):
244244
line_width = self.style.get_line_width(face_element=False)
245245
style = create_css(edge_color=self.edge_color, stroke_width=line_width)
246246
svg = ""
@@ -253,7 +253,7 @@ def linebox(self, offset=None):
253253
return svg
254254

255255

256-
add_conversion_fn(LineBox)
256+
add_conversion_fn(LineBox, line_box)
257257

258258

259259
def pointbox(self, offset=None):

0 commit comments

Comments
 (0)