0

I'm working with building pdf:s via reportlab and I'm currently trying to clean up the code. I want to build a light little class for handling the fonts (accessing and registering appropriately). I would like all available fonts to behave as properties and register themselves (a method call) when they are first accessed.

The list of available-fonts is a simple tuple of strings like so:

available_fonts = (
    "Roboto-Regular",
    "Roboto-Bold",
    "Roboto-Italic",
    "Roboto-Light",
    "Roboto-LightItalic",
    "Roboto-Thin",
    "Roboto-ThinItalic"
)

My python sense tells me that there is a neat solution to this :). I'm thinking of methods with the @property decorator but I would prefer not to have to write the methods for each font, it seems non-dry and harder to maintain.

Help would be much appreciated!

SverkerSbrg
  • 503
  • 1
  • 9
  • 23
  • how do you want to deal with characters that cannot be contained in identifier names (such as dash)? – fferri Jun 03 '15 at 13:15
  • See [how to add property dynamically](http://stackoverflow.com/questions/1325673/how-to-add-property-to-a-python-class-dynamically). – Ami Tavory Jun 03 '15 at 13:16
  • 1
    @mescalinum right, probably a poor approach then, to replace with underscores seems overly complicated, a simple method call such as font.get("...") is probably better then. – SverkerSbrg Jun 03 '15 at 13:19
  • @AmiTavory I'll have a look, thank you! – SverkerSbrg Jun 03 '15 at 13:20

0 Answers0