Skip to content

Commit

Permalink
added an example for mutable_instance usage
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Jul 12, 2024
1 parent e78378e commit e92d9d1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 16 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,49 @@ The core of the PyConstClasses package are the `const_class` and `static_const_c
Error: Cannot modify const attribute `version` of class `ProjectConfiguration`
```

Although the `static_const_class` decorator prevents "standard" class instantiation, you can create mutable instances of such classes:

```python
@cc.static_const_class
class DatabaseConfiguration:
host: str = "localhost"
port: int = 5432
username: str = "admin"
password: str = "secret"

def __repr__(self):
return (
f"DatabaseConfiguration:\n"
f"Host: {self.host}\n"
f"Port: {self.port}\n"
f"Username: {self.username}\n"
f"Password: {self.password}"
)


if __name__ == "__main__":
print(f"Database configuration:\n{DatabaseConfiguration}")

try:
DatabaseConfiguration.host = "remotehost"
except cc.ConstError as err:
print(f"Error: {err}")

try:
DatabaseConfiguration.port = 3306
except cc.ConstError as err:
print(f"Error: {err}")

# Create a mutable instance for testing or development
mutable_config = cc.mutable_instance(DatabaseConfiguration)
mutable_config.host = "testhost"
mutable_config.username = "testuser"
mutable_config.password = "testpassword"

print("\nMutable configuration for testing:")
print(mutable_config)
```

> [!IMPORTANT]
> In the current version of the package the constant attributes have to be defined using annotations, i.e. the `member: type (= value)` syntax of the class member declaration is required

Expand Down
41 changes: 41 additions & 0 deletions examples/static_const_class_mutable_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import constclasses as cc


@cc.static_const_class
class DatabaseConfiguration:
host: str = "localhost"
port: int = 5432
username: str = "admin"
password: str = "secret"

def __repr__(self):
return (
f"DatabaseConfiguration:\n"
f"Host: {self.host}\n"
f"Port: {self.port}\n"
f"Username: {self.username}\n"
f"Password: {self.password}"
)


if __name__ == "__main__":
print(f"Database configuration:\n{DatabaseConfiguration}")

try:
DatabaseConfiguration.host = "remotehost"
except cc.ConstError as err:
print(f"Error: {err}")

try:
DatabaseConfiguration.port = 3306
except cc.ConstError as err:
print(f"Error: {err}")

# Create a mutable instance for testing or development
mutable_config = cc.mutable_instance(DatabaseConfiguration)
mutable_config.host = "testhost"
mutable_config.username = "testuser"
mutable_config.password = "testpassword"

print("\nMutable configuration for testing:")
print(mutable_config)
33 changes: 17 additions & 16 deletions test/test_static_const_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,6 @@ def test_member_modification():
assert util.msg(err) == util.const_error_msg(attr_name, STATIC_CONST_CLASS_NAME)


def test_mutable_instance_of_static_const_class():
mut_instance = mutable_instance(StaticConstClass)

for attr_name in ATTR_NAMES:
assert getattr(mut_instance, attr_name) == ATTR_VALS_1[attr_name]
setattr(mut_instance, attr_name, ATTR_VALS_2[attr_name])
assert getattr(mut_instance, attr_name) == ATTR_VALS_2[attr_name]

for attr_name in MANDATORY_CONST_ATTRS:
with pytest.raises(ConstError) as err:
setattr(mut_instance, attr_name, DUMMY_VALUE)
assert util.msg(err) == util.const_error_msg(
"mutable_StaticConstClass", attr_name
)


def test_member_modification_with_include_parameter():
include = {X_ATTR_NAME}

Expand Down Expand Up @@ -136,3 +120,20 @@ class StaticConstClassStrictTypes:
with pytest.raises(TypeError) as err:
setattr(StaticConstClassStrictTypes, attr_name, DUMMY_VALUE)
assert util.msg(err).startswith(util.invalid_type_error_msg_prefix())


def test_mutable_instance_of_static_const_class():
mut_instance = mutable_instance(StaticConstClass)

for attr_name in ATTR_NAMES:
assert getattr(mut_instance, attr_name) == ATTR_VALS_1[attr_name]
setattr(mut_instance, attr_name, ATTR_VALS_2[attr_name])
assert getattr(mut_instance, attr_name) == ATTR_VALS_2[attr_name]

for attr_name in MANDATORY_CONST_ATTRS:
assert not getattr(mut_instance, attr_name)
with pytest.raises(ConstError) as err:
setattr(mut_instance, attr_name, DUMMY_VALUE)
assert util.msg(err) == util.const_error_msg(
"mutable_StaticConstClass", attr_name
)

0 comments on commit e92d9d1

Please sign in to comment.