Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea06596

Browse files
committed
Convert FontEntry to a dataclass.
1 parent 0d3632b commit ea06596

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

doc/api/font_manager_api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
:undoc-members:
88
:show-inheritance:
99

10-
11-
10+
.. autoclass:: FontEntry
11+
:no-undoc-members:

lib/matplotlib/font_manager.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# - setWeights function needs improvement
2424
# - 'light' is an invalid weight value, remove it.
2525

26+
import dataclasses
2627
from functools import lru_cache
2728
import json
2829
import logging
@@ -342,35 +343,22 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
342343
return [fname for fname in fontfiles if os.path.exists(fname)]
343344

344345

345-
class FontEntry:
346-
"""
347-
A class for storing Font properties. It is used when populating
348-
the font lookup dictionary.
349-
"""
350-
def __init__(self,
351-
fname ='',
352-
name ='',
353-
style ='normal',
354-
variant='normal',
355-
weight ='normal',
356-
stretch='normal',
357-
size ='medium',
358-
):
359-
self.fname = fname
360-
self.name = name
361-
self.style = style
362-
self.variant = variant
363-
self.weight = weight
364-
self.stretch = stretch
365-
try:
366-
self.size = str(float(size))
367-
except ValueError:
368-
self.size = size
369-
370-
def __repr__(self):
371-
return "<Font '%s' (%s) %s %s %s %s>" % (
372-
self.name, os.path.basename(self.fname), self.style, self.variant,
373-
self.weight, self.stretch)
346+
FontEntry = dataclasses.make_dataclass(
347+
'FontEntry', [
348+
('fname', str, dataclasses.field(default='')),
349+
('name', str, dataclasses.field(default='')),
350+
('style', str, dataclasses.field(default='normal')),
351+
('variant', str, dataclasses.field(default='normal')),
352+
('weight', str, dataclasses.field(default='normal')),
353+
('stretch', str, dataclasses.field(default='normal')),
354+
('size', str, dataclasses.field(default='medium')),
355+
],
356+
namespace={
357+
'__doc__': """
358+
A class for storing Font properties.
359+
360+
It is used when populating the font lookup dictionary.
361+
"""})
374362

375363

376364
def ttfFontProperty(font):

0 commit comments

Comments
 (0)