Skip to content

Commit

Permalink
cleanup and prep
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Sep 4, 2024
1 parent 8041109 commit 67f3a3a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,53 @@ The core of the PyConstClasses package are the `const_class` and `static_const_c
Error: Cannot modify const attribute `last_name` of class `Person`
```

This `const_class` decorators also provides the `new` method which allows for the creationg of new instances of the class based on an existing instance with the option to modify individual fields:

```python
# const_class_new.py

@cc.const_class(with_kwargs=True)
class PersonKwargs:
first_name: str
last_name: str
age: int

def __repr__(self) -> str:
return f"(kwargs) {self.first_name} {self.last_name} [age: {self.age}]"


@cc.const_class(with_kwargs=False)
class PersonArgs:
first_name: str
last_name: str
age: int

def __repr__(self) -> str:
return f"(args) {self.first_name} {self.last_name} [age: {self.age}]"


if __name__ == "__main__":
john = PersonKwargs(first_name="John", last_name="Doe", age=21)
print(f"{john = }")

john_aged = john.new(age=22)
print(f"{john_aged = }")

john = PersonArgs("John", "Doe", 21)
print(f"{john = }")

john_aged = john.new(age=22)
print(f"{john_aged = }")
```

This program will produce the following output:
```
john = (kwargs) John Doe [age: 21]
john_aged = (kwargs) John Doe [age: 22]
john = (args) John Doe [age: 21]
john_aged = (args) John Doe [age: 22]
```

* The `static_const_class` deacorator allows you to define a pseudo-static resource with const members (it creates an instance of the decorated class):

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


@cc.const_class(with_kwargs=True)
class PersonKwargs:
first_name: str
last_name: str
age: int

def __repr__(self) -> str:
return f"(kwargs) {self.first_name} {self.last_name} [age: {self.age}]"


@cc.const_class(with_kwargs=False)
class PersonArgs:
first_name: str
last_name: str
age: int

def __repr__(self) -> str:
return f"(args) {self.first_name} {self.last_name} [age: {self.age}]"


if __name__ == "__main__":
john = PersonKwargs(first_name="John", last_name="Doe", age=21)
print(f"{john = }")

john_aged = john.new(age=22)
print(f"{john_aged = }")

john = PersonArgs("John", "Doe", 21)
print(f"{john = }")

john_aged = john.new(age=22)
print(f"{john_aged = }")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyconstclasses"
version = "1.0.0"
version = "1.0.4"
description = "Package with const class decoratos and utility"
authors = [
{name = "SpectraL519"}
Expand Down
4 changes: 2 additions & 2 deletions src/constclasses/const_class.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from copy import deepcopy

from .ccerror import ConstError, InitializationError
from .const_class_base import (
CC_BASE_ATTR_NAME,
CC_INITIALIZED_ATTR_NAME,
ConstClassBase,
)

from copy import deepcopy


def const_class_impl(
cls,
Expand Down
12 changes: 10 additions & 2 deletions test/test_const_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ class ConstClassWithKwargs:
s: str

def __eq__(self, other: object) -> bool:
return isinstance(other, self.__class__) and other.x == self.x and other.s == self.s
return (
isinstance(other, self.__class__)
and other.x == self.x
and other.s == self.s
)

const_instance = ConstClassWithKwargs(**ATTR_VALS_1)

Expand All @@ -228,7 +232,11 @@ class ConstClassWithKwargs:
s: str

def __eq__(self, other: object) -> bool:
return isinstance(other, self.__class__) and other.x == self.x and other.s == self.s
return (
isinstance(other, self.__class__)
and other.x == self.x
and other.s == self.s
)

const_instance = ConstClassWithKwargs(*(ATTR_VALS_1.values()))

Expand Down

0 comments on commit 67f3a3a

Please sign in to comment.