Skip to content

Commit

Permalink
update syntax for py3
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Mar 10, 2023
1 parent 739b4ad commit 39734cf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
19 changes: 4 additions & 15 deletions miutil/fdio.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import logging
import re
from collections.abc import Iterable
from contextlib import contextmanager
from os import makedirs
from os import fspath, makedirs
from pathlib import Path
from shutil import copyfileobj, rmtree
from tempfile import mkdtemp
from zipfile import ZipFile

try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
try:
from os import fspath
except ImportError:
fspath = str
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path

from tqdm.auto import tqdm
from tqdm.utils import CallbackIOWrapper

Expand All @@ -32,7 +21,7 @@ def create_dir(pth):
try:
makedirs(fspath(pth))
except Exception as exc:
log.warning("cannot create:%s:%s" % (pth, exc))
log.warning("cannot create:%s:%s", pth, exc)


def is_iter(x):
Expand Down
12 changes: 6 additions & 6 deletions miutil/mlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def check_output_u8(*args, **kwargs):

def env_prefix(key, dir):
try:
os.environ[key] = "%s%s%s" % (os.environ[key], os.pathsep, fspath(dir))
os.environ[key] = f"{os.environ[key]}{os.pathsep}{fspath(dir)}"
except KeyError:
os.environ[key] = str(fspath(dir))
os.environ[key] = fspath(dir)


@lru_cache()
Expand Down Expand Up @@ -148,7 +148,7 @@ def _install_engine():
def get_runtime(cache="~/.mcr", version=99):
cache = Path(cache).expanduser()
mcr_root = cache
i = mcr_root / ("v%d"%version)
i = mcr_root / f"v{version}"
if i.is_dir():
mcr_root = i
else:
Expand All @@ -170,8 +170,8 @@ def get_runtime(cache="~/.mcr", version=99):
if system() == "Linux":
install.chmod(0o755)
check_output_u8([
fspath(install), "-P",
'bean421.installLocation="%s"' % fspath(cache), "-silent"])
fspath(install), "-P", f'bean421.installLocation="{fspath(cache)}"',
"-silent"])
else:
raise NotImplementedError(
dedent("""\
Expand All @@ -181,7 +181,7 @@ def get_runtime(cache="~/.mcr", version=99):
""").format(fspath(install), system()))
else:
raise IndexError(version)
mcr_root /= "v%d" % version
mcr_root /= f"v{version}"
log.info("Installed")

# bin
Expand Down
6 changes: 3 additions & 3 deletions miutil/mlab/beautify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
def ensure_mbeautifier(*args, **kwargs):
eng = get_engine(*args, **kwargs)
fn = get_file(
"MBeautifier-%s.zip" % MBEAUTIFIER_REV[:7],
"https://github.com/davidvarga/MBeautifier/archive/%s.zip" % MBEAUTIFIER_REV,
f"MBeautifier-{MBEAUTIFIER_REV[:7]}.zip",
f"https://github.com/davidvarga/MBeautifier/archive/{MBEAUTIFIER_REV}.zip",
)
outpath = path.join(path.dirname(fn), "MBeautifier-%s" % MBEAUTIFIER_REV)
outpath = path.join(path.dirname(fn), f"MBeautifier-{MBEAUTIFIER_REV}")
if not path.exists(outpath):
with ZipFile(fn) as fd:
fd.extractall(path=path.dirname(outpath))
Expand Down
4 changes: 2 additions & 2 deletions miutil/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_file(fname, origin, cache_dir=None, chunk_size=None):
fpath = path.join(cache_dir, fname)

if not path.exists(fpath):
log.debug("Downloading %s from %s" % (fpath, origin))
log.debug("Downloading %s from %s", fpath, origin)
try:
d = requests.get(origin, stream=True)
with tqdm(
Expand Down Expand Up @@ -93,7 +93,7 @@ def urlopen_cached(url, outdir, fname=None, mode="rb"):
if fname is None:
fname = Path(urlparse(url).path).name
fout = outdir / fname
cache = outdir / (fspath(fname) + ".url")
cache = outdir / f"{fspath(fname)}.url"
if not fout.is_file() or not cache.is_file() or cache.read_text().strip() != url:
fi = urlopen(url)
with fout.open("wb") as raw:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_imio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def test_nii(tmp_path):
fname = tmp_path / "test_nii.nii"
nii.array2nii(x, np.eye(4), fname, flip=(1, 1, 1))
nii.nii_gzip(fname)
assert (imread(fspath(fname) + ".gz") == x).all()
assert (imread(f"{fspath(fname)}.gz") == x).all()
2 changes: 1 addition & 1 deletion tests/test_mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def eng():
try:
res = get_engine()
except Exception as exc:
skip("MATLAB not found:\n%s" % exc)
skip(f"MATLAB not found:\n{exc}")

if engine is None:
from matlab import engine
Expand Down

0 comments on commit 39734cf

Please sign in to comment.