Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/synced collection/optimize protected key lookup #510

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions signac/contrib/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import shutil
from copy import deepcopy
from json import JSONDecodeError
from typing import Tuple
from typing import FrozenSet

from deprecation import deprecated

Expand Down Expand Up @@ -46,7 +46,7 @@ class _StatePointDict(JSONAttrDict):
job directory migrations.
"""

_PROTECTED_KEYS: Tuple[str, ...] = JSONAttrDict._PROTECTED_KEYS + ("_jobs",)
_PROTECTED_KEYS: FrozenSet[str] = JSONAttrDict._PROTECTED_KEYS.union(("_jobs",))
_all_validators = (json_attr_dict_validator,)

def __init__(
Expand Down
65 changes: 34 additions & 31 deletions signac/synced_collections/backends/collection_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import uuid
import warnings
from collections.abc import Mapping, Sequence
from typing import Callable
from typing import Callable, FrozenSet
from typing import Sequence as Sequence_t
from typing import Tuple

from .. import SyncedCollection, SyncedDict, SyncedList
from ..buffers.memory_buffered_collection import SharedMemoryFileBufferedCollection
Expand Down Expand Up @@ -278,24 +277,26 @@ def _lock_id(self):


# These are the common protected keys used by all JSONDict types.
_JSONDICT_PROTECTED_KEYS = (
# These are all protected keys that are inherited from data type classes.
"_data",
"_name",
"_suspend_sync_",
"_load",
"_sync",
"_root",
"_validators",
"_all_validators",
"_load_and_save",
"_suspend_sync",
"_supports_threading",
"_LoadSaveType",
"registry",
# These keys are specific to the JSON backend.
"_filename",
"_write_concern",
_JSONDICT_PROTECTED_KEYS = frozenset(
(
# These are all protected keys that are inherited from data type classes.
"_data",
"_name",
"_suspend_sync_",
"_load",
"_sync",
"_root",
"_validators",
"_all_validators",
"_load_and_save",
"_suspend_sync",
"_supports_threading",
"_LoadSaveType",
"registry",
# These keys are specific to the JSON backend.
"_filename",
"_write_concern",
)
)


Expand Down Expand Up @@ -342,7 +343,7 @@ class JSONDict(JSONCollection, SyncedDict):

"""

_PROTECTED_KEYS: Tuple[str, ...] = _JSONDICT_PROTECTED_KEYS
_PROTECTED_KEYS: FrozenSet[str] = _JSONDICT_PROTECTED_KEYS

def __init__(
self,
Expand Down Expand Up @@ -443,20 +444,22 @@ class BufferedJSONCollection(SerializedFileBufferedCollection, JSONCollection):


# These are the keys common to buffer backends.
_BUFFERED_PROTECTED_KEYS = (
"buffered",
"_is_buffered",
"_buffer_lock",
"_buffer_context",
"_buffered_collections",
_BUFFERED_PROTECTED_KEYS = frozenset(
(
"buffered",
"_is_buffered",
"_buffer_lock",
"_buffer_context",
"_buffered_collections",
)
)


class BufferedJSONDict(BufferedJSONCollection, SyncedDict):
"""A buffered :class:`JSONDict`."""

_PROTECTED_KEYS: Tuple[str, ...] = (
_JSONDICT_PROTECTED_KEYS + _BUFFERED_PROTECTED_KEYS
_PROTECTED_KEYS: FrozenSet[str] = (
_JSONDICT_PROTECTED_KEYS | _BUFFERED_PROTECTED_KEYS
)

def __init__(
Expand Down Expand Up @@ -521,8 +524,8 @@ class MemoryBufferedJSONCollection(SharedMemoryFileBufferedCollection, JSONColle
class MemoryBufferedJSONDict(MemoryBufferedJSONCollection, SyncedDict):
"""A buffered :class:`JSONDict`."""

_PROTECTED_KEYS: Tuple[str, ...] = (
_JSONDICT_PROTECTED_KEYS + _BUFFERED_PROTECTED_KEYS
_PROTECTED_KEYS: FrozenSet[str] = (
_JSONDICT_PROTECTED_KEYS | _BUFFERED_PROTECTED_KEYS
)

def __init__(
Expand Down
12 changes: 8 additions & 4 deletions signac/synced_collections/data_types/attr_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
simple mixin can be combined via inheritance without causing much difficulty.
"""

from typing import Tuple
from typing import FrozenSet


class AttrDict:
Expand All @@ -34,7 +34,7 @@ class variable, which indicates known attributes of the object. This indication

"""

_PROTECTED_KEYS: Tuple[str, ...] = ()
_PROTECTED_KEYS: FrozenSet[str] = frozenset()

def __getattr__(self, name):
if name.startswith("__"):
Expand All @@ -49,13 +49,17 @@ def __setattr__(self, key, value):
# the object has been fully instantiated. We may want to add a try
# except in the else clause in case someone subclasses these and tries
# to use d['foo'] inside a constructor prior to _data being defined.
if key.startswith("__") or key in self._PROTECTED_KEYS:
# The order of these checks assumes that setting protected keys will be
# much more common than setting dunder attributes.
if key in self._PROTECTED_KEYS or key.startswith("__"):
super().__setattr__(key, value)
else:
self.__setitem__(key, value)

def __delattr__(self, key):
if key.startswith("__") or key in self._PROTECTED_KEYS:
# The order of these checks assumes that deleting protected keys will be
# much more common than deleting dunder attributes.
if key in self._PROTECTED_KEYS or key.startswith("__"):
super().__delattr__(key)
else:
self.__delitem__(key)