Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Created November 19, 2021 10:58
Show Gist options
  • Save anthrotype/d82b4884c173f4d8eb2a58b7d8c3e272 to your computer and use it in GitHub Desktop.
Save anthrotype/d82b4884c173f4d8eb2a58b7d8c3e272 to your computer and use it in GitHub Desktop.
import logging
import shutil
import os
from fontTools.misc.loggingTools import Timer
import orjson
from ufoLib2.converters import json_converter as c
from ufoLib2.objects import Font
timer = Timer(logging.getLogger())
logging.basicConfig(level=logging.DEBUG)
# timing Font.save() on fresh new path, without shutil.rmtree implied by overwrite=True
if os.path.exists("/tmp/NotoSans-Regular.ufo"):
shutil.rmtree("/tmp/NotoSans-Regular.ufo")
with timer("open + save ufoLib2.Font from/to XML UFO (total time)"):
with timer("parse ufoLib2.Font from XML UFO (non lazy)"):
font = Font.open("NotoSans-Regular.ufo", lazy=False)
with timer("save Font to XML UFO"):
font.save("/tmp/NotoSans-Regular.ufo")
with timer("dump + load ufoLib2.Font to/from json (total time)"):
with timer("dump ufoLib2.Font to json (total time)"):
with timer("unstructure ufoLib2.Font with cattrs"):
font_data = c.unstructure(font)
with timer("serialize unstructured font data to json with orjson"):
json_data = orjson.dumps(font_data)
with timer("write out json string to disk"):
with open("notosans-regular.json", "wb") as f:
f.write(json_data)
with timer("parse ufoLib2.Font from json (total time)"):
with timer("reading json string from disk"):
with open("notosans-regular.json", "rb") as f:
json_data = f.read()
with timer("parsing raw font data from json string with orjson"):
font_data = orjson.loads(json_data)
with timer("structuring ufoLib2.Font from raw font data with cattrs"):
font2 = c.structure(font_data, Font)
assert font == font2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment