Skip to content

Commit

Permalink
PERF: delay most costly import statement until they are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Nov 28, 2024
1 parent d08f957 commit 86dc7e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/inifix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from .validation import validate_inifile_schema
from .format import format_string

from importlib.metadata import version

__version__ = version("inifix")
del version
def __getattr__(item: str):
if item == "__version__":
from importlib.metadata import version

return version("inifix")
10 changes: 6 additions & 4 deletions src/inifix/format.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from __future__ import annotations

import argparse
import os
import re
import sys
import warnings
from collections.abc import Iterable
from concurrent.futures import ThreadPoolExecutor
from difflib import unified_diff
from functools import partial
from io import StringIO
from tempfile import TemporaryDirectory
from typing import IO, Literal

from inifix._cli import Message, TaskResults, get_cpu_count
Expand Down Expand Up @@ -143,7 +140,10 @@ def iniformat(s: str, /) -> str:


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser()
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor

parser = ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument(
"--diff",
Expand Down Expand Up @@ -234,6 +234,8 @@ def _format_single_file_cli(
)
return TaskResults(status, messages)

from tempfile import TemporaryDirectory

with TemporaryDirectory(dir=os.path.dirname(file)) as tmpdir:
tmpfile = os.path.join(tmpdir, "ini")
with open(tmpfile, "wb") as bfh:
Expand Down

0 comments on commit 86dc7e2

Please sign in to comment.