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

Add property_or_method decorator #1301

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions dimod/binary/binary_quadratic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from dimod.binary.cybqm import cyBQM_float32, cyBQM_float64
from dimod.binary.pybqm import pyBQM
from dimod.binary.vartypeview import VartypeView
from dimod.decorators import forwarding_method, unique_variable_labels
from dimod.decorators import forwarding_method, unique_variable_labels, property_or_method
from dimod.quadratic import QuadraticModel, QM
from dimod.quadratic.quadratic_model import _VariableArray
from dimod.serialization.fileview import SpooledTemporaryFile, _BytesIO, VariablesSection
Expand Down Expand Up @@ -544,15 +544,15 @@ def offset(self) -> np.number:
def offset(self, offset):
self.data.offset = offset

@property
@property_or_method
def num_interactions(self) -> int:
"""Number of interactions in the model.

The complexity is linear in the number of variables.
"""
return self.data.num_interactions()

@property
@property_or_method
def num_variables(self) -> int:
"""Number of variables in the model."""
return self.data.num_variables()
Expand Down Expand Up @@ -614,7 +614,7 @@ def variables(self) -> Variables:
"""The variables of the binary quadratic model."""
return self.data.variables

@property
@property_or_method
def vartype(self) -> Vartype:
"""The model's variable type.

Expand Down
27 changes: 27 additions & 0 deletions dimod/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from numbers import Integral

from dimod.exceptions import BinaryQuadraticModelStructureError, WriteableError
from dimod.future import shape_methods
from dimod.utilities import new_variable_label
from dimod.vartypes import as_vartype

Expand Down Expand Up @@ -421,3 +422,29 @@ def conditional_unique_label(label = None, *args, **kwargs):
return qm

return conditional_unique_label


class property_or_method:
def __init__(self, func):
self.func = func

def __get__(self, obj, objtype=None):
func = self.func

caller = inspect.currentframe().f_back
if caller.f_globals.get("shape_methods", None) is not shape_methods:
# warnings.warn(
# f"Accessing '{type(obj).__name__}.{func.__name__}' as a property is deprecated, "
# "use 'from dimod.future import shape_methods' and "
# f"'{type(obj).__name__}.{func.__name__}()' instead.",
# DeprecationWarning, stacklevel=2)
return func(obj)

@wraps(func)
def wrapper(*args, **kwargs):
return func(obj, *args, **kwargs)

return wrapper

def __set__(self, obj, value):
raise AttributeError("can't set attribute") # same error as property
17 changes: 17 additions & 0 deletions dimod/future.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2018 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class shape_methods:
pass
6 changes: 3 additions & 3 deletions dimod/quadratic/quadratic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
ArrayLike = Any
DTypeLike = Any

from dimod.decorators import forwarding_method, unique_variable_labels
from dimod.decorators import forwarding_method, unique_variable_labels, property_or_method
from dimod.quadratic.cyqm import cyQM_float32, cyQM_float64
from dimod.serialization.fileview import SpooledTemporaryFile, _BytesIO
from dimod.serialization.fileview import VariablesSection, Section
Expand Down Expand Up @@ -352,15 +352,15 @@ def dtype(self) -> np.dtype:
"""Data-type of the model's biases."""
return self.data.dtype

@property
@property_or_method
def num_interactions(self) -> int:
"""Number of interactions in the model.

The complexity is linear in the number of variables.
"""
return self.data.num_interactions()

@property
@property_or_method
def num_variables(self) -> int:
"""Number of variables in the model."""
return self.data.num_variables()
Expand Down