Fixed methods

Removed Attributes.properties and implemented private attributes instead.
This commit is contained in:
SimpleArt
2021-06-10 22:44:45 -04:00
parent d752f08a4a
commit 7a7a985b49

View File

@ -123,8 +123,6 @@ class Attributes:
Contains default attributes for each attribute. Contains default attributes for each attribute.
""" """
properties: Dict[str, Any] = field(default_factory=dict, init=False, repr=False, compare=False)
run: int = 0 run: int = 0
chromosome_length: int = 10 chromosome_length: int = 10
@ -216,9 +214,9 @@ class Attributes:
self.database.insert_current_chromosome(self.current_generation, chromosome) self.database.insert_current_chromosome(self.current_generation, chromosome)
#==================================================# #=========================#
# Properties for attributes behaving like methods. # # Properties for methods. #
#==================================================# #=========================#
def get_method(name: str) -> Callable[[Attributes], Callable[..., Any]]: def get_method(name: str) -> Callable[[Attributes], Callable[..., Any]]:
@ -236,7 +234,7 @@ def get_method(name: str) -> Callable[[Attributes], Callable[..., Any]]:
The getter property, taking in an object and returning the method. The getter property, taking in an object and returning the method.
""" """
def getter(self: Attributes) -> Callable[..., Any]: def getter(self: Attributes) -> Callable[..., Any]:
return self.properties[name] return getattr(self, f"_{name}")
return getter return getter
@ -261,7 +259,7 @@ def set_method(name: str) -> Callable[[Attributes, Optional[Callable[..., Any]]]
raise TypeError(f"{name} must be a method i.e. callable.") raise TypeError(f"{name} must be a method i.e. callable.")
elif next(iter(signature(method).parameters), None) in ("self", "ga"): elif next(iter(signature(method).parameters), None) in ("self", "ga"):
method = MethodType(method, self) method = MethodType(method, self)
self.properties[name] = method setattr(self, f"_{name}", method)
return setter return setter
@ -280,11 +278,16 @@ for name in (
"chromosome_impl", "chromosome_impl",
"population_impl", "population_impl",
): ):
# Rename to private attribute:
# name -> _name
setattr(Attributes, f"_{name}", getattr(Attributes, name))
# Replace name with property
setattr(Attributes, name, property(get_method(name), set_method(name))) setattr(Attributes, name, property(get_method(name), set_method(name)))
#============================# #============================#
# Static checking properties # # Static checking properties $
# for non-methods #
#============================# #============================#
@ -323,7 +326,7 @@ def get_attr(name: str) -> Callable[[Attributes], Any]:
A getter method which returns an attribute. A getter method which returns an attribute.
""" """
def getter(self: Attributes) -> Any: def getter(self: Attributes) -> Any:
return self.properties[name] return getattr(self, f"_{name}")
return getter return getter
@ -348,7 +351,7 @@ def set_attr(name: str, check: Callable[[Any], bool], error: str) -> Callable[[A
""" """
def setter(self: Attributes, value: Any) -> Any: def setter(self: Attributes, value: Any) -> Any:
if check(value): if check(value):
self.properties[name] = value setattr(self, f"_{name}", value)
else: else:
raise ValueError(error) raise ValueError(error)
return setter return setter