Lib

Overview

BaseLib.copy

Copy the current object into a new object of the same type.

BaseLib.glyph

Get or set the lib's parent glyph object.

BaseLib.font

Get or set the lib's parent font object.

BaseLib.__len__

Return the number of keys in the lib.

BaseLib.keys

Return the lib's keys.

BaseLib.items

Return the lib's items.

BaseLib.values

Return the lib's values.

BaseLib.__contains__

Check if the given key exists in the lib.

BaseLib.__setitem__

Set the value for a given key in the lib.

BaseLib.__getitem__

Get the value associated with the given key.

BaseLib.get

Get the value for the given key in the lib.

BaseLib.__delitem__

Remove the given key from the lib.

BaseLib.pop

Remove the specified key and return its associated value.

BaseLib.__iter__

Return an iterator over the keys in the lib.

BaseLib.update

Update the current lib with key-value pairs from another.

BaseLib.clear

Remove all keys from the lib.

BaseLib.naked

Return the environment's native object wrapped by the current object.

BaseLib.changed

Tell the environment that something has changed in the object.

Reference

class fontParts.base.BaseLib(*args: Any, **kwargs: Any)[source]

Represent the basis for a lib object.

This object behaves like a Python dict object. Most of the dictionary functionality comes from BaseDict. Consult that object’s documentation for the required environment implementation details.

Variables:

This object is normally created as part of a BaseFont. An orphan BaseLib object instance can be created like this:

>>> lib = RLib()

Copy

BaseLib.copy() BaseObjectType

Copy the current object into a new object of the same type.

The returned object will not have a parent object.

Returns:

A new BaseObject subclass instance with the same attributes.

Parents

BaseLib.glyph: dynamicProperty

Get or set the lib’s parent glyph object.

The value must be a BaseGlyph instance or None.

Returns:

The BaseGlyph instance containing the lib or None.

Raises:

AssertionError

  • If the font for the lib has already been set.

  • If attempting to set the glyph when it has already been set and is not the same as the provided glyph.

Example:

>>> glyph = lib.glyph
BaseLib.font: dynamicProperty

Get or set the lib’s parent font object.

The value must be a BaseFont instance or None.

Returns:

The BaseFont instance containing the lib or None.

Raises:

AssertionError

  • If attempting to set the font when it has already been set and is not the same as the provided font.

  • If the glyph for the lib has already been set.

Example:

>>> font = lib.font

Dictionary

BaseLib.__len__() int[source]

Return the number of keys in the lib.

Returns:

An int representing the number of keys in the lib.

Example:

>>> len(font.lib)
5
BaseLib.keys() BaseKeys[str][source]

Return the lib’s keys.

Returns:

A type-view of str items representing the lib’s keys.

Example:

>>> font.lib.keys()
["public.glyphOrder", "org.robofab.scripts.SomeData",
 "public.postscriptNames"]
BaseLib.items() BaseItems[str, LibValueType][source]

Return the lib’s items.

Each item is represented as a tuple of key-value pairs, where:
  • key is a str.

  • value is a type-lib-value.

Returns:

A type-view of the lib’s (key, value) pairs.

Example:

>>> font.lib.items()
[("public.glyphOrder", ["A", "B", "C"]),
 ("public.postscriptNames", {'be': 'uni0431', 'ze': 'uni0437'})]
BaseLib.values() BaseValues[LibValueType][source]

Return the lib’s values.

Returns:

A type-view of type-lib-value.

Example:

>>> font.lib.items()
[["A", "B", "C"], {'be': 'uni0431', 'ze': 'uni0437'}]
BaseLib.__contains__(key: str) bool[source]

Check if the given key exists in the lib.

Parameters:

key – The key to check for existence as a str.

Returns:

True if the key exists in the lib False otherwise.

Example:

>>> "public.glyphOrder" in font.lib
True
BaseLib.__setitem__(key: str, value: LibValue) None[source]

Set the value for a given key in the lib.

Parameters:
  • key – The key to set as a str.

  • value – The type-lib-value to set for the given key.

Example:

>>> font.lib["public.glyphOrder"] = ["A", "B", "C"]
BaseLib.__getitem__(key: str) LibValue[source]

Get the value associated with the given key.

Parameters:

key – The key to retrieve the value for as a str.

Returns:

The type-lib-value associated with the specified key.

Raises:

KeyError – If the specified key does not exist.

Example:

>>> font.lib["public.glyphOrder"]
["A", "B", "C"]

Note

Any changes to the returned lib contents will not be reflected in it’s BaseLib instance. To make changes to this content, do the following:

>>> lib = font.lib["public.glyphOrder"]
>>> lib.remove("A")
>>> font.lib["public.glyphOrder"] = lib
BaseLib.get(key: str, default: TypeAliasForwardRef('~fontParts.base.annotations.LibValue') | None = None) TypeAliasForwardRef('~fontParts.base.annotations.LibValue') | None[source]

Get the value for the given key in the lib.

If the given key is not found, The specified default will be returned.

Parameters:
  • key – The key to look up as a str.

  • default – The optional default type-lib-value to return if the key is not found. Defaults to None.

Returns:

The type-lib-value for the given key, or the default value if the key is not found.

Example:

>>> font.lib.get("public.glyphOrder")
["A", "B", "C"]
>>> font.lib.get("missingKey", default="Default Value")
"Default Value"

..note:

Any changes to the returned lib contents will not be reflected in
it's :class:`BaseLib` instance. To make changes to this content,
do the following::

    >>> lib = font.lib.get("public.glyphOrder")
    >>> lib.remove("A")
    >>> font.lib["public.glyphOrder"] = lib
BaseLib.__delitem__(key: str) None[source]

Remove the given key from the lib.

Parameters:

key – The key to remove as a str.

Example:

>>> del font.lib["public.glyphOrder"]
BaseLib.pop(key: str, default: TypeAliasForwardRef('~fontParts.base.annotations.LibValue') | None = None) TypeAliasForwardRef('~fontParts.base.annotations.LibValue') | None[source]

Remove the specified key and return its associated value.

If the key does not exist, the default value is returned.

Parameters:
  • key – The key to remove as a str.

  • default – The optional default type-lib-value to return if the key is not found. Defaults to None.

Returns:

The type-lib-value associated with the given key, or the default value if the key is not found.

Example:

>>> font.lib.pop("public.glyphOrder")
["A", "B", "C"]
BaseLib.__iter__() Iterator[str][source]

Return an iterator over the keys in the lib.

The iteration order is not fixed.

Returns:

An Iterator over the str keys.

Example:

>>> for key in font.lib:
>>>     print key
"public.glyphOrder"
"org.robofab.scripts.SomeData"
"public.postscriptNames"
BaseLib.update(otherLib: MutableMapping[str, TypeAliasForwardRef('~fontParts.base.annotations.LibValue')]) None[source]

Update the current lib with key-value pairs from another.

For each key in otherLib:
  • If the key exists in the current lib, its value is replaced with the value from otherLib.

  • If the key does not exist in the current lib, it is added.

Keys that exist in the current lib but are not in otherLib remain unchanged.

Parameters:

otherLib – A MutableMapping of str keys mapped to type-lib-value to update the current lib with.

Example:

>>> font.lib.update(newLib)
BaseLib.clear() None[source]

Remove all keys from the lib.

This will reset the BaseLib instance to an empty dictionary.

Example:

>>> font.lib.clear()

Environment

BaseLib.naked() Any

Return the environment’s native object wrapped by the current object.

Raises:

NotImplementedError – If the method has not been overridden by a subclass.

Example:

>>> loweLevelObj = obj.naked()
BaseLib.changed(*args: Any, **kwargs: Any) None

Tell the environment that something has changed in the object.

The behavior of this method will vary from environment to environment.

Parameters:
  • *args – Any positional arguments.

  • **kwargs – Any keyword arguments.

Example:

>>> obj.changed()