Skip to content

Commit

Permalink
ENH: rewrite backports from Python std lib in a fashion that can be a…
Browse files Browse the repository at this point in the history
…utomatically cleaned up with pyupgrade
  • Loading branch information
neutrinoceros committed Sep 27, 2021
1 parent ef7d320 commit a0c8587
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
42 changes: 22 additions & 20 deletions yt/geometry/geometry_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
import os
import sys
import weakref

import numpy as np
Expand All @@ -15,6 +16,27 @@
parallel_root_only,
)

if sys.version_info >= (3, 8):
from functools import cached_property
else:

def cached_property(func):
n = f"_{func.__name__}"

def cached_func(self):
if self._cache and getattr(self, n, None) is not None:
return getattr(self, n)
if self.data_size is None:
tr = self._accumulate_values(n[1:])
else:
tr = func(self)
if self._cache:

setattr(self, n, tr)
return tr

return property(cached_func)


class Index(ParallelAnalysisInterface, abc.ABC):
"""The base index class"""
Expand Down Expand Up @@ -243,26 +265,6 @@ def _chunk(self, dobj, chunking_style, ngz=0, **kwargs):
raise NotImplementedError


def cached_property(func):
# TODO: remove this once minimal supported version of Python reaches 3.8
# and replace with functools.cached
n = f"_{func.__name__}"

def cached_func(self):
if self._cache and getattr(self, n, None) is not None:
return getattr(self, n)
if self.data_size is None:
tr = self._accumulate_values(n[1:])
else:
tr = func(self)
if self._cache:

setattr(self, n, tr)
return tr

return property(cached_func)


class YTDataChunk:
def __init__(
self,
Expand Down
8 changes: 1 addition & 7 deletions yt/visualization/plot_window.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from collections import defaultdict
from functools import wraps
from numbers import Number
Expand Down Expand Up @@ -48,18 +49,11 @@
)
from .plot_modifications import callback_registry

import sys # isort: skip

if sys.version_info < (3, 10):
# this function is deprecated in more_itertools
# because it is superseded by the standard library
from more_itertools import zip_equal
else:

def zip_equal(*args):
# FUTURE: when only Python 3.10+ is supported,
# drop this conditional and call the builtin zip
# function directly where due
return zip(*args, strict=True)


Expand Down
23 changes: 22 additions & 1 deletion yt/visualization/volume_rendering/old_camera.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import builtins
import sys
from copy import deepcopy

import numpy as np

from yt.config import ytcfg
from yt.data_objects.api import ImageArray
from yt.funcs import ensure_numpy_array, get_num_threads, get_pbar, is_sequence, mylog
from yt.geometry.geometry_handler import cached_property
from yt.units.yt_array import YTArray
from yt.utilities.amr_kdtree.api import AMRKDTree
from yt.utilities.exceptions import YTNotInsideNotebook
Expand Down Expand Up @@ -35,6 +35,27 @@

from .transfer_functions import ProjectionTransferFunction

if sys.version_info >= (3, 8):
from functools import cached_property
else:

def cached_property(func):
n = f"_{func.__name__}"

def cached_func(self):
if self._cache and getattr(self, n, None) is not None:
return getattr(self, n)
if self.data_size is None:
tr = self._accumulate_values(n[1:])
else:
tr = func(self)
if self._cache:

setattr(self, n, tr)
return tr

return property(cached_func)


def get_corners(le, re):
return np.array(
Expand Down

0 comments on commit a0c8587

Please sign in to comment.