Coverage for sources/accretive/objects.py: 100%
19 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 02:16 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 02:16 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21# pylint: disable=line-too-long
22''' Accretive objects.
24Provides the base class for objects with accretive attributes. Once an
25attribute is set on an instance, it cannot be reassigned or deleted.
27The implementation uses a special dictionary type for attribute storage that
28enforces the accretive behavior. This makes it suitable as a base class for:
30* Configuration objects
31* Plugin interfaces
32* Immutable data containers
33* Objects requiring attribute stability
35>>> from accretive import Object
36>>> obj = Object( )
37>>> obj.x = 1 # Add new instance attribute
38>>> obj.y = 2 # Add another instance attribute
39>>> obj.x = 3 # Attempt modification
40Traceback (most recent call last):
41 ...
42accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'x'.
43'''
44# pylint: enable=line-too-long
47from . import __
48from . import classes as _classes
51class _Dictionary(
52 __.CoreDictionary[ __.H, __.V ], metaclass = _classes.Class
53): pass
56class Object:
57 ''' Accretive objects. '''
59 __slots__ = ( '__dict__', )
61 def __init__( self, *posargs: __.a.Any, **nomargs: __.a.Any ) -> None:
62 super( ).__setattr__( '__dict__', _Dictionary( ) )
63 # Pass all arguments down MRO chain without consuming any.
64 super( ).__init__( *posargs, **nomargs )
66 def __repr__( self ) -> str:
67 return "{fqname}( )".format( fqname = __.calculate_fqname( self ) )
69 def __delattr__( self, name: str ) -> None:
70 from .exceptions import AttributeImmutabilityError
71 raise AttributeImmutabilityError( name )
73 def __setattr__( self, name: str, value: __.a.Any ) -> None:
74 if hasattr( self, name ):
75 from .exceptions import AttributeImmutabilityError
76 raise AttributeImmutabilityError( name )
77 super( ).__setattr__( name, value )
79Object.__doc__ = __.generate_docstring(
80 Object, 'instance attributes accretion' )