Skip to content

Commit

Permalink
Update internal utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Apr 5, 2024
1 parent 323ceb7 commit 20df81e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion foamlib/_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import aioshutil

from ._subprocesses import run_process, run_process_async, CalledProcessError
from ._util import run_process, run_process_async, CalledProcessError
from ._dictionaries import FoamFile, FoamFieldFile


Expand Down
25 changes: 5 additions & 20 deletions foamlib/_dictionaries/_serialization.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
from contextlib import suppress
from typing import Any, Mapping, Sequence
from typing import Any, Mapping

from ._base import FoamDictionaryBase

try:
import numpy as np
except ModuleNotFoundError:
numpy = False
else:
numpy = True


def _is_sequence(value: Any) -> bool:
return (
isinstance(value, Sequence)
and not isinstance(value, str)
or numpy
and isinstance(value, np.ndarray)
)
from .._util import is_sequence


def _serialize_bool(value: Any) -> str:
Expand All @@ -30,14 +15,14 @@ def _serialize_bool(value: Any) -> str:


def _serialize_list(value: Any) -> str:
if _is_sequence(value):
if is_sequence(value):
return f"({' '.join(_serialize_value(v) for v in value)})"
else:
raise TypeError(f"Not a valid sequence: {type(value)}")


def _serialize_field(value: Any) -> str:
if _is_sequence(value):
if is_sequence(value):
try:
s = _serialize_list(value)
except TypeError:
Expand All @@ -64,7 +49,7 @@ def _serialize_field(value: Any) -> str:


def _serialize_dimensions(value: Any) -> str:
if _is_sequence(value) and len(value) == 7:
if is_sequence(value) and len(value) == 7:
return f"[{' '.join(str(v) for v in value)}]"
else:
raise TypeError(f"Not a valid dimension set: {type(value)}")
Expand Down
31 changes: 23 additions & 8 deletions foamlib/_subprocesses.py → foamlib/_util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import asyncio
import sys
import subprocess

from pathlib import Path
from typing import Union, Sequence, Mapping
import subprocess
from subprocess import CalledProcessError
from typing import Any, Mapping, Sequence, Union

try:
import numpy as np
except ModuleNotFoundError:
numpy = False
else:
numpy = True


def is_sequence(value: Any) -> bool:
return (
isinstance(value, Sequence)
and not isinstance(value, str)
or numpy
and isinstance(value, np.ndarray)
)


__all__ = ["run_process", "run_process_async", "CalledProcessError"]
CalledProcessError = subprocess.CalledProcessError


def run_process(
Expand All @@ -16,7 +32,7 @@ def run_process(
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
shell = isinstance(cmd, str) or not isinstance(cmd, Sequence)
shell = not is_sequence(cmd)

if sys.version_info < (3, 8):
if shell:
Expand All @@ -28,8 +44,7 @@ def run_process(
cmd,
cwd=cwd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
capture_output=True,
shell=shell,
check=check,
)
Expand All @@ -44,7 +59,7 @@ async def run_process_async(
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
if isinstance(cmd, str) or not isinstance(cmd, Sequence):
if not is_sequence(cmd):
proc = await asyncio.create_subprocess_shell(
str(cmd),
cwd=cwd,
Expand Down

0 comments on commit 20df81e

Please sign in to comment.