vintagedave / fontimize Goto Github PK
View Code? Open in Web Editor NEWFontimize: optimize fonts to only the specific glyphs needed for your text and website
License: GNU General Public License v3.0
Fontimize: optimize fonts to only the specific glyphs needed for your text and website
License: GNU General Public License v3.0
Hi,
I randomly came across your blog post on this library and thought I'd take a look at the code. I noticed you have an accidental O(n^2) string concatenation. In a few places in your code, you're appending lists of strings. This is a problem as it creates new intermediate strings in each iteration. Here is one such example:
Lines 186 to 190 in 317f1e1
A more efficient way to write the above would be something like the following as it creates a single new string object:
text = ''.join(BeautifulSoup(html, 'html.parser').get_text() for html in html_contents)
The main function optimise_fonts
and the other exposed functions using it return dict[str, typing.Any]
. This is inconvenient for the users of the library as they will have no autocomplete and type safety. There are two possible ways you can solve it. I would recommend 1 here, although it will break backwards compatibility. You can use 2 if you really prefer dictionaries and don't want to break the backwards compatibility.
Returning a suitable dataclass instead will allow users to access the values with dot notation:
from dataclasses import dataclass, field
@dataclass
class FontResult:
css: set[str] = field(default_factory=set)
fonts: dict[str, str] = field(default_factory=dict)
chars: set[str] = field(default_factory=set)
uranges: str = ''
Then you can return an instance of this instead of your dictionary:
font_res = FontResult()
...
font_res.chars = characters # etc
...
return font_res
Return a typed dictionary. The code would be something along these lines:
from typing import TypedDict
class FontResult(TypedDict):
css: set[str]
fonts: dict[str, str]
chars: set[str]
uranges: str
def optimise_fonts(...) -> FontResult:
font_res = {'css': set(), 'fonts': {}, 'chars': set(), 'uranges': ''}
...
return font_res
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.