converters
Utility functions for converting values between various formats.
These functions focus on converting between different string and numeric formats related to measurements, Unicode codepoints, and letter casing.
To import the module:
>>> from smufolib import converters
- convertMeasurement(measurement: int | float | str, targetUnit: str, unitsPerEm: int | float, rounded: bool = False) int | float[source]
Convert between units of measurement based on UPM size.
- Parameters:
measurement – The value to convert.
targetUnit – Unit to convert to.
unitsPerEm – The UPM value to base conversion on.
rounded – Whether to round result to nearest integer.
- Raises:
TypeError – If
measurementis not an accepted type.ValueError – If
targetUnitis neither"spaces"nor"units".
Example:
>>> converters.convertMeasurement(0.795, "units", 1000, rounded=False) 198.75 >>> converters.convertMeasurement(0.795, "units", 1000, rounded=True) 199 >>> converters.convertMeasurement("198.75", "spaces", 1000, rounded=True) 0.795
- toDecimal(string: str) int[source]
Convert formatted unicode or uni name to decimal codepoint.
Function accepts any hexadecimal string within the Unicode range (U+0000 – U+10FFFF) prefixed by
"u","U+"or"uni".- Parameters:
string – The value to convert.
- Raises:
TypeError – If
stringis not the accepted type.ValueError – If
stringis not a valid formatted unicode codepoint or outside the unicode range (U+0000 – U+10FFFF).
Example:
>>> converters.toDecimal("uniE000") 57344 >>> converters.toDecimal("u1D100") 119040 >>> converters.toDecimal("U+0A00") 2560
- toIntIfWhole(number: int | float) int | float[source]
Convert float without a fractional part to integer.
If
numberis already anintor thefloathas a fractional part, it is left unchanged.- Parameters:
number – The value to convert.
Example:
>>> converters.toIntIfWhole(34.0) 34 >>> converters.toIntIfWhole(34.1) 34.1
- toKebab(camelCaseString: str) str[source]
Convert camelCase to kebab-case.
- Parameters:
camelCaseString – The string to convert.
Example:
>>> converters.toKebab("camelCase") 'camel-case'
- toNumber(numericString: str) int | float[source]
Convert numeric string to number.
- Parameters:
numericString – The value to convert.
- Raises:
ValueError – If
numericStringis not a valid int or float.
Example:
>>> converters.toNumber("57344") 57344 >>> converters.toNumber("57.344") 57.344 >>> converters.toNumber("E000") 57344
- toUniHex(value: int | str) str[source]
Convert decimal codepoint or uni-name to formatted Unicode hex.
- Parameters:
value – The decimal value to convert.
- Raises:
TypeError – If
valueis not the accepted type.ValueError – If
valueis outside the Unicode range (U+0000 – U+10FFFF).
Example:
>>> converters.toUniHex(2560) 'U+0A00' >>> converters.toUniHex("uni0A00") 'U+0A00'
- toUniName(value: int | str, short: bool = False) str[source]
Convert decimal codepoint or Unicode notation string to uni-name.
Function accepts any integer or prefixed hexadecimal string (e.g.,
"U+E000","uE000","uniE000") within the Unicode range (U+0000 – U+10FFFF).- Parameters:
value – The value to convert.
short – Whether to return name with single
"u"prefix.
- Raises:
TypeError – If
valueis not an accepted type.ValueError – If
valueis outside the Unicode range (U+0000 – U+10FFFF) or not a valid formatted string.
Example:
>>> converters.toUniName("U+E000") 'uniE000' >>> converters.toUniName('U+E000', short=True) 'uE000' >>> converters.toUniName(57344) 'uniE000'
error
Utility functions for error message generation and type validation.
This module provides functions to generate and validate error messages, check types, and suggest corrections for invalid values. It includes a dictionary of error message templates to ensure streamlined and consistent error reporting.
To import the module:
>>> from smufolib import error
- ERROR_TEMPLATES: dict[str, str]
Dictionary of error message templates.
Key
Template
"alphanumericValue"The value for
objectName!rmust be alphanumeric"alphanumericValueItems"Value items for
objectName!rmust be alphanumeric"argumentConflict"The option ‘
key’ is already added as positional argument or flag"attributeError"objectName!rhas no attributeattribute!r"contextualAttributeError"The attribute
attribute!ris not available whencontext"contextualSetAttributeError"Cannot set attribute
attribute!rwhencontext"contextualTypeError"Expected
objectName!rto be of typevalidTypeswhencontext, but gotvalueType"contextualItemsTypeError"Items in
objectName!rmust bevalidTypeswhencontext, notvalueType"deprecated"objectName!ris deprecated and will be removed in the next version of SMufoLib (afterversion)"deprecatedReplacement"Use
replacement!rinstead"duplicateFlags"Arguments
argument1!randargument2!rhave duplicate short flag:flag"duplicateAttributeValue"The value
value!rforattribute!ris already assigned to anotherobjectNameinstance:conflictingInstance!r"duplicateItems"Items in
objectName!rcannot be duplicates"emptyValue"The value for
objectName!rcannot be empty"emptyValueItems"Value items for
objectName!rcannot be empty"fileNotFound"The file or directory for
objectName!rdoes not exist"invalidFormat"The value for
objectName!ris not correctly formatted"invalidInitialCharacter"The value for
objectName!rmust start with a lowercase letter or number"invalidInitialItemsCharacter"Value items for
objectName!rmust start with a lowercase letter or number"itemsTypeError"Items in
objectName!rmust bevalidTypes, notvalueType"itemsValueError"Invalid value for item in
objectName!r:value!r"missingDependency"Cannot set
objectName!rbecausedependency!ris None"missingExtension"The value for
objectName!rmust have aextension!rextension"missingGlyph"No SMuFL glyph named
name!r"missingValue"Required values for
objectName!rare missing"nonIncreasingRange"The values in
objectName!rmust form an increasing range"notImplementedError"The
objectName!rsubclass does not implement this method"numericValue"The value for
objectName!rmust be numeric"overlappingRange"Range ‘
name’ (start-end) overlaps with an existing range"permissionError"Permission denied:
context"recommendScript"Consider running the script
scriptName!rbefore the current process"serializationError"Error serializing JSON data or writing to the file
"singleItem"objectName!rmust contain a value pair"suggestion"Did you mean
suggestion!r?"typeError"Expected
objectName!rto be of typevalidTypes, but gotvalueType"unicodeOutOfRange"The value for
objectName!ris outside the Unicode rangestart-end"urlError"Could not connect to URL:
url!r"valueError"Invalid value for
objectName!r:value!r"valueTooHigh"The value for
objectName!rmust bevalue!ror lower"valueTooLow"The value for
objectName!rmust bevalue!ror higher
- generateErrorMessage(*templateNames: Literal['alphanumericValue', 'alphanumericValueItems', 'argumentConflict', 'attributeError', 'contextualAttributeError', 'contextualItemsTypeError', 'contextualSetAttributeError', 'contextualTypeError', 'deprecated', 'deprecatedReplacement', 'duplicateAttributeValue', 'duplicateFlags', 'duplicateItems', 'emptyValue', 'emptyValueItems', 'fileNotFound', 'invalidFormat', 'invalidInitialCharacter', 'invalidInitialItemsCharacter', 'itemsTypeError', 'itemsValueError', 'missingDependency', 'missingExtension', 'missingGlyph', 'missingValue', 'nonIncreasingRange', 'notImplementedError', 'numericValue', 'overlappingRange', 'permissionError', 'recommendScript', 'serializationError', 'singleItem', 'suggestion', 'typeError', 'unicodeOutOfRange', 'urlError', 'valueError', 'valueTooHigh', 'valueTooLow'], string: str | None = None, **kwargs) str[source]
Generate an error message from a template and keyword arguments.
The
templateNamesandstringwill be concatenated in order.- Parameters:
*templateNames – The error message template string, which should contain placeholders for keyword arguments.
string – An additional message string to include. Defaults to
None**kwargs – Arbitrary keyword arguments corresponding to the placeholders in the template string.
- Raises:
KeyError – If a placeholder in the template does not have a corresponding keyword argument.
Examples:
>>> error.generateErrorMessage("alphanumericValue", objectName="unicode") "The value for 'unicode' must be alphanumeric"
>>> error.generateErrorMessage( ... "typeError", objectName="index", validTypes="int", valueType="str" ... ) "Expected 'index' to be of type int, but got str"
>>> error.generateErrorMessage( ... "urlError", string="Please try again", url="some/url.com" ... ) "Could not connect to URL: 'some/url.com'. Please try again"
- generateTypeError(value: Any, validTypes: type | tuple[type, ...], objectName: str, context: str | None = None, items: bool = False) str[source]
Generate a
TypeErrormessage.This function generates an error message based on the number of valid types. By default, the message is generated from
ERROR_TEMPLATES:"typeError".If
contextin notNone(or empty) anditemsisFalse,ERROR_TEMPLATES:"contextualTypeError"is used. IfitemsisTrueandcontextisNone,ERROR_TEMPLATES:"itemsTypeError"is used. If bothcontextis notNoneanditemsisTrue,ERROR_TEMPLATES:"contextualItemsTypeError"is used.- Parameters:
- Raises:
TypeError – If
valuedoes not match any of the valid types.ValueError – If
itemsisTrueandvalueis not an iterable.
Examples:
>>> error.generateTypeError(123, (str,), "path") "Expected 'path' to be of type str, but got int"
>>> from pathlib import Path >>> error.generateTypeError(123, (str, Path), "path") "Expected 'path' to be of type str or Path, but got int"
>>> from smufolib import Request >>> error.generateTypeError(123, (str, Path, Request), "path") "Expected 'path' to be of type str, Path or Request, but got int"
- suggestValue(value: str, possibilities: list[str] | tuple[str, ...], objectName: str, cutoff: float = 0.6, items=False) str[source]
Validate value and suggests a valid close match.
If
itemsisTrue, an alternate error message template is used to validate the values of items within an iterable value rather than the value itself.- Parameters:
value – The value to be validated.
possibilities – A list of valid possibilities for the value.
objectName – The name of the object being validated.
cutoff – A float value representing the cutoff threshold for similarity matches. Defaults to
0.6.items – Whether to use items-specific template. Defaults to
False.
- Raises:
ValueError – If the provided value is not found in
possibilitiesor ifitemsisTrueandvalueis not an iterable.
Example:
>>> try: ... error.suggestValue( ... "spiltStemUpSE", ["splitStemUpSE", "splitStemUpSW"], ... "anchorName", cutoff=0.5) ... except ValueError as e: ... print(e) Invalid value for 'anchorName': 'spiltStemUpSE'. Did you mean 'splitStemUpSE'?
- validateType(value: Any, validTypes: type | tuple[type, ...] | UnionType, objectName: str, items=False) None[source]
Validate if a value matches any of the valid types.
- Parameters:
- Raises:
TypeError – If
valuedoes not match any of the valid types.ValueError – If
itemsisTrueand anyvalueitem does not match any of the valid types.
Examples:
>>> try: ... error.validateType(123, str, "glyphName") ... except TypeError as e: ... print(e) Expected 'glyphName' to be of type str, but got int
>>> myList = ["uniE000", 1] >>> for item in myList: ... try: ... error.validateType(item, str, "myList", items=True) ... except ValueError as e: ... print(e) Items in 'myList' must be str, not int
normalizers
Utilities for normalizing various data types in SMufoLib.
This module extends fontParts.base.normalizers and provides
functions to normalize objects and data related to SMuFL fonts and
metadata requests. It includes normalization for:
Fontobjects
Smuflobjects and attributes
EngravingDefaultsobjects and attributes
Requestobjects and attributes
These functions ensure that the data adheres to expected formats and values, handling type validation and normalization as needed.
In addition to the documented functions below, it provides direct access to the following external normalizers for convenience:
To import the module:
>>> from smufolib import normalizers
- normalizeClasses(value: list[str] | tuple[str, ...] | None) tuple[str, ...][source]
Normalize smufl classes.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not an accepted type.If any
valueitem does not normalize withnormalizeSmuflName().If any
valueitems are duplicates.
- normalizeDescription(value: str | None, attributeName: str) str | None[source]
Normalize human-readable smufl descriptions.
- Parameters:
value – The value to normalize.
attributeName – The name of the attribute to normalize.
- Raises:
TypeError – If
valueis not an accepted type.ValueError – If
valueis an empty string.
- normalizeDesignSize(value: int | None) int | None[source]
Normalize design size.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not an accepted type.ValueError – If
valueis less than10.
- normalizeEngravingDefaults(value: EngravingDefaults) EngravingDefaults[source]
Normalize EngravingDefaults object.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not the accepted type.
- normalizeEngravingDefaultsAttr(name: str, value: int | float | list[str] | tuple[str, ...] | None) int | float | tuple[str, ...] | None[source]
Normalize engraving defaults attribute.
- Parameters:
name – The attribute name to normalize.
value – The attribute value to normalize.
- Raises:
TypeError – If any parameter value is not an accepted type.
AttributeError – If name is not a valid
EngravingDefaultsattribute name.ValueError – If
"name=textFontFamily"andvalueis not astror empty.
- normalizeFont(value: Font) Font[source]
Normalize Font object.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not the accepted type.
- normalizeRequest(value: Request) Request[source]
Normalize Request object.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not the accepted type.
- normalizeRequestPath(value: Path | str | None, parameter: str) str | None[source]
Normalize Request path.
Relative paths are resolved automatically.
- Parameters:
value – The value to normalize.
parameter – The name of the parameter being validated.
- Raises:
TypeError – If
valueis not the accepted type.
- normalizeSizeRange(value: list[int] | tuple[int, int] | None) tuple[int, int] | None[source]
Normalize design size.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueor it’s contents is not an accepted type.If
valuedoes not contain a value pair.If
valueis not an increasing range.If
valueitems do not normalize withnormalizeDesignSize(), except not beNone.
- normalizeSmufl(value: Smufl) Smufl[source]
Normalize Smufl object.
- Parameters:
value – The value to normalize.
- Raises:
TypeError – If
valueis not the accepted type.
- normalizeSmuflName(value: str | None, attributeName: str, items: bool = False) str | None[source]
Normalize cannonical SMuFL names of glyphs, classes and ranges.
- Parameters:
value – The value to normalize.
attributeName – The name of the attribute to normalize.
items – Whether to normalize
valueitems rather thanvalueitself. Defaults toFalse.
- Raises:
TypeError – If
valueis not an accepted type.If
valueis an empty string.If
valueis value contains non-alphanumeric characters.If
valuedoes not start with a lowercase letter or number.
rulers
Measurement utilities for calculating glyph geometry.
This module provides functions to retrieve, measure and inspect various parts of glyphs. It also defines:
ENGRAVING_DEFAULTS_MAPPING, the default association between ruler functions and glyphs used for calculatingEngravingDefaultsattribute values.DISPATCHER, for dynamically executing measurement operations by function name.
To import the module:
>>> from smufolib import rulers
- ENGRAVING_DEFAULTS_MAPPING: dict[str, dict[str, str]]
Default mapping of rulers and glyphs to
EngravingDefaultsattributes.Attribute
Glyph
Ruler Function
U+EB60
U+E031
U+E1F9
U+E1F7
U+E003
U+E036
U+E036
U+E036
U+E53E
U+E4F0
U+E022
U+E022
U+E010
U+E010
U+E010
U+E040
U+E030
U+E1FD
U+E1FD
U+E010
U+E210
U+E030
U+E010
U+E034
U+E030
U+E032
U+E1FD
U+E1FD
U+E1FE
- areAlligned(points: tuple[RPoint, ...], axis: str, tolerance: int | float = 6) bool[source]
Check if the specified
pointsare aligned onaxis.- Parameters:
points – The points to check for alignment.
axis – The axis (
"x"or"y") accross which to check for alignment.tolerance – The tolerance for misalignment to apply in font units. Defaults to
6.
Example:
>>> glyph = font["uniE050"] >>> point1, point2 = glyph[0].points[:2] >>> rulers.areAlligned((point1, point2), axis="x", tolerance=3) False
- combineBounds(boundsList: list[tuple[int | float, int | float, int | float, int | float]]) tuple[int | float, int | float, int | float, int | float] | None[source]
Combine a list of bounds into one bounding box.
Example:
>>> glyph1, glyph2 = font["uniE050"], font["uniE260"] >>> boundsList = [glyph1.bounds, glyph2.bounds] >>> rulers.combineBounds(boundsList) (-50, -634, 648, 1167)
- getGlyphContours(glyph: Glyph, includeComponents: bool = True) Generator[RContour][source]
Return all contours in
glyph, including component references.- Parameters:
Example:
>>> glyph = font["uniE050"] >>> rulers.getGlyphContours(glyph) <generator object getGlyphContours.<locals>.<genexpr> at 0x...>
- getGlyphPoints(glyph, types: str | tuple[str, ...] = ('line', 'curve', 'qcurve'), includeComponents: bool = True) Generator[RPoint][source]
Return all points in
glyph, matching giventypes.Any points referenced by components may be included.
- Parameters:
Example:
>>> glyph = font["uniE050"] >>> rulers.getGlyphPoints(glyph) <generator object getGlyphPoints.<locals>.<genexpr> at 0x...>
- getGlyphSegments(glyph, types: str | tuple[str, ...] = ('line', 'curve', 'qcurve'), includeComponents: bool = True) Generator[RSegment][source]
Return all segments in
glyph, matching giventypes.Any segments referenced by components may be included.
- Parameters:
Example:
>>> glyph = font["uniE050"] >>> rulers.getGlyphSegments(glyph) <generator object getGlyphSegments.<locals>.<genexpr> at 0x...>
- getParentSegment(point: RPoint) RSegment | None[source]
Get the segment which the given point belongs to.
- Parameters:
point – The point to find the parent segment for.
Example:
>>> glyph = font["uniE050"] >>> point = glyph[0].points[0] >>> rulers.getParentSegment(point) <RSegment line index='3' at ...>
- glyphBoundsHeight(glyph: Glyph) int | float | None[source]
Return height of the glyph’s bounding box.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE050"] >>> rulers.glyphBoundsHeight(glyph) 1801
- glyphBoundsWidth(glyph: Glyph) int | float | None[source]
Return width of the glyph’s bounding box.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE050"] >>> rulers.glyphBoundsWidth(glyph) 648
- glyphBoundsXMinAbs(glyph: Glyph) int | float | None[source]
Return absolute value of glyph’s xMin bound.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE022"] >>> rulers.glyphBoundsXMinAbs(glyph) 80
- hasHorizontalOffCurve(point: RPoint) bool[source]
Check if the given point has a predominantly horizontal off-curve.
An off-curve control point is considered horizontal if its x-difference from the on-curve point is greater than its y-difference.
- Parameters:
point – The on-curve point to check.
Example:
>>> glyph = font["uniE260"] >>> point = glyph[0].points[0] >>> rulers.hasHorizontalOffCurve(point) True
- hasVerticalOffCurve(point: RPoint) bool[source]
Check if the given point has a predominantly vertical off-curve.
An off-curve control point is considered vertical if its y-difference from the on-curve point is greater than its x-difference.
- Parameters:
point – The on-curve point to check.
Example:
>>> glyph = font["uniE260"] >>> point = glyph[0].points[0] >>> rulers.hasVerticalOffCurve(point) False
- wedgeArmStrokeWidth(glyph: Glyph)[source]
Measure thickness of arm in a wedge-shaped glyph.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE1FD"] >>> rulers.yStrokeWidthAtMinimum(glyph) 50
- xDistanceBetweenContours(glyph: Glyph) int | float | None[source]
Measure horizontal distance between two adjacent contours in a glyph.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE031"] >>> rulers.xDistanceBetweenContours(glyph) 85
- xDistanceStemToDot(glyph: Glyph) int | float | None[source]
Measure horizontal distance between a stem and a dot contour.
The contours may be placed on either side of each other.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE040"] >>> rulers.glyphBoundsXMinAbs(glyph) 63
- xStrokeWidthAtOrigin(glyph: Glyph) int | float | None[source]
Measure horizontal distance between aligned points closest to origin.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE1FE"] >>> rulers.xStrokeWidthAtOrigin(glyph) 32
- yDistanceBetweenContours(glyph: Glyph) int | float | None[source]
Measure vertical distance between two adjacent contours in a glyph.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE036"] >>> rulers.yDistanceBetweenContours(glyph) 119
- yStrokeWidthAtMinimum(glyph: Glyph) int | float | None[source]
Measure vertical distance between aligned low-points in the glyph.
- Parameters:
glyph – Source
Glyphof contours to measure.
Example:
>>> glyph = font["uniE1FD"] >>> rulers.yStrokeWidthAtMinimum(glyph) 50
- DISPATCHER: dict[str, Callable[[Glyph], int | float | None]] = {'glyphBoundsHeight': <function glyphBoundsHeight>, 'glyphBoundsWidth': <function glyphBoundsWidth>, 'glyphBoundsXMinAbs': <function glyphBoundsXMinAbs>, 'wedgeArmStrokeWidth': <function wedgeArmStrokeWidth>, 'xDistanceBetweenContours': <function xDistanceBetweenContours>, 'xDistanceStemToDot': <function xDistanceStemToDot>, 'xStrokeWidthAtOrigin': <function xStrokeWidthAtOrigin>, 'yDistanceBetweenContours': <function yDistanceBetweenContours>, 'yStrokeWidthAtMinimum': <function yStrokeWidthAtMinimum>}
Dispatch the different ruler functions by their
strname.
scriptUtils
Script utility functions.
This module provides a set of utility functions specifically intended for use in scripts and applications. Particualarly, it provides a set of noramlizers to ensure the integrity of objects frequently passed by the user in an application setting.
To import the module:
>>> from smufolib import scriptUtils
- normalizeColor(color: tuple[int | float, int | float, int | float, int | float] | None, mark: bool) tuple[int | float, int | float, int | float, int | float] | None[source]
Ensure that
coloris valid based on themarksetting.”If the input
colorisNoneandmarkisTrue, aTypeErroris raised. Otherwise, the color is normalized each dictionary value- Parameters:
color – The color value to normalize.
mark – Mark objects with the specified
color.
- Raises:
TypeError – If
coloris not the expected type.
- normalizeColorDict(colorDict: dict[str, tuple[int | float, int | float, int | float, int | float]] | None, mark: bool) dict[str, tuple[int | float, int | float, int | float, int | float]] | None[source]
Ensure that
ColorDictis valid based on themarksetting.”If the input
ColorDictisNoneandmarkisTrue, aTypeErroris raised. Otherwise, each value is normalized usingnormalizeColor().- Parameters:
colorDict – The color dictionary to normalize.
mark – Mark objects with the specified colors in
colorDict.
- Raises:
TypeError – If
colorDictis not the expected type.
- normalizeFont(font: Font | Path | str) Font[source]
Ensure that
fontrepresents a font object.If
fontis astrorPath, it is treated as a path and passed to theFontconstructor.- Parameters:
font – The font to normalize.
- Raises:
TypeError – If
fontis not the expected type.
- normalizeJsonDict(jsonDict: dict[str, Any] | None) dict[str, Any][source]
Ensure that
jsonDictis notNone.
- normalizeRequest(request: Request | Path | str) Request[source]
Ensure that
requestrepresents a request object.If
requestis astrorPath, it is treated as a path and passed to theRequestconstructor.- Parameters:
request – The request to normalize.
- Raises:
TypeError – If
requestis not the expected type.
stdUtils
Miscellaneous general-purpose utility functions.
This module provides a set of utility functions for various common tasks, including flattening nested iterables, summing tuples, and extracting summary lines from docstrings. It also includes functions for validating class attributes and string representations of floats, printing verbose messages, as well as a a no-op function for placeholder purposes.
To import the module:
>>> from smufolib import stdUtils
- addTuples(*tuples: tuple[int | float, ...]) tuple[int | float, ...][source]
Sumize tuple values.
- Parameters:
*tuples – tuples to add.
Example:
>>> stdUtils.addTuples((2, 4), (2, 4)) (4, 8)
- doNothing(*args: Any, **kwargs: Any) None[source]
Do nothing at all.
This function accepts any arguments but does nothing with them.
- Parameters:
*args – Positional arguments.
**kwargs – Keyword arguments.
Example:
>>> stdUtils.doNothing(1, 2, 3) >>> stdUtils.doNothing(a=1, b=2)
- flatten(iterable: Iterable[Any], depth: int | None = None) Generator[source]
Flatten irregularly nested iterables of any depth.
- Parameters:
Examples:
>>> list(stdUtils.flatten([1, [2, [3, [4, 5]]]], depth=2)) [1, 2, 3, [4, 5]]
>>> list(stdUtils.flatten([1, [2, [3, [4, [5]]]]], depth=None)) [1, 2, 3, 4, 5]
- getSummary(docstring: str | None) str | None[source]
Extract the summary line from a docstring.
- Parameters:
docstring – The docstring from which to get the summary line.
Example:
>>> def showcaseGetSummary(): ... """Provide an example for the `getSummary()` function. ... ... This function provides an example to show how to ... extract a summary line from a docstring. ... ... """ ... >>> docstring = showcaseGetSummary.__doc__ >>> stdUtils.getSummary(docstring) "Give example for `getSummary()` function."
- isFloat(string: str) bool[source]
Check if string represents a
float.- Parameters:
string – The string to check.
Examples:
>>> stdUtils.isFloat("3.14") True
>>> stdUtils.isFloat("314") False
- isInUnicodeRange(codepoint: int) bool[source]
Check if an integer is within the Unicode range (U+0000 to U+10FFFF).
- validateAttr(obj, attributes: Iterable[str] | None = None) bool[source]
Validate
objbased on whether the given attributes have been set.This function returns
Falseif any given attribute is notNone, otherwise itreturnsTrue. It supports validation of nested attributes, e.g.:"attr1.attr2.attr3".- Parameters:
obj – The object to validate.
attributes – The attribute names to check.
Example:
>>> class MyClass: ... def __init__(self): ... self.attr1 = 1 ... self.attr2 = None ... self.attr3 = False ... >>> obj = MyClass() >>> stdUtils.validateAttr(obj, "attr1") True >>> stdUtils.validateAttr(obj, "attr2") False >>> stdUtils.validateAttr(obj, "attr3") True >>> stdUtils.validateAttr(obj, ["attr1", "attr2"]) False
- verbosePrint(message: str, verbose: bool, *args: Any, **kwargs: Any) None[source]
Print a message if
verboseisTrue.This function behaves like the built-in
print()function, but only prints the message ifverboseisTrue. IfverboseisFalse, it does nothing.- Parameters:
Example:
>>> stdUtils.verbosePrint("Hello, world!", True) Hello, world! >>> stdUtils.verbosePrint("Hello, world!", False)