Coverage for sources/frigid/namespaces.py: 100%
20 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-25 23:15 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-25 23:15 +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''' Immutable namespaces.
23 Provides a namespace type with immutable attributes. Similar to
24 :py:class:`types.SimpleNamespace`, but attributes cannot be modified or
25 deleted after initialization.
27 The namespace implementation is modeled after
28 :py:class:`types.SimpleNamespace` but adds immutability. Like
29 :py:class:`types.SimpleNamespace`, it provides a simple ``__repr__`` which
30 lists all attributes.
32 >>> from frigid import Namespace
33 >>> ns = Namespace( x = 1, y = 2 )
34 >>> ns.z = 3 # Attempt to add attribute
35 Traceback (most recent call last):
36 ...
37 frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'z'.
38 >>> ns.x = 4 # Attempt modification
39 Traceback (most recent call last):
40 ...
41 frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'x'.
42 >>> ns
43 frigid.namespaces.Namespace( x = 1, y = 2 )
44''' # noqa: E501
47from . import __
48from . import classes as _classes
51class Namespace( metaclass = _classes.Class ): # noqa: PLW1641
52 # TODO: Dynadoc fragments.
53 ''' Immutable namespaces. '''
55 __slots__ = ( '__dict__', )
57 def __init__(
58 self,
59 *iterables: __.DictionaryPositionalArgument[ __.H, __.V ],
60 **attributes: __.DictionaryNominativeArgument[ __.V ],
61 ) -> None:
62 self.__dict__.update(
63 __.ImmutableDictionary(
64 *iterables, **attributes ) ) # pyright: ignore
65 super( ).__init__( )
67 def __repr__( self ) -> str:
68 attributes = ', '.join(
69 f"{key} = {value!r}" for key, value
70 in getattr( self, '__dict__', { } ).items( ) )
71 fqname = __.ccutils.qualify_class_name( type( self ) )
72 if not attributes: return f"{fqname}( )"
73 return f"{fqname}( {attributes} )"
75 def __eq__( self, other: __.typx.Any ) -> __.ComparisonResult:
76 if isinstance( other, ( Namespace, __.types.SimpleNamespace ) ):
77 return self.__dict__ == other.__dict__
78 return NotImplemented
80 def __ne__( self, other: __.typx.Any ) -> __.ComparisonResult:
81 if isinstance( other, ( Namespace, __.types.SimpleNamespace ) ):
82 return self.__dict__ != other.__dict__
83 return NotImplemented