Coverage for sources/accretive/namespaces.py: 100%
22 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 namespaces.
24Provides a namespace type that can grow but never shrink. Once an attribute is
25set, it cannot be modified or removed. This provides a simple way to create
26objects with named attributes that become immutable after assignment.
28The namespace implementation is modeled after :py:class:`types.SimpleNamespace`
29but adds accretive behavior. Like :py:class:`types.SimpleNamespace`, it
30provides a simple ``__repr__`` which lists all attributes.
32>>> from accretive import Namespace
33>>> ns = Namespace( apples = 12, bananas = 6 )
34>>> ns.cherries = 42 # Add new attribute
35>>> ns.apples = 14 # Attempt modification
36Traceback (most recent call last):
37 ...
38accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'apples'.
39>>> del ns.bananas # Attempt deletion
40Traceback (most recent call last):
41 ...
42accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'bananas'.
43'''
44# pylint: enable=line-too-long
47from . import __
48from . import objects as _objects
51class Namespace( _objects.Object ): # pylint: disable=eq-without-hash
52 ''' Accretive namespaces. '''
54 def __init__(
55 self,
56 *iterables: __.DictionaryPositionalArgument[ __.H, __.V ],
57 **attributes: __.DictionaryNominativeArgument[ __.V ],
58 ) -> None:
59 super( ).__init__( )
60 super( ).__getattribute__( '__dict__' ).update(
61 *iterables, **attributes )
63 def __repr__( self ) -> str:
64 attributes = ', '.join( tuple(
65 f"{key} = {value!r}" for key, value
66 in super( ).__getattribute__( '__dict__' ).items( ) ) )
67 fqname = __.calculate_fqname( self )
68 if not attributes: return f"{fqname}( )"
69 return f"{fqname}( {attributes} )"
71 def __eq__( self, other: __.a.Any ) -> __.ComparisonResult:
72 mydict = super( ).__getattribute__( '__dict__' )
73 if isinstance( other, ( Namespace, __.SimpleNamespace ) ):
74 return mydict == other.__dict__
75 return NotImplemented
77 def __ne__( self, other: __.a.Any ) -> __.ComparisonResult:
78 mydict = super( ).__getattribute__( '__dict__' )
79 if isinstance( other, ( Namespace, __.SimpleNamespace ) ):
80 return mydict != other.__dict__
81 return NotImplemented
83Namespace.__doc__ = __.generate_docstring(
84 Namespace, 'description of namespace', 'instance attributes accretion' )