Skip to content

Commit

Permalink
fstring with flint (#763)
Browse files Browse the repository at this point in the history
* fstring with flint

* un-needed str() call
  • Loading branch information
benkiel authored Nov 4, 2024
1 parent de73f13 commit 19f9ac6
Show file tree
Hide file tree
Showing 26 changed files with 184 additions and 343 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build/*
dist/*
.eggs/
.vscode
*-venv/

documentation/build/*
documentation/.sass-cache/*
Expand All @@ -22,4 +23,4 @@ doc/*
htmlcov

# scm version
Lib/fontParts/_version.py
Lib/fontParts/_version.py
6 changes: 3 additions & 3 deletions Lib/fontParts/base/anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class BaseAnchor(

def _reprContents(self):
contents = [
("({x}, {y})".format(x=self.x, y=self.y)),
f"({self.x}, {self.y})",
]
if self.name is not None:
contents.append("name='%s'" % self.name)
contents.append(f"name='{self.name}'")
if self.color:
contents.append("color=%r" % str(self.color))
contents.append(f"color={self.color!r}")
return contents

# ----
Expand Down
6 changes: 3 additions & 3 deletions Lib/fontParts/base/bPoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class BaseBPoint(

def _reprContents(self) -> List[str]:
contents = [
"%s" % self.type,
"anchor='({x}, {y})'".format(x=self.anchor[0], y=self.anchor[1]),
f"{self.type}",
f"anchor='({self.anchor[0]}, {self.anchor[1]})'",
]
return contents

Expand Down Expand Up @@ -530,7 +530,7 @@ def _get_type(self) -> str:
bType = "corner"

if bType is None:
raise FontPartsError("A %s point can not be converted to a bPoint." % typ)
raise FontPartsError(f"A {typ} point can not be converted to a bPoint.")
return bType

def _set_type(self, value: str) -> None:
Expand Down
14 changes: 5 additions & 9 deletions Lib/fontParts/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ def __get__(self, obj: Any, cls: Type[Any]) -> Any:
# via the class instead of an instance
if obj is None:
return self
raise FontPartsError("no getter for %r" % self.name)
raise FontPartsError(f"no getter for {self.name!r}")

def __set__(self, obj: Any, value: Any) -> None:
setter = getattr(obj, self.setterName, None)
if setter is not None:
setter(value)
else:
raise FontPartsError("no setter for %r" % self.name)
raise FontPartsError(f"no setter for {self.name!r}")


def interpolate(
Expand Down Expand Up @@ -351,9 +351,7 @@ def raiseNotImplementedError(self) -> NoReturn:
"""
raise NotImplementedError(
"The {className} subclass does not implement this method.".format(
className=self.__class__.__name__
)
f"The {self.__class__.__name__} subclass does not implement this method."
)

# ---------------------
Expand Down Expand Up @@ -1099,9 +1097,7 @@ def isCompatible(self, other: Any, cls: Type[Any]) -> Tuple[bool, Any]:
"""
if not isinstance(other, cls):
raise TypeError(
"""Compatibility between an instance of %r and an \
instance of %r can not be checked."""
% (cls.__name__, other.__class__.__name__)
f"""Compatibility between an instance of {cls.__name__!r} and an instance of {other.__class__.__name__!r} can not be checked."""
)
reporter = self.compatibilityReporterClass(self, other)
self._isCompatible(other, reporter)
Expand Down Expand Up @@ -1397,7 +1393,7 @@ def __init__(self, value: IntFloatType, threshold: IntFloatType) -> None:
self.threshold = threshold

def __repr__(self) -> str:
return "[%f %f]" % (self.value, self.threshold)
return f"[{self.value:f} {self.threshold:f}]"

def __lt__(self, other: Union[FuzzyNumber, IntFloatType]) -> bool:
if hasattr(other, "value"):
Expand Down
98 changes: 17 additions & 81 deletions Lib/fontParts/base/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def __init__(self, obj1, obj2):
warning = False

def _get_title(self):
title = "{object1Name} + {object2Name}".format(
object1Name=self.object1Name, object2Name=self.object2Name
)
title = f"{self.object1Name} + {self.object2Name}"
if self.fatal:
return self.formatFatalString(title)
elif self.warning:
Expand Down Expand Up @@ -53,13 +51,13 @@ def _get_object2Name(self):
@staticmethod
def _getObjectName(obj):
if hasattr(obj, "name") and obj.name is not None:
return '"%s"' % obj.name
return f'"{obj.name}"'
elif hasattr(obj, "identifier") and obj.identifier is not None:
return '"%s"' % obj.identifier
return f'"{obj.identifier}"'
elif hasattr(obj, "index"):
return "[%s]" % obj.index
return f"[{obj.index}]"
else:
return "<%s>" % id(obj)
return f"<{id(obj)}>"

# Report

Expand All @@ -70,13 +68,13 @@ def report(self, showOK=False, showWarnings=False):
raise NotImplementedError

def formatFatalString(self, text):
return "[Fatal] {objectName}: ".format(objectName=self.objectName) + text
return f"[Fatal] {self.objectName}: " + text

def formatWarningString(self, text):
return "[Warning] {objectName}: ".format(objectName=self.objectName) + text
return f"[Warning] {self.objectName}: " + text

def formatOKString(self, text):
return "[OK] {objectName}: ".format(objectName=self.objectName) + text
return f"[OK] {self.objectName}: " + text

@staticmethod
def reportSubObjects(reporters, showOK=True, showWarnings=True):
Expand All @@ -90,44 +88,20 @@ def reportSubObjects(reporters, showOK=True, showWarnings=True):
def reportCountDifference(
subObjectName, object1Name, object1Count, object2Name, object2Count
):
text = (
"{object1Name} contains {object1Count} {subObjectName} | "
"{object2Name} contains {object2Count} {subObjectName}"
).format(
subObjectName=subObjectName,
object1Name=object1Name,
object1Count=object1Count,
object2Name=object2Name,
object2Count=object2Count,
)
text = f"{object1Name} contains {object1Count} {subObjectName} | {object2Name} contains {object2Count} {subObjectName}"
return text

@staticmethod
def reportOrderDifference(
subObjectName, object1Name, object1Order, object2Name, object2Order
):
text = (
"{object1Name} has {subObjectName} ordered {object1Order} | "
"{object2Name} has {object2Order}"
).format(
subObjectName=subObjectName,
object1Name=object1Name,
object1Order=object1Order,
object2Name=object2Name,
object2Order=object2Order,
)
text = f"{object1Name} has {subObjectName} ordered {object1Order} | {object2Name} has {object2Order}"
return text

@staticmethod
def reportDifferences(object1Name, subObjectName, subObjectID, object2Name):
text = (
"{object1Name} contains {subObjectName} {subObjectID} "
"not in {object2Name}"
).format(
object1Name=object1Name,
subObjectName=subObjectName,
subObjectID=subObjectID,
object2Name=object2Name,
f"{object1Name} contains {subObjectName} {subObjectID} not in {object2Name}"
)
return text

Expand Down Expand Up @@ -469,25 +443,15 @@ def report(self, showOK=True, showWarnings=True):
state1 = "open"
if contour2.open:
state2 = "open"
text = "{contour1Name} is {state1} | {contour2Name} is {state2}".format(
contour1Name=self.contour1Name,
state1=state1,
contour2Name=self.contour2Name,
state2=state2,
)
text = f"{self.contour1Name} is {state1} | {self.contour2Name} is {state2}"
report.append(self.formatFatalString(text))
if self.directionDifference:
state1 = state2 = "counter-clockwise"
if contour1.clockwise:
state1 = "clockwise"
if contour2.clockwise:
state2 = "clockwise"
text = "{contour1Name} is {state1} | {contour2Name} is {state2}".format(
contour1Name=self.contour1Name,
state1=state1,
contour2Name=self.contour2Name,
state2=state2,
)
text = f"{self.contour1Name} is {state1} | {self.contour2Name} is {state2}"
report.append(self.formatFatalString(text))
report += self.reportSubObjects(
self.segments, showOK=showOK, showWarnings=showWarnings
Expand Down Expand Up @@ -521,12 +485,7 @@ def report(self, showOK=True, showWarnings=True):
if self.typeDifference:
type1 = segment1.type
type2 = segment2.type
text = "{segment1Name} is {type1} | {segment2Name} is {type2}".format(
segment1Name=self.segment1Name,
type1=type1,
segment2Name=self.segment2Name,
type2=type2,
)
text = f"{self.segment1Name} is {type1} | {self.segment2Name} is {type2}"
report.append(self.formatFatalString(text))
if report or showOK:
report.insert(0, self.title)
Expand Down Expand Up @@ -557,15 +516,7 @@ def report(self, showOK=True, showWarnings=True):
if self.baseDifference:
name1 = component1.baseName
name2 = component2.baseName
text = (
"{component1Name} has base glyph {name1} | "
"{component2Name} has base glyph {name2}"
).format(
component1Name=self.component1Name,
name1=name1,
component2Name=self.component2Name,
name2=name2,
)
text = f"{self.component1Name} has base glyph {name1} | {self.component2Name} has base glyph {name2}"
report.append(self.formatWarningString(text))
if report or showOK:
report.insert(0, self.title)
Expand Down Expand Up @@ -596,14 +547,7 @@ def report(self, showOK=True, showWarnings=True):
if self.nameDifference:
name1 = anchor1.name
name2 = anchor2.name
text = (
"{anchor1Name} has name {name1} | " "{anchor2Name} has name {name2}"
).format(
anchor1Name=self.anchor1Name,
name1=name1,
anchor2Name=self.anchor2Name,
name2=name2,
)
text = f"{self.anchor1Name} has name {name1} | {self.anchor2Name} has name {name2}"
report.append(self.formatWarningString(text))
if report or showOK:
report.insert(0, self.title)
Expand Down Expand Up @@ -634,15 +578,7 @@ def report(self, showOK=True, showWarnings=True):
if self.nameDifference:
name1 = guideline1.name
name2 = guideline2.name
text = (
"{guideline1Name} has name {name1} | "
"{guideline2Name} has name {name2}"
).format(
guideline1Name=self.guideline1Name,
name1=name1,
guideline2Name=self.guideline2Name,
name2=name2,
)
text = f"{self.guideline1Name} has name {name1} | {self.guideline2Name} has name {name2}"
report.append(self.formatWarningString(text))
if report or showOK:
report.insert(0, self.title)
Expand Down
4 changes: 2 additions & 2 deletions Lib/fontParts/base/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class BaseComponent(

def _reprContents(self):
contents = [
"baseGlyph='%s'" % self.baseGlyph,
"offset='({x}, {y})'".format(x=self.offset[0], y=self.offset[1]),
f"baseGlyph='{self.baseGlyph}'",
f"offset='({self.offset[0]}, {self.offset[1]})'",
]
if self.glyph is not None:
contents.append("in glyph")
Expand Down
14 changes: 7 additions & 7 deletions Lib/fontParts/base/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseContour(
def _reprContents(self):
contents = []
if self.identifier is not None:
contents.append("identifier='%r'" % self.identifier)
contents.append(f"identifier='{self.identifier!r}'")
if self.glyph is not None:
contents.append("in glyph")
contents += self.glyph._reprContents()
Expand Down Expand Up @@ -619,7 +619,7 @@ def removeSegment(self, segment, preserveCurve=False):
segment = self.segments.index(segment)
segment = normalizers.normalizeIndex(segment)
if segment >= self._len__segments():
raise ValueError("No segment located at index %d." % segment)
raise ValueError(f"No segment located at index {segment}.")
preserveCurve = normalizers.normalizeBoolean(preserveCurve)
self._removeSegment(segment, preserveCurve)

Expand Down Expand Up @@ -652,7 +652,7 @@ def setStartSegment(self, segment):
return
if segmentIndex >= len(segments):
raise ValueError(
("The contour does not contain a segment at index %d" % segmentIndex)
(f"The contour does not contain a segment at index {segmentIndex}")
)
self._setStartSegment(segmentIndex)

Expand Down Expand Up @@ -773,7 +773,7 @@ def removeBPoint(self, bPoint):
bPoint = bPoint.index
bPoint = normalizers.normalizeIndex(bPoint)
if bPoint >= self._len__points():
raise ValueError("No bPoint located at index %d." % bPoint)
raise ValueError(f"No bPoint located at index {bPoint}.")
self._removeBPoint(bPoint)

def _removeBPoint(self, index, **kwargs):
Expand Down Expand Up @@ -829,7 +829,7 @@ def _lenPoints(self, **kwargs):
def _getitem__points(self, index):
index = normalizers.normalizeIndex(index)
if index >= self._len__points():
raise ValueError("No point located at index %d." % index)
raise ValueError(f"No point located at index {index}.")
point = self._getPoint(index)
self._setContourInPoint(point)
return point
Expand Down Expand Up @@ -953,7 +953,7 @@ def removePoint(self, point, preserveCurve=False):
point = self.points.index(point)
point = normalizers.normalizeIndex(point)
if point >= self._len__points():
raise ValueError("No point located at index %d." % point)
raise ValueError(f"No point located at index {point}.")
preserveCurve = normalizers.normalizeBoolean(preserveCurve)
self._removePoint(point, preserveCurve)

Expand Down Expand Up @@ -981,7 +981,7 @@ def setStartPoint(self, point):
return
if pointIndex >= len(points):
raise ValueError(
("The contour does not contain a point at index %d" % pointIndex)
(f"The contour does not contain a point at index {pointIndex}")
)
self._setStartPoint(pointIndex)

Expand Down
Loading

0 comments on commit 19f9ac6

Please sign in to comment.