Delete OT Classes with Python

malcolm's picture


Can anyone tell if it's possible to delete OT Classes using Python. I have tried something like:

for k in range(len(f.classes)):
s = f.classes[k][0]
if s=="_":
del f.classes[k]

to delete every class that starts with an underscore, but this approach doesn't work. Is there another way to delete classes or am I doing something stupid?


k.l.'s picture

Try this:

classes = f.classes
for k in range(len(classes)-1,-1,-1):
   if len(classes[k].strip()) and classes[k][0] == "_":
      del classes[k]
f.classes = classes

Tim Ahrens's picture

Yeah, very strange behaviour of the API. The elements of the list are read-only but the enclosing structure can be overwritten. I ended up with the same workaround.

malcolm's picture

Thank you Karsten. I must admit I don't understand 100% why that works but thanks you for your solution.


twardoch's picture

Two things:

1. When you delete an element from a list, the number of items in the list obviously is reduced by one. But the initial range(len()) loop is just a static list so if you remove any elements, you'll get an "Index out of range". This is why Karsten is iterating through the loop in the descending order of elements. This is not FontLab-specific, it would happen with every Python list for which you'd remove elements that way.

2. Many of FontLab's pseudolist objects only implement partial behavior of list objects. But you can assign such pseudolist object to a real Python list, then perform some list modifications, and then reassign the list back to the FontLab pseudolist. I don't consider this "normal behavior" but, well, the FontLab Python API was developed quite a few years ago and some of the objects behave a bit strangely.

Syndicate content Syndicate content