From ea06596b8a196b4bfefc026546d25a933e826094 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 30 Apr 2021 00:29:38 -0400 Subject: [PATCH 1/2] Convert FontEntry to a dataclass. --- doc/api/font_manager_api.rst | 4 +-- lib/matplotlib/font_manager.py | 46 +++++++++++++--------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/doc/api/font_manager_api.rst b/doc/api/font_manager_api.rst index 24bfefe00d32..8b698bacf0fe 100644 --- a/doc/api/font_manager_api.rst +++ b/doc/api/font_manager_api.rst @@ -7,5 +7,5 @@ :undoc-members: :show-inheritance: - - +.. autoclass:: FontEntry + :no-undoc-members: diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 5cef1ce57c9d..61ce6489c303 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -23,6 +23,7 @@ # - setWeights function needs improvement # - 'light' is an invalid weight value, remove it. +import dataclasses from functools import lru_cache import json import logging @@ -342,35 +343,22 @@ def findSystemFonts(fontpaths=None, fontext='ttf'): return [fname for fname in fontfiles if os.path.exists(fname)] -class FontEntry: - """ - A class for storing Font properties. It is used when populating - the font lookup dictionary. - """ - def __init__(self, - fname ='', - name ='', - style ='normal', - variant='normal', - weight ='normal', - stretch='normal', - size ='medium', - ): - self.fname = fname - self.name = name - self.style = style - self.variant = variant - self.weight = weight - self.stretch = stretch - try: - self.size = str(float(size)) - except ValueError: - self.size = size - - def __repr__(self): - return "" % ( - self.name, os.path.basename(self.fname), self.style, self.variant, - self.weight, self.stretch) +FontEntry = dataclasses.make_dataclass( + 'FontEntry', [ + ('fname', str, dataclasses.field(default='')), + ('name', str, dataclasses.field(default='')), + ('style', str, dataclasses.field(default='normal')), + ('variant', str, dataclasses.field(default='normal')), + ('weight', str, dataclasses.field(default='normal')), + ('stretch', str, dataclasses.field(default='normal')), + ('size', str, dataclasses.field(default='medium')), + ], + namespace={ + '__doc__': """ + A class for storing Font properties. + + It is used when populating the font lookup dictionary. + """}) def ttfFontProperty(font): From 8e075a1b35e19306433a3bdbd89383e9f2290b39 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 30 Apr 2021 00:32:26 -0400 Subject: [PATCH 2/2] Flatten FontProperties.__init__ arguments. No need to split over multiple lines and align equals signs. --- .flake8 | 2 +- lib/matplotlib/font_manager.py | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/.flake8 b/.flake8 index 0afde30b4a0e..715729f93b63 100644 --- a/.flake8 +++ b/.flake8 @@ -58,7 +58,7 @@ per-file-ignores = lib/matplotlib/backends/qt_editor/formlayout.py: F401, F403 lib/matplotlib/cbook/__init__.py: F401 lib/matplotlib/cbook/deprecation.py: F401 - lib/matplotlib/font_manager.py: E221, E251, E501 + lib/matplotlib/font_manager.py: E501 lib/matplotlib/image.py: F401, F403 lib/matplotlib/lines.py: F401 lib/matplotlib/mathtext.py: E221, E251 diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 61ce6489c303..5a9ed7953581 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -618,16 +618,10 @@ class FontProperties: fontconfig. """ - def __init__(self, - family = None, - style = None, - variant= None, - weight = None, - stretch= None, - size = None, - fname = None, # if set, it's a hardcoded filename to use - math_fontfamily = None, - ): + def __init__(self, family=None, style=None, variant=None, weight=None, + stretch=None, size=None, + fname=None, # if set, it's a hardcoded filename to use + math_fontfamily=None): self._family = _normalize_font_family(rcParams['font.family']) self._slant = rcParams['font.style'] self._variant = rcParams['font.variant']