Giter Club home page Giter Club logo

fontimize's People

Contributors

vintagedave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fontimize's Issues

O(n^2) string concatenation

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:

Fontimize/fontimize.py

Lines 186 to 190 in 317f1e1

def optimise_fonts_for_html_contents(html_contents : list[str], fonts : list[str], fontpath : str = "", subsetname = "FontimizeSubset", verbose : bool = False, print_stats : bool = True) -> dict[str, typing.Any]:
text = ""
for html in html_contents:
soup = BeautifulSoup(html, 'html.parser')
text = text + soup.get_text()

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)

optimise_fonts's return value could be better typed

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.

1. Using a dataclass:

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

2. Using TypedDict:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.