Make the constant Sentinel() class public

This pattern can be useful so make it publicly available.

Change-Id: I8fab1a5804aecf17a00a908646b6a20869c4a03b
This commit is contained in:
Henry Gessau
2016-06-08 11:50:46 -04:00
parent 0a6a347c21
commit 6f09e4dc18
2 changed files with 15 additions and 13 deletions

View File

@@ -246,15 +246,18 @@ DEVICE_NAME_MAX_LEN = 15
ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f' ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
class Sentinel(object):
"""A constant object that does not change even when copied."""
def __deepcopy__(self, memo):
# Always return the same object because this is essentially a constant.
return self
############################# #############################
# Attribute related constants # Attribute related constants
############################# #############################
class _Sentinel(object):
def __deepcopy__(self, memo):
# always return the same object because this is essentially a constant
return self
ATTR_NOT_SPECIFIED = _Sentinel() ATTR_NOT_SPECIFIED = Sentinel()
HEX_ELEM = '[0-9A-Fa-f]' HEX_ELEM = '[0-9A-Fa-f]'
UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}', UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}',

View File

@@ -12,17 +12,16 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
""" import copy
test_neutron_lib
----------------------------------
Tests for `neutron_lib` module. from neutron_lib import constants
"""
from neutron_lib.tests import _base as base from neutron_lib.tests import _base as base
class TestNeutron_lib(base.BaseTestCase): class TestNeutronLib(base.BaseTestCase):
def test_something(self): def test_sentinel_constant(self):
pass foo = constants.Sentinel()
bar = copy.deepcopy(foo)
self.assertEqual(id(foo), id(bar))